spring使用Java配置配置自定义AuthenticationFailureHandler
如何使用基于Java的配置在Spring Security中配置自定义AuthenticationFailureHandler
?我已经有了一个SecurityConfig
类,它扩展了WebSecurityConfigurerAdapter
,并使用httpBasic()配置了HTTP基本身份验证,但我不知道如何设置AuthenticationFailureHandler
我真正的目标是重定向到一个外部URL(登录页面),而不是只为某些请求返回401响应(获取对某些URL的请求),所以如果有其他或更好的方法,我想知道
# 1 楼答案
您必须在failureHandler方法中传递它。。链可以如下所示:
spring提供的类似乎可以帮助您实现简单的重定向:
http://docs.spring.io/autorepo/docs/spring-security/4.0.0.M2/apidocs/org/springframework/security/web/authentication/SimpleUrlAuthenticationFailureHandler.html
希望能有帮助
# 2 楼答案
我使用了freakman关于创建自定义BasicAuthenticationEntryPoint类的建议。以下是我的代码,以防其他人需要执行类似操作: