类org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator源码实例Demo

下面列出了怎么用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;
        }
    };
}
 
源代码3 项目: Auth-service   文件: OAuth2Config.java
@Autowired
public OAuth2Config(AuthenticationManager authenticationManager, WebResponseExceptionTranslator webResponseExceptionTranslator,
                    HikariDataSource dataSource, RedisConnectionFactory redisConnectionFactory) {
    this.authenticationManager = authenticationManager;
    this.webResponseExceptionTranslator = webResponseExceptionTranslator;
    this.dataSource = dataSource;
    this.redisConnectionFactory = redisConnectionFactory;
}
 
源代码4 项目: pacbot   文件: ResourceServerConfig.java
/** Define your custom exception translator bean here */
@Bean
public WebResponseExceptionTranslator exceptionTranslator() {
    return new ApiErrorWebResponseExceptionTranslator();
}
 
源代码5 项目: lion   文件: AuthorizationServerConfig.java
@Bean
public WebResponseExceptionTranslator<OAuth2Exception> webResponseExceptionTranslator() {
    return new CustomWebResponseExceptionTranslator();
}
 
源代码6 项目: Auth-service   文件: ServiceConfig.java
@Bean
public WebResponseExceptionTranslator webResponseExceptionTranslator() {
    return new CustomWebResponseExceptionTranslator();
}
 
源代码7 项目: microservice-integration   文件: ServiceConfig.java
@Bean
public WebResponseExceptionTranslator webResponseExceptionTranslator() {
    return new CustomWebResponseExceptionTranslator();
}
 
源代码8 项目: spring-oauth-server   文件: OAuthRestController.java
protected WebResponseExceptionTranslator getExceptionTranslator() {
    return providerExceptionHandler;
}
 
源代码9 项目: SpringCloud   文件: AuthorizationServerConfig.java
/**
 * 自定义OAuth2异常处理
 *
 * @return CustomWebResponseExceptionTranslator
 */
@Bean
public WebResponseExceptionTranslator<OAuth2Exception> customExceptionTranslator() {
    return new CustomWebResponseExceptionTranslator();
}
 
 类方法
 同包方法