2016-10-13 10 views
0

を呼び出さない春のセキュリティ顧客トークンエンハンサーは

カスタムトークンエンハンサー

public class CustomTokenEnhancer implements TokenEnhancer { 
    @Override 
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) { 
     final Map<String, Object> additionalInfo = new HashMap<>(); 
     additionalInfo.put("organization", authentication.getName() + randomAlphabetic(4)); 
     ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo); 
     return accessToken; 
    } 

} 

以下

@Configuration 
@EnableAuthorizationServer 
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter { 

    @Autowired 
    private DataSource dataSource; 

    @Autowired 
    private UserApprovalHandler userApprovalHandler; 

    @Autowired 
    @Qualifier("authenticationManagerBean") 
    private AuthenticationManager authenticationManager; 

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients.jdbc(dataSource).withClient("abcd").secret("secret") 
       .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit") 
       .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT").scopes("read", "write", "trust") 
       .accessTokenValiditySeconds(60 * 60 * 24 * 1) 
       .refreshTokenValiditySeconds(60 * 60 * 24 * 30); 
    } 

    @Override 
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
     TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain(); 
     tokenEnhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhancer(), accessTokenConverter())); 
     endpoints.tokenStore(tokenStore()).tokenEnhancer(tokenEnhancerChain).userApprovalHandler(userApprovalHandler) 
       .authenticationManager(authenticationManager); 
    } 

    @Override 
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { 
     oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()").realm(REALM); 
    } 

    @Bean 
    public TokenStore tokenStore() { 
     return new JdbcTokenStore(dataSource); 
    } 

    @Bean 
    public TokenEnhancer tokenEnhancer() { 
     return new CustomTokenEnhancer(); 
    } 

    @Bean 
    public JwtAccessTokenConverter accessTokenConverter() { 
     JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); 
     converter.setSigningKey("123"); 
     return converter; 
    } 

} 

以下に示すように私はJavaの構成を使用してカスタムトークンエンハンサーを追加している私は、デバッグ上でアプリケーションを実行しています、 CustomTokenEnhancerのエンハンスメソッドでデバッグポイントを持っていました。今私はトークンを生成するためのoauth/tokenメソッドをヒットし、それはエンハンスメントメソッドには入っていません。

何か不足している場合は、お勧めします。

答えて

1

トークンエンハンサーをどこにでも割り当てられません。あなたのトークンエンハンサーが参加すべきその後

@Override 
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
    endpoints 
     // some code here 
     .tokenEnhancer(tokenEnhancer()); 
} 

@Bean 
@Primary 
public AuthorizationServerTokenServices tokenServices() { 
    DefaultTokenServices tokenServices = new DefaultTokenServices(); 
    // some code here as well 
    tokenServices.setTokenEnhancer(tokenEnhancer()); 
    return tokenServices; 
} 

// Beans beans beans 

@Bean 
public TokenEnhancer tokenEnhancer() { 
    return new CustomTokenEnhancer(); 
} 

:私が覚えている限りでは、あなたがこのようなものが必要。

+0

私はbeanのtokenServices()を追加しましたが、それでも同じです。 – rohit

関連する問題