1 回答

TA贡献1815条经验 获得超13个赞
授权服务器
如果您想将此功能添加到您的授权服务器,并且您有一个自定义AbstractTokenGranter(请参阅下面的对话),那么您可以在该方法中捕获所需的异常getOAuth2Authentication。然后,您可以抛出一个自定义异常,用您需要的任何附加字段
扩展OAuth2Exception和填充地图。 有关如何执行此操作的示例,您可以查看spring-security-oauth 项目中的ResourceOwnerPasswordTokenGranter 。additionalInformation
客户
相反,如果您想将此功能添加到您的 OAuth2 客户端,那么您可以使用自定义AuthenticationFailureHandler来捕获LockedExceptionand DisabledException。
这是一个示例安全配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.oauth2Login()
.failureHandler(new CustomAuthenticationFailureHandler());
}
一个自定义的例子AuthenticationFailureHandler:
public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request,
HttpServletResponse response, AuthenticationException exception) {
if (exception instanceof DisabledException) {
// throw new exception or modify response
}
}
}
添加回答
举报