【问题标题】:Spring Oauth2 Modify response on Invalid Token ExceptionSpring Oauth2修改无效令牌异常的响应
【发布时间】:2018-01-11 06:56:04
【问题描述】:

我的应用程序使用带有 JWTToken 的 Spring Security Oauth2。

当它返回错误 401 时,我正在尝试修改响应的正文,当未经身份验证的用户尝试访问数据时会发生这种情况:

{
    "error": "invalid_token",
    "error_description": "Cannot convert access token to JSON"
}

为此,我实现了自己的WebResponseExceptionTranslator,并将其设置在我的身份验证入口点中,如下所示:

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.authenticationEntryPoint(authenticationEntryPoint());
    }

    @Bean
    public AuthenticationEntryPoint authenticationEntryPoint () {
        OAuth2AuthenticationEntryPoint authenticationEntryPoint = new OAuth2AuthenticationEntryPoint();
        authenticationEntryPoint.setExceptionTranslator(new CustomWebResponseExceptionTranslator());
        return authenticationEntryPoint;
    }
}

但是,它继续使用默认的WebResponseExceptionTranslator。所以,我的问题是,我怎样才能强迫他使用我的?

【问题讨论】:

    标签: java spring-security spring-oauth2


    【解决方案1】:

    如果你使用WebResponseExceptionTranslator,你应该像这样把它放在AuthorizationServerEndpointsConfigurer中:

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
        enhancerChain.setTokenEnhancers(Arrays.asList(accessTokenConverter));
        endpoints.tokenStore(tokenStore)
                .accessTokenConverter(accessTokenConverter)
                .tokenEnhancer(enhancerChain)
                .authenticationManager(authenticationManager)
                .userDetailsService(userDetailsService)
                .exceptionTranslator(webResponseExceptionTranslator());
    

    【讨论】:

      猜你喜欢
      • 2019-03-13
      • 2019-01-20
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 2011-03-07
      • 2012-06-08
      • 2015-11-04
      • 2019-02-18
      相关资源
      最近更新 更多