2016-10-18 243 views
0

我创建了一个使用弹簧安全登录的简单项目。 我的项目组成由截图提供:无法使用弹簧安全登录

this screenshot

波纹管是将所需的文件:

弹簧security.xml文件

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

 
\t <http auto-config="true"> 
 
\t \t <intercept-url pattern="/welcome.jsf" access="ROLE_USER" /> 
 
\t \t <form-login login-page="/login.jsf" default-target-url="/welcome.jsf" 
 
\t \t \t authentication-failure-url="/login.jsf?status=error" /> 
 
\t \t <logout logout-success-url="/login.jsf?status=logout" /> 
 
\t </http> 
 

 
\t <authentication-manager> 
 
\t \t <authentication-provider> 
 
\t \t \t <user-service> 
 
\t \t \t \t <user name="walid" password="111" authorities="ROLE_USER" /> 
 
\t \t \t </user-service> 
 
\t \t </authentication-provider> 
 
\t </authentication-manager> 
 

 
</beans:beans>

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
 
<web_1:web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 
\t \t \t \t xmlns="http://java.sun.com/xml/ns/j2ee" 
 
\t \t \t \t xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee" 
 
\t \t \t \t xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" 
 
\t \t \t \t xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 
\t \t \t \t \t \t \t \t \t http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd 
 
\t \t \t \t \t \t \t \t \t http://java.sun.com/xml/ns/j2ee 
 
\t \t \t \t \t \t \t \t \t http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd 
 
\t \t \t \t \t \t \t \t \t http://xmlns.jcp.org/xml/ns/javaee 
 
\t \t \t \t \t \t \t \t \t http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
 
\t \t \t \t id="Spring-Security-Custom-Login-Form_ID" 
 
\t \t \t \t version="2.4"> 
 
    <javaee:display-name>HelloWorld</javaee:display-name> 
 
    <javaee:listener> 
 
    <javaee:listener-class>org.springframework.web.context.ContextLoaderListener</javaee:listener-class> 
 
    </javaee:listener> 
 
    <javaee:filter> 
 
    <javaee:filter-name>springSecurityFilterChain</javaee:filter-name> 
 
    <javaee:filter-class>org.springframework.web.filter.DelegatingFilterProxy</javaee:filter-class> 
 
    </javaee:filter> 
 
    <javaee:filter-mapping> 
 
    <javaee:filter-name>springSecurityFilterChain</javaee:filter-name> 
 
    <javaee:url-pattern>/*</javaee:url-pattern> 
 
    </javaee:filter-mapping> 
 
    <javaee:context-param> 
 
    <javaee:param-name>contextConfigLocation</javaee:param-name> 
 
    <javaee:param-value>/WEB-INF/spring-security.xml</javaee:param-value> 
 
    </javaee:context-param> 
 
    <javaee:context-param> 
 
    <javaee:description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</javaee:description> 
 
    <javaee:param-name>javax.faces.STATE_SAVING_METHOD</javaee:param-name> 
 
    <javaee:param-value>client</javaee:param-value> 
 
    </javaee:context-param> 
 
    <javaee:context-param> 
 
    <javaee:param-name>javax.servlet.jsp.jstl.fmt.localizationContext</javaee:param-name> 
 
    <javaee:param-value>resources.application</javaee:param-value> 
 
    </javaee:context-param> 
 
    <javaee:listener> 
 
    <javaee:listener-class>com.sun.faces.config.ConfigureListener</javaee:listener-class> 
 
    </javaee:listener> 
 
    <javaee:servlet> 
 
    <javaee:servlet-name>Faces Servlet</javaee:servlet-name> 
 
    <javaee:servlet-class>javax.faces.webapp.FacesServlet</javaee:servlet-class> 
 
    <javaee:load-on-startup>1</javaee:load-on-startup> 
 
    </javaee:servlet> 
 
    <javaee:servlet-mapping> 
 
    <javaee:servlet-name>Faces Servlet</javaee:servlet-name> 
 
    <javaee:url-pattern>/faces/*</javaee:url-pattern> 
 
    </javaee:servlet-mapping> 
 
</web_1:web-app>

login.xhtml

<?xml version="1.0" encoding="UTF-8"?> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml" 
 
\t xmlns:c="http://java.sun.com/jstl/core"> 
 
<body> 
 
\t <h3>Enter your username and password</h3> 
 
\t <form method="post" action="j_spring_security_check"> 
 
\t \t UserName :<input name="j_username" type="text" /> <br /> 
 
\t \t Password :<input name="j_password" type="password" /> <br /> 
 
\t \t <input value="Login" type="submit" /> 
 
\t </form> 
 
\t 
 
\t <br /> 
 
\t <c:if test="${param.status=='error'}"> 
 
\t \t <label style="color:red">Invalid username or password!!</label> 
 
\t </c:if> 
 
\t <c:if test="${param.status=='logout'}"> 
 
\t \t <label style="color:red">Logged out successfully!</label> 
 
\t </c:if> 
 
</body> 
 
</html>

welcome.xhtml

<?xml version="1.0" encoding="UTF-8"?> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
<body> 
 
\t <h2>Welcome Page</h2> 
 
\t <br /> 
 
\t <a href="j_spring_security_logout"> Logout </a> 
 
</body> 
 
</html>

与(瓦利德111)登录后,我得到了this exception 你能帮我解决这个。非常感谢。

回答

0

的web.xml似乎是错误的,你可以试试这个:

<?xml version="1.0" encoding="UTF-8"?> 
 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 
\t xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee" 
 
\t xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" 
 
\t xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
 
\t id="Spring-Security-Database-Authentication_ID" version="2.4"> 
 
\t <display-name>Spring-Security-Database-Authentication</display-name> 
 
\t <servlet> 
 
\t \t <servlet-name>JSF Servlet</servlet-name> 
 
\t \t <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
 
\t \t <load-on-startup>1</load-on-startup> 
 
\t </servlet> 
 
\t <servlet-mapping> 
 
\t \t <servlet-name>JSF Servlet</servlet-name> 
 
\t \t <url-pattern>*.xhtml</url-pattern> 
 
\t </servlet-mapping> 
 
\t <servlet-mapping> 
 
\t \t <servlet-name>JSF Servlet</servlet-name> 
 
\t \t <url-pattern>*.jsf</url-pattern> 
 
\t </servlet-mapping> 
 
\t <listener> 
 
\t \t <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
 
\t </listener> 
 
\t <filter> 
 
\t \t <filter-name>springSecurityFilterChain</filter-name> 
 
\t \t <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
 
\t </filter> 
 
\t <filter-mapping> 
 
\t \t <filter-name>springSecurityFilterChain</filter-name> 
 
\t \t <url-pattern>/*</url-pattern> 
 
\t </filter-mapping> 
 
\t <context-param> 
 
\t \t <param-name>contextConfigLocation</param-name> 
 
\t \t <param-value>/WEB-INF/spring-security.xml</param-value> 
 
\t </context-param> 
 
</web-app>

HTH