gpt4 book ai didi

java - 捕获并增强LockedException和DisabledException

转载 作者:行者123 更新时间:2023-12-02 09:45:00 28 4
gpt4 key购买 nike

我将 Spring Security 与 OAuth 2 结合使用。我的 OAuth 用户具有 account_lockedenabled 字段,我出于不同的原因使用它们。例如。 account_locked 用于阻止用户,enabled 用于停用用户。当用户尝试登录并被阻止时,他会收到 401 HTTP 代码和消息“用户帐户已锁定”。如果他被停用,那么他还会收到 401 HTTP 代码和消息“用户已禁用”。

我想通过附加信息来增强这些错误(例如可以使用 TokenEnhancer 来增强 token ),以区分客户端上的 block 代码和停用代码。例如。与字段reason_of_fail。请注意,我不想使用这些错误中的短信。

我已经尝试使用这个(来自 this answer ):

@Override
protected void configure(HttpSecurity http) throws Exception {
http.exceptionHandling()
.authencationEntryPoint(unauthorizedHandler)
.accessDeniedHandler(accessDeniedHandler);
}

但是这些处理程序不会捕获 LockedExceptionDisabledException

那么,我如何通过附加字段(键值对)来增强这些错误?

最佳答案

授权服务器

如果您想将此功能添加到您的授权服务器,并且您有一个自定义 AbstractTokenGranter(请参阅下面的对话),然后您可以在 getOAuth2Authentication 方法中捕获所需的异常。
然后,您可以抛出一个自定义异常,该异常扩展 OAuth2Exception 并使用您需要的任何其他字段填充 additionalInformation 映射。
有关如何执行此操作的示例,您可以查看 ResourceOwnerPasswordTokenGranter来自 spring-security-oauth 项目。

客户端

如果您想将此功能添加到 OAuth2 客户端,则可以使用自定义 AuthenticationFailureHandler 来捕获 LockedExceptionDisabledException
以下是安全配置示例:

@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
}
}
}

关于java - 捕获并增强LockedException和DisabledException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56730581/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com