2013-06-26 26 views
16

私はOAuth 2.0を春にトークンを生成するために使用しています。expire_inを手動で設定したいので、トークンは自分の基準に従って期限切れになる可能性があります。誰か助けてくれますか?OAUTH 2.0でexpire_inを設定するにはどうすればよいですか?

これは私の応答です:

{ 
    access_token: "c7a6cb95-1506-40e7-87d1-ddef0a239f64" 
    token_type: "bearer" 
    expires_in: 43199 
    scope: "read" 
} 

答えて

-4
public interface OAuth2AccessToken { 

    public static String BEARER_TYPE = "Bearer"; 

    public static String OAUTH2_TYPE = "OAuth2"; 

    /** 
    * The access token issued by the authorization server. This value is REQUIRED. 
    */ 
    public static String ACCESS_TOKEN = "access_token"; 

    /** 
    * The type of the token issued as described in <a 
    * href="http://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-7.1">Section 7.1</a>. Value is case insensitive. 
    * This value is REQUIRED. 
    */ 
    public static String TOKEN_TYPE = "token_type"; 

    /** 
    * The lifetime in seconds of the access token. For example, the value "3600" denotes that the access token will 
    * expire in one hour from the time the response was generated. This value is OPTIONAL. 
    */ 
    public static String EXPIRES_IN = "expires_in"; 

    /** 
    * The refresh token which can be used to obtain new access tokens using the same authorization grant as described 
    * in <a href="http://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-6">Section 6</a>. This value is OPTIONAL. 
    */ 
    public static String REFRESH_TOKEN = "refresh_token"; 

    /** 
    * The scope of the access token as described by <a 
    * href="http://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-3.3">Section 3.3</a> 
    */ 
    public static String SCOPE = "scope"; 

    /** 
    * The additionalInformation map is used by the token serializers to export any fields used by extensions of OAuth. 
    * @return a map from the field name in the serialized token to the value to be exported. The default serializers 
    * make use of Jackson's automatic JSON mapping for Java objects (for the Token Endpoint flows) or implicitly call 
    * .toString() on the "value" object (for the implicit flow) as part of the serialization process. 
    */ 
    Map<String, Object> getAdditionalInformation(); 

    Set<String> getScope(); 

    OAuth2RefreshToken getRefreshToken(); 

    String getTokenType(); 

    boolean isExpired(); 

    Date getExpiration(); 

    int getExpiresIn(); 

    String getValue(); 

} 
10

は、あなたのOAuth設定は、あなたの豆TokenServicesを変更し、accessTokenValiditySecondsプロパティを設定する設定:

<bean id="tokenServices" 
    class="org.springframework.security.oauth2.provider.token.DefaultTokenServices"> 
    <property name="accessTokenValiditySeconds" value="1" /> 
    <property name="tokenStore" ref="tokenStore" /> 
    <property name="supportRefreshToken" value="true" /> 
    <property name="clientDetailsService" ref="clientDetails" /> 
</bean> 
1
  • AuthorizationCodeAccessTokenProviderのカスタムクラスを作成します。オーバーライドthこれはAccessTokenを返します

    DefaultOAuth2AccessToken token = super.obtainAccessToken(details, request); 
    
  • :電子親カスタムクラスのオーバーライドされたメソッドで

    public method obtainAccessToken(OAuth2ProtectedResourceDetails details, AccessTokenRequest request) 
    
  • は、その親クラスのプログラムロジック呼びかけます。 は今、あなたはちょうどそれがClientDetailsServiceConfigurerから入手ClientBuilderで設定することができ、過去 token.setExpiresIn(int timestamp)

16

からタイムスタンプを提供することで、直接そのトークンの有効期限が切れた値を操作する必要があります。

@Configuration 
@EnableAuthorizationServer 
public class OAuth2Config extends AuthorizationServerConfigurerAdapter { 

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients.inMemory() 
      .withClient("client") 
      .secret("secret") 
      .authorizedGrantTypes("authorization_code", "refresh_token", "password") 
      .scopes("app") 
      .accessTokenValiditySeconds(30); 
    } 

    // ... additional configuration 
} 

または直接DefaultTokenServicesに依存します。あなたはGrailsのセキュリティのOAuth2プロバイダを使用している場合

@Configuration 
@EnableAuthorizationServer 
public class OAuth2Config extends AuthorizationServerConfigurerAdapter { 
    @Override 
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 

     // optionally here you could just get endpoints.getConsumerTokenService() 
     // and cast to DefaultTokenServices and just set values needed 

     DefaultTokenServices tokenServices = new DefaultTokenServices(); 
     tokenServices.setTokenStore(endpoints.getTokenStore()); 
     tokenServices.setSupportRefreshToken(true); 
     tokenServices.setClientDetailsService(endpoints.getClientDetailsService()); 
     tokenServices.setTokenEnhancer(endpoints.getTokenEnhancer()); 
     tokenServices.setAccessTokenValiditySeconds(60); 

     endpoints.tokenServices(tokenServices);    
    } 
} 
0

あなただけのあなたもapplication.yaml fileDefaultTokenServicesを設定することができますGrailsのアプリ/ confに/春/ resources.groovy

import org.springframework.security.oauth2.provider.token.DefaultTokenServices 

// Place your Spring DSL code here  

beans = { 

    tokenServices(DefaultTokenServices){ 
    accessTokenValiditySeconds = 600; 
    tokenStore = ref('tokenStore') 
    supportRefreshToken = true; 
    clientDetailsService = ref('clientDetailsService') 
    } 

} 
4

を変更することができます。

security: 
    oauth2: 
    client: 
     clientId: client-id 
     clientSecret: client-secret 
     authorized-grant-types: authorization_code,refresh_token,password 
     scope: openid 
     access-token-validity-seconds: 30 
+1

security.oauth2.client.access-token-validity-seconds = 30 – alayor

関連する問題