即使在注销后也可以访问Spring security欢迎页面
我是春天安全的新手。我正在学习这个教程 当我们请求“欢迎页面”时,一切正常。它重定向到登录表单,然后在进入凭据后提供“欢迎页面”。问题是,当我单击“注销”时,它确实注销了,但欢迎页面仍然可以访问。我点击了…@localhost/SpringSecurity2/welcome,它没有请求登录。我想它与会话相关,但不是了解如何解决这个问题。我还使用了delete cookies=“JSESSIONID”,但它不起作用 我只想当用户登录并再次点击欢迎url时。他应该被引导到登录表单而不是欢迎页面,因为它需要身份验证。我很快就需要帮助 我的安全xml如下所示即使在注销后也可以访问Spring security欢迎页面,spring,spring-security,logout,Spring,Spring Security,Logout,我是春天安全的新手。我正在学习这个教程 当我们请求“欢迎页面”时,一切正常。它重定向到登录表单,然后在进入凭据后提供“欢迎页面”。问题是,当我单击“注销”时,它确实注销了,但欢迎页面仍然可以访问。我点击了…@localhost/SpringSecurity2/welcome,它没有请求登录。我想它与会话相关,但不是了解如何解决这个问题。我还使用了delete cookies=“JSESSIONID”,但它不起作用 我只想当用户登录并再次点击欢迎url时。他应该被引导到登录表单而不是欢迎页面,因
<?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-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">
<http auto-config="true">
<intercept-url pattern="/welcome*" access="ROLE_USER" />
<form-login login-page="/login" default-target-url="/welcome"
authentication-failure-url="/loginfailed" />
<logout
logout-success-url="/logout" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="mkyong" password="123456" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>com.filter.SessionFilter
</filter-class>
最好的方法是通过实现
javax.servlet.Filter
类来编写SessionFilter
类,如下所示
package com.filter;
public class SessionFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
if(null == session){
response.sendRedirect("/login.html");
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
并在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-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">
<http auto-config="true">
<intercept-url pattern="/welcome*" access="ROLE_USER" />
<form-login login-page="/login" default-target-url="/welcome"
authentication-failure-url="/loginfailed" />
<logout
logout-success-url="/logout" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="mkyong" password="123456" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>com.filter.SessionFilter
</filter-class>
希望这能解决您的问题将其添加到您的注销标记中,并且您应该在应用程序中实现logoutSuccessHandler。我已经这样做了,但不起作用。它给出了404错误,无法找到注销。您能告诉我如何实现logoutsuccesshandler吗?请检查您的loginController,以及您为注销方法返回的内容。我说的是映射页面,听起来好像是缓存问题。如果您使用SpringSecurity3.2,那么可以使用新的头支持来解决这个问题。请看,我已从下载了源代码,并将其构建到
war
。然后我部署到tomcat
,它工作得很好。我建议您也这样做,并比较文件以找出缺少的内容。因此,我们必须为此创建自己的筛选器…Spring Security没有任何内置功能来终止特定的用户会话?默认情况下,Spring Security会终止会话。我猜您正在查看欢迎页面的本地副本(缓存版本)。尝试以下操作,转到页面并刷新(或按CTRL键刷新),可能会要求您输入用户名/密码。@palak spring将在您在会话超时属性中提到的时间段后终止会话,但在您的情况下,您必须在注销事件中终止用户会话,因此您必须编写代码,在用户注销时使会话无效。