2014-10-08 81 views
0

我想了解Spring登錄系統如何在Internet上的一些示例後工作。我面臨許多困難。我最近的問題是,即使我正確輸入用戶名和密碼(它們在security-config.xml中設置),我也會被重定向到登錄頁面。春季登錄表單不能正常工作

這是我迄今爲止所做的(我會告訴你只是相關的代碼)。

安全-config.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/security 
     http://www.springframework.org/schema/security/spring-security-3.1.xsd 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> 

    <http use-expressions="true"> 
     <intercept-url pattern="/login*" access="isAnonymous()" /> 
     <intercept-url pattern="/**" access="isAuthenticated()"/> 

     <form-login 
     login-page='/login' 
     default-target-url="/profile" 
     authentication-failure-url="/login?error=true" /> 

     <logout logout-success-url="/login" /> 

    </http> 
    <authentication-manager> 
     <authentication-provider> 
     <user-service> 
      <user name="user1" password="user1Pass" authorities="ROLE_USER" /> 
     </user-service> 
     </authentication-provider> 
    </authentication-manager> 
</beans:beans> 

的web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 

    <display-name>Sample Spring Maven Project</display-name> 

    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
     /WEB-INF/mvc-dispatcher-servlet.xml, 
     /WEB-INF/security-config.xml 
     </param-value> 
    </context-param> 

    <filter> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 


    </web-app> 

這是我的控制器

import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestParam; 


@Controller 
public class LoginController { 

@RequestMapping("/login") 
public String getLoginForm(Model model, @RequestParam(required = false) String error, String logout) { 

    if (error != null) { 
     model.addAttribute("message", "Invalid username of password, try again !"); 

    } else if (logout != null) { 

    model.addAttribute("message", "Logged Out successfully, login again to continue !"); 
    } 
    return "login"; 
} 

@RequestMapping("/profile") 
public String geProfilePage() { 
    return "profile"; 
} 

} 

的login.jsp

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<html> 
<head> 
<title></title> 
</head> 
<body> 
<center> 

    <div 
    style="text-align: center; padding: 30px; border: 1px solid green; width: 250px;"> 
    <form method="post" 
    action="<c:url value='j_spring_security_check' />"> 

    <table> 
    <tr> 
     <td colspan="2" style="color: red">${message}</td> 

    </tr> 
    <tr> 
     <td>User Name:</td> 
     <td><input type="text" name="username" /></td> 
    </tr> 
    <tr> 
     <td>Password:</td> 
     <td><input type="password" name="password" /></td> 
    </tr> 
    <tr> 
     <td> </td> 
     <td><input type="submit" value="Login" /></td> 

    </tr> 
    </table> 
    </form> 
    </div> 
</center> 
</body> 
</html> 
+1

它的原因是返回「登錄」在你的控制器/登錄請求。 – 2014-10-08 14:37:03

+0

哦,我的上帝,這樣一個愚蠢的錯誤:(我一直跟着我發現的例子... 謝謝你的人 – MDP 2014-10-08 14:45:54

回答

1

由於在/ login請求返回「登錄」在你的控制器/登錄請求