org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator Java Examples

The following examples show how to use org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomAuthorizationServerConfigurer.java    From spring-microservice-exam with MIT License 7 votes vote down vote up
@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;
            }
        }
    };
}
 
Example #2
Source File: SecurityHandlerConfig.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
@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;
        }
    };
}