Har*_*ish 5 java spring spring-security spring-boot
ProviderManager 在 DaoAuthenticationProvider.class 中检索用户时抛出 InternalAuthenticationServiceException.class,
loadedUser = this.getUserDetailsService().loadUserByUsername(username);
Run Code Online (Sandbox Code Playgroud)
我想处理这个异常并将我的自定义响应返回给客户端。
我不想通过编写自定义 ProviderManager 来处理这个问题。
对于所有其他 OAuth 异常,我可以使用 Custom WebResponseExceptionTranslator 处理异常。
但我无法捕获像InternalAuthenticationServiceException.class这样的安全异常。
我无法选择将 ErrorController 与 /error 路径一起使用,它会破坏其他流程。
您可以编写一个带有注释@ControllerAdvice并具有@ExceptionHandler(value=InternalAuthenticationServiceException.class).
前任:-
@ControllerAdvice
public class ExceptionHandler {
@ExceptionHandler(InternalAuthenticationServiceException.class)
public ResponseEntity<String> handleInternalAuthenticationServiceException(InternalAuthenticationServiceException e) {
ResponseEntity<String> response = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
return response;
}
}
Run Code Online (Sandbox Code Playgroud)
更新
如果您没有控制器并没有使用,@EnableAuthorizationServer那么您需要扩展AuthorizationServerConfigurerAdapter并重写,configure(AuthorizationServerEndpointsConfigurer endpoints)如下所示。您可以使用AuthorizationServerEndpointsConfigurer.exceptionTranslator来处理您的InternalAuthenticationServiceException.
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
// other endpoints
.exceptionTranslator(e -> {
if (e instanceof InternalAuthenticationServiceException) {
InternalAuthenticationServiceException internalAuthenticationServiceException = (InternalAuthenticationServiceException) e;
// return a ResponseEntity or throw a custom Exception.
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6169 次 |
| 最近记录: |