在网上找了不少关于UsernameNotFoundException捕获的帖子,捕获成功后每次用户名错误会在控制台打印大段的异常信息,用统一异常处理也无法处理。
配置捕获UsernameNotFoundException,在 WebSecurityConfigurer 配置 provider.setHideUserNotFoundExceptions(false)
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setHideUserNotFoundExceptions(false);
provider.setUserDetailsService(userDetailsService);
provider.setPasswordEncoder(passwordEncoder);
return provider;
}
然后在 configure(AuthenticationManagerBuilder auth) 方法中 auth.authenticationProvider(authenticationProvider())
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider());
}
这样就可以捕获到UsernameNotFoundException了,网上有很多大神解析的博文,这里就不复述了。下面主要讲UsernameNotFoundException在控制台打印大段异常信息如何处理的问题。
查看源码发现 TokenEndpoit 中处理了 Exception 的代码,会通过logger.error打印异常信息。
@ExceptionHandler({HttpRequestMethodNotSupportedException.class})
public ResponseEntity<OAuth2Exception> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) throws Exception {
if (this.logger.isInfoEnabled()) {
this.logger.info("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage());
}
return this.getExceptionTranslator().translate(e);
}
@ExceptionHandler({Exception.class})
public ResponseEntity<OAuth2Exception> handleException(Exception e) throws Exception {
if (this.logger.isErrorEnabled()) {
this.logger.error("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
}
return this.getExceptionTranslator().translate(e);
}
@ExceptionHandler({ClientRegistrationException.class})
public ResponseEntity<OAuth2Exception> handleClientRegistrationException(Exception e) throws Exception {
if (this.logger.isWarnEnabled()) {
this.logger.warn("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage());
}
return this.getExceptionTranslator().translate(new BadClientCredentialsException());
}
@ExceptionHandler({OAuth2Exception.class})
public ResponseEntity<OAuth2Exception> handleException(OAuth2Exception e) throws Exception {
if (this.logger.isWarnEnabled()) {
this.logger.warn("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage());
}
return this.getExceptionTranslator().translate(e);
}
UsernameNotFoundException 的父类是 AuthenticationException 在 TokenEndpoit 中没有单独处理,会被集中使用 handleException(Exception e) 方式处理,该方法使用logger.error(String,Exception) 把异常打印出来。
大段的异常信息对日志文件处理很不友好,需要在异常打印前把异常处理了。查看源码没有暴露方法处理这块,最后想到一个方案:下载源码在TokenEndpoit 中添加对AuthenticationException的处理重新编译jar包替换现有官方jar包。
spring-security-oauth的开源代码在:https://github.com/spring-projects/spring-security-oauth,我当前使用的版本是2.3.8.RELEASE 版 clone 2.3.x分支,编译的时候发现官方还build一个2.3.9的SNAPSHOT版本没有发布,在这个版本中官方在ResourceOwnerPasswordTokenGranter 类中加入了对UsernameNotFoundException异常的处理。
@Override
protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {
Map<String, String> parameters = new LinkedHashMap<String, String>(tokenRequest.getRequestParameters());
String username = parameters.get("username");
String password = parameters.get("password");
// Protect from downstream leaks of password
parameters.remove("password");
Authentication userAuth = new UsernamePasswordAuthenticationToken(username, password);
((AbstractAuthenticationToken) userAuth).setDetails(parameters);
try {
userAuth = authenticationManager.authenticate(userAuth);
} catch (AccountStatusException ase) {
//covers expired, locked, disabled cases (mentioned in section 5.2, draft 31)
throw new InvalidGrantException(ase.getMessage());
} catch (BadCredentialsException e) {
// If the username/password are wrong the spec says we should send 400/invalid grant
throw new InvalidGrantException(e.getMessage());
} catch (UsernameNotFoundException e) {
// If the user is not found, report a generic error message
throw new InvalidGrantException(e.getMessage());
}
if (userAuth == null || !userAuth.isAuthenticated()) {
throw new InvalidGrantException("Could not authenticate user: " + username);
}
OAuth2Request storedOAuth2Request = getRequestFactory().createOAuth2Request(client, tokenRequest);
return new OAuth2Authentication(storedOAuth2Request, userAuth);
}
在 try catch 中 catch UsernameNotFoundException 然后 throw 了另一个 InvalidGrantException异常这个异常继承与 OAuth2Exception 在 TokenEndpoit 对 OAuth2Exception 处理是打印一条警告信息,不打印异常日志。
这样就不用我前面想的那个方案了,直接编译官方源码 用2.3.9-SNAPSHOT 版本替换现有jar包就可以了!