我遇到了一个问题,当您已经有一个不再有效的 SessionID 时,公共(public) URL 在 Spring 安全性中不起作用。
例子:
我有用户注册页面并给它一个 permitAll 访问权限,如下所示:
http.authorizeRequests().antMatchers("/register**").permitAll();
http.authorizeRequests().anyRequest().authenticated();
http.formLogin().loginPage("/login").permitAll();
对于我的 session 设置,我有:
http.sessionManagement().invalidSessionUrl("/login?logoutcause=sessiontimeout");
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);
http.sessionManagement().sessionAuthenticationErrorUrl("/login");
http.sessionManagement().maximumSessions(1).maxSessionsPreventsLogin(true);
http.sessionManagement().sessionFixation().newSession();
如果我有一个来自前一个 session 的 sessionID,它可能是一个旧的和无效的,并且我点击了路由“/register”,spring 提示无效的 session ID 并将我重定向到“/login”。
顺便提一下:其他一切,如登录、资源管理、 protected URL 和注销,都按配置正常工作。
重现这一点: 在 Spring 中使用 Redis-Session 管理。进入登录页面,使用控制台刷新 redis 数据库。直接在浏览器中访问注册页面 -> 由于 session ID 无效而重定向到登录。
o.s.s.w.s.SessionManagementFilter : Requested session ID 8ad2e166-bc21-4646-8390-ad8d1043baec is invalid.
w.s.SimpleRedirectInvalidSessionStrategy : Starting new session (if required) and redirecting to '/login?logoutcause=sessiontimeout'
o.s.s.w.DefaultRedirectStrategy : Redirecting to '/login?logoutcause=sessiontimeout'
为什么 Spring 甚至会检查具有“公共(public)”访问权限的路由的 session ID?
下一步: 如果我完全禁用路由本身的任何安全检查,遗憾的是所需的资源,如 js 和 css Assets 触发相同的行为,要么我被重定向到登录,要么 Assets 根本没有交付(两者都没有选项:D)
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/register/**");
super.configure(web);
}
我的解决方案和解决方法
我禁用了以下配置,这解决了我所有的问题
// DISABLED
// http.sessionManagement().invalidSessionUrl("/login?logoutcause=sessiontimeout");
我的问题
这不是最好的方法,对吧? 什么是更好、更安全的方法。 请帮助我理解为什么在 spring 之前以这种方式完成,或者我配置错误的方式。
最佳答案
我通过添加以下配置解决了同样的问题:
.antMatchers("/login-invalid").permitAll()
关于java - Spring Security - 公共(public)页面重定向到使用无效 session ID 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61303023/