ProviderManager 在 DaoAuthenticationProvider.class 中检索用户时抛出 InternalAuthenticationServiceException.class,
loadedUser = this.getUserDetailsService().loadUserByUsername(username);
我想处理这个异常并将我的自定义响应返回给客户端。
我不想通过编写自定义 ProviderManager 来处理这个问题。
对于所有其他 OAuth 异常,我可以使用 Custom WebResponseExceptionTranslator 处理异常。
但我无法捕获像InternalAuthenticationServiceException.class这样的安全异常。
我无法选择将 ErrorController 与 /error 路径一起使用,它会破坏其他流程。
您可以编写一个带有注释的类@ControllerAdvice
并有一个@ExceptionHandler(value=InternalAuthenticationServiceException.class)
.
Ex:-
@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;
}
}
UPDATE
如果您没有控制器并使用@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.
}
});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)