我正在使用WebSecurityConfigurerAdapter,我想知道是否有办法配置 WebSecurityConfigurerAdapter 中的错误处理程序就像以前在 web.xml 中一样 我想捕获所有 UsernameNotFoundException/401 并处理它。
示例:
<error-page>
<error-code>401</error-code>
<location>/SngErrorHandler</location>
</error-page>
<error-page>
<exception-type>org.springframework.security.core.userdetails.UsernameNotFoundException</exception-type>
<location>/SngErrorHandler</location>
</error-page>
最佳答案
根据AbstractAuthenticationFilterConfigurer
(对于Spring-Security 3.2.x)的javadoc,Spring security通常会自动为您重定向到“/login?error”
如果您想重定向到另一个页面并且正在使用登录表单例份验证,可以通过在 中添加
方法。http.formLogin().failureUrl("/SngErrorHandler")
来实现WebSecurityConfigurerAdapter
的 configure(HttpSecurity http)
如果您使用其他身份验证方法,则必须对此进行调整。
关于java - web.xml 到 WebSecurityConfigurerAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24433078/