首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用spring安全登录

无法使用spring安全登录
EN

Stack Overflow用户
提问于 2016-10-18 22:12:28
回答 1查看 238关注 0票数 0

我创建了一个使用spring安全性登录的简单项目。我的项目由截图介绍:

贝娄是所需的档案:

spring-security.xml

代码语言:javascript
复制
<?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>

web.xml

代码语言:javascript
复制
<?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>

login.xhtml

代码语言:javascript
复制
<?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>

welcome.xhtml

代码语言:javascript
复制
<?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>
	<h2>Welcome Page</h2>
	<br />
	<a href="j_spring_security_logout"> Logout </a>
</body>
</html>

登录后(瓦利德,111),我得到了这一例外,你能帮我解决这个问题吗?非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-19 06:55:37

您的web.xml似乎错了,您可以尝试如下:

代码语言:javascript
复制
<?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>

HTH

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40118961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档