无法使用spring security登录

时间:2016-10-18 22:12:29

标签: spring-security

我创建了一个使用spring security登录的简单项目。 我的项目的构成由截图提供:

this screenshot

bellow是必需的文件:

弹簧security.xml文件

string script = string.Format("alert('{0}'); window.location.href='{1}';",
    msg, ResolveUrl("~/Dashboard.aspx"));

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
	xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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">

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

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

</beans:beans>

login.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<web_1:web-app  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
				xmlns="http://java.sun.com/xml/ns/j2ee" 
				xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee" 
				xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" 
				xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
									http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd 
									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" 
				id="Spring-Security-Custom-Login-Form_ID" 
				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>

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"
	xmlns:c="http://java.sun.com/jstl/core">
<body>
	<h3>Enter your username and password</h3>
	<form method="post" action="j_spring_security_check">
		UserName :<input name="j_username" type="text" /> <br />
		Password :<input name="j_password" type="password" /> <br />
		<input value="Login" type="submit" />
	</form>
	
	<br />
	<c:if test="${param.status=='error'}">
		<label style="color:red">Invalid username or password!!</label>
	</c:if>
	<c:if test="${param.status=='logout'}">
		<label style="color:red">Logged out successfully!</label>
	</c:if>
</body>
</html>

用(walid,111)登录后,我得到this exception 你能帮我解决一下吗?非常感谢。

1 个答案:

答案 0 :(得分:0)

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

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee"
	xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
	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"
	id="Spring-Security-Database-Authentication_ID" version="2.4">
	<display-name>Spring-Security-Database-Authentication</display-name>
	<servlet>
		<servlet-name>JSF Servlet</servlet-name>
		<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>JSF Servlet</servlet-name>
		<url-pattern>*.xhtml</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>JSF Servlet</servlet-name>
		<url-pattern>*.jsf</url-pattern>
	</servlet-mapping>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<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>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring-security.xml</param-value>
	</context-param>
</web-app>
&#13;
&#13;
&#13;

HTH