我正在使用 Spring Security 5.0.4 和 Spring Boot 2.0.1
我有一个有效的安全配置:
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/", "/index.html", "/#", "/reader.html").permitAll()
.antMatchers("/favicon.ico").permitAll()
.antMatchers("/app/**", "/assets/**", "/translations/**", "/vendor/**").permitAll()
.antMatchers("/login**").permitAll()
.anyRequest().authenticated() // everything else needs authentication
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login")
.usernameParameter("username")
.passwordParameter("password")
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.permitAll();
它按预期工作:当我提供登录的良好凭据时,它会将我转发到 /index.html
。当我提供错误的凭据时,它会将我转发回带有 ?error
查询参数的 /login
。
但是,如果我使用自定义 failureForwardUrl()
或 successForwardUrl()
来完成它,例如:
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login")
.usernameParameter("username")
.passwordParameter("password")
.successForwardUrl("/home")
.failureForwardUrl("/login?fail=true")
.permitAll()
它的行为很奇怪。
如果我提供了错误的凭据,它会将我转发回 /login
,而不带任何查询参数。如果我提供了良好的凭据,则会收到 HTTP 405 Method Not Allowed
状态(POST
到 /login
)
此配置在 Spring Security 4.x 中有效。
可能是什么问题?谢谢!
最佳答案
我发现我混淆了 .failureUrl("/login?error")
和 .failureForwardUrl("/login?error")
。捂脸。
关于Spring Security 5自定义failureForwardUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49820547/