下面列出了怎么用org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator的API类实例代码及写法,或者点击链接到github查看源代码。
@Bean
@Lazy
public WebResponseExceptionTranslator<OAuth2Exception> webResponseExceptionTranslator() {
return new DefaultWebResponseExceptionTranslator() {
@Override
public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
if (e instanceof OAuth2Exception) {
OAuth2Exception exception = (OAuth2Exception) e;
// 转换返回结果
return ResponseEntity.status(exception.getHttpErrorCode()).body(new CustomOauthException(e.getMessage()));
} else {
throw e;
}
}
};
}
@Bean
public WebResponseExceptionTranslator webResponseExceptionTranslator() {
return new DefaultWebResponseExceptionTranslator() {
public static final String BAD_MSG = "坏的凭证";
@Override
public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
OAuth2Exception oAuth2Exception;
if (e.getMessage() != null && e.getMessage().equals(BAD_MSG)) {
oAuth2Exception = new InvalidGrantException("用户名或密码错误", e);
} else if (e instanceof InternalAuthenticationServiceException) {
oAuth2Exception = new InvalidGrantException(e.getMessage(), e);
} else if (e instanceof RedirectMismatchException) {
oAuth2Exception = new InvalidGrantException(e.getMessage(), e);
} else if (e instanceof InvalidScopeException) {
oAuth2Exception = new InvalidGrantException(e.getMessage(), e);
} else {
oAuth2Exception = new UnsupportedResponseTypeException("服务内部错误", e);
}
ResponseEntity<OAuth2Exception> response = super.translate(oAuth2Exception);
ResponseEntity.status(oAuth2Exception.getHttpErrorCode());
response.getBody().addAdditionalInformation("resp_code", oAuth2Exception.getHttpErrorCode() + "");
response.getBody().addAdditionalInformation("resp_msg", oAuth2Exception.getMessage());
return response;
}
};
}
@Autowired
public OAuth2Config(AuthenticationManager authenticationManager, WebResponseExceptionTranslator webResponseExceptionTranslator,
HikariDataSource dataSource, RedisConnectionFactory redisConnectionFactory) {
this.authenticationManager = authenticationManager;
this.webResponseExceptionTranslator = webResponseExceptionTranslator;
this.dataSource = dataSource;
this.redisConnectionFactory = redisConnectionFactory;
}
/** Define your custom exception translator bean here */
@Bean
public WebResponseExceptionTranslator exceptionTranslator() {
return new ApiErrorWebResponseExceptionTranslator();
}
@Bean
public WebResponseExceptionTranslator<OAuth2Exception> webResponseExceptionTranslator() {
return new CustomWebResponseExceptionTranslator();
}
@Bean
public WebResponseExceptionTranslator webResponseExceptionTranslator() {
return new CustomWebResponseExceptionTranslator();
}
@Bean
public WebResponseExceptionTranslator webResponseExceptionTranslator() {
return new CustomWebResponseExceptionTranslator();
}
protected WebResponseExceptionTranslator getExceptionTranslator() {
return providerExceptionHandler;
}
/**
* 自定义OAuth2异常处理
*
* @return CustomWebResponseExceptionTranslator
*/
@Bean
public WebResponseExceptionTranslator<OAuth2Exception> customExceptionTranslator() {
return new CustomWebResponseExceptionTranslator();
}