2014-07-25 2 views
3

스프링 보안 및 Oauth 2.0 인증 프로토콜을 사용하여 REST 서비스를 보안하고 있습니다.스프링 보안 OAuth 2 : oauth/토큰 요청 후 액세스 토큰 및 추가 데이터를 얻는 방법

MVC Spring 애플리케이션을 구현했으며 정상적으로 작동합니다.

<http pattern="/oauth/token" create-session="stateless" 
    authentication-manager-ref="clientAuthenticationManager" 
    xmlns="http://www.springframework.org/schema/security" > 
    <intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY" /> 
    <anonymous enabled="false" /> 
    <http-basic entry-point-ref="clientAuthenticationEntryPoint" /> 
    <custom-filter ref="clientCredentialsTokenEndpointFilter" before="BASIC_AUTH_FILTER" /> 
    <access-denied-handler ref="oauthAccessDeniedHandler" /> 
</http> 

자격 증명이있는 경우 : 클라이언트는 클라이언트 자격 증명 (CLIENT_ID & client_secret) & 사용자 자격 증명 (사용자 이름 & 비밀번호) 서블릿-config.xml 파일에 정의 된 호출 OUTH/토큰 서비스를 제공하여 AccessToken에 대한 서버 요청 유효한 클라이언트는 다음과 같이 응답 액세스 토큰을 얻을 것이다 :

{ 
    "value": "b663f10d-553d-445b-afde-e9cd84066a1c", 
    "expiration": 1406598295994, 
    "tokenType": "bearer", 
    "refreshToken": { 
     "value": "36737abf-24bd-4b86-ad22-601f4d5cdee4", 
     "expiration": 1408890295994 
    }, 
    "scope": [], 
    "additionalInformation": {}, 
    "expiresIn": 299999, 
    "expired": false 
} 

나는 또한이 같은 사용자 정보가 포함 된 응답이 싶습니다

{ 
     "value": "b663f10d-553d-445b-afde-e9cd84066a1c", 
     "expiration": 1406598295994, 
     "tokenType": "bearer", 
     "refreshToken": { 
      "value": "36737abf-24bd-4b86-ad22-601f4d5cdee4", 
      "expiration": 1408890295994 
     }, 

     "additionalInformation": {}, 
     "expiresIn": 299999, 
     "expired": false, 

     "USER_ID": "1", 
     "USER_ROLE": "admin", 
     "OTHER DATA..." 
    } 

누구든지이 기능을 구현하는 방법을 알고 있습니까?

저는 꽤 많이 인터넷 검색을 해왔지만 유사한 시나리오를 구현하는 예제를 찾지 못했습니다. 질문이 어리석은 일은 유감 스럽지만 Spring Security에서는 매우 새로운 것입니다.

답변

1

이렇게하면 시나리오와 비슷한 방식으로 구현됩니다.

1) 사용자 토큰을 증강 만들기 : 구성

public class CustomTokenEnhancer implements TokenEnhancer { 

    @Override 
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) { 

     final Map<String, Object> additionalInfo = new HashMap<>(); 

     User user = (User) authentication.getPrincipal(); 

     additionalInfo.put("user_id", user.getId()); 
     additionalInfo.put("business_id", user.getBusinessId()); 

     ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo); 

     return accessToken; 
    } 

} 

2) 사용자 정의 토큰 인핸서.

@Override 
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
    endpoints 
     .tokenStore(tokenStore()) 
     .tokenEnhancer(tokenEnhancerChain()) 
     .authenticationManager(authenticationManager) 
     .accessTokenConverter(jwtAccessTokenConverter()); 
} 

/** 
* Creates a chain of the list of token enhancers. 
* @return 
* 
* @since 0.0.1 
* 
* @author Anil Bharadia 
*/ 
public TokenEnhancerChain tokenEnhancerChain() { 
    TokenEnhancerChain chain = new TokenEnhancerChain(); 
    chain.setTokenEnhancers(Arrays.asList(tokenEnhancer(), jwtAccessTokenConverter())); 
    return chain; 
} 

/** 
* Get the new instance of the token enhancer. 
* @return 
*/ 
@Bean 
public TokenEnhancer tokenEnhancer() { 
    return new CustomTokenEnhancer(); 
} 

그게 전부입니다. 다음 번에 토큰을 요청하면 추가 정보가 표시됩니다.

관련 문제