[英]Spring Oauth2 Modify response on Invalid Token Exception
我的應用程序使用帶有JWTToken的Spring Security Oauth2。
我正在嘗試修改響應的正文,當它返回錯誤401時,該錯誤會在未經身份驗證的用戶嘗試訪問數據時發生:
{
"error": "invalid_token",
"error_description": "Cannot convert access token to JSON"
}
為此,我實現了自己的WebResponseExceptionTranslator
並在Authentification Entry Point中將其設置為:
@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
。 所以,我的問題是,我怎么能強迫他使用我的?
如果使用了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());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.