我是春天安全的新手。我正在学习本教程
http://www.mkyong.com/spring-security/spring-security-form-login-example/
当我们请求欢迎页面重定向到登录表单然后在输入凭据后提供欢迎页面时,一切正常。问题是当我点击退出时它会注销但欢迎页面仍然可访问我们点击... @ localhost / SpringSecurity2 / welcome并且它不要求登录。我猜它与会话有关但没有得到如何解决这个问题。我也使用了delete-cookies =“JSESSIONID”,但是它没有用。
我只想在用户注销时再次点击welcome url.he应该指向登录表单而不是欢迎页面,因为它需要身份验证。很快需要帮助 我的安全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>
答案 0 :(得分:0)
最好的方法是通过实现SessionFilter
类来编写javax.servlet.Filter
类,如下所示。
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 文件中提及它,如下所示
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>com.filter.SessionFilter
</filter-class>
并且不要忘记使用以下语句在登出控制器中 使会话无效
request.getSession().invalidate();
希望这能解决您的问题