org.springframework.security.oauth2.provider.token.AccessTokenConverter Java Examples

The following examples show how to use org.springframework.security.oauth2.provider.token.AccessTokenConverter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OAuth2CookieHelper.java    From cubeai with Apache License 2.0 6 votes vote down vote up
/**
 * Create a cookie out of the given refresh token.
 * Refresh token cookies contain the base64 encoded refresh token (a JWT token).
 * They also contain a hint whether the refresh token was for remember me or not.
 * If not, then the cookie will be prefixed by the timestamp it was created at followed by a pipe '|'.
 * This gives us the chance to expire session cookies regardless of the token duration.
 */
private Cookie createRefreshTokenCookie(OAuth2RefreshToken refreshToken, boolean rememberMe) {
    int maxAge = -1;
    String name = SESSION_TOKEN_COOKIE;
    String value = refreshToken.getValue();
    if (rememberMe) {
        name = REFRESH_TOKEN_COOKIE;
        //get expiration in seconds from the token's "exp" claim
        Integer exp = getClaim(refreshToken.getValue(), AccessTokenConverter.EXP, Integer.class);
        if (exp != null) {
            int now = (int) (System.currentTimeMillis() / 1000L);
            maxAge = exp - now;
            log.debug("refresh token valid for another {} secs", maxAge);
            //let cookie expire a bit earlier than the token to avoid race conditions
            maxAge -= REFRESH_TOKEN_EXPIRATION_WINDOW_SECS;
        }
    }
    Cookie refreshTokenCookie = new Cookie(name, value);
    refreshTokenCookie.setMaxAge(maxAge);
    return refreshTokenCookie;
}
 
Example #2
Source File: OAuth2CookieHelper.java    From tutorials with MIT License 6 votes vote down vote up
/**
 * Create a cookie out of the given refresh token.
 * Refresh token cookies contain the base64 encoded refresh token (a JWT token).
 * They also contain a hint whether the refresh token was for remember me or not.
 * If not, then the cookie will be prefixed by the timestamp it was created at followed by a pipe '|'.
 * This gives us the chance to expire session cookies regardless of the token duration.
 */
private Cookie createRefreshTokenCookie(OAuth2RefreshToken refreshToken, boolean rememberMe) {
    int maxAge = -1;
    String name = SESSION_TOKEN_COOKIE;
    String value = refreshToken.getValue();
    if (rememberMe) {
        name = REFRESH_TOKEN_COOKIE;
        //get expiration in seconds from the token's "exp" claim
        Integer exp = getClaim(refreshToken.getValue(), AccessTokenConverter.EXP, Integer.class);
        if (exp != null) {
            int now = (int) (System.currentTimeMillis() / 1000L);
            maxAge = exp - now;
            log.debug("refresh token valid for another {} secs", maxAge);
            //let cookie expire a bit earlier than the token to avoid race conditions
            maxAge -= REFRESH_TOKEN_EXPIRATION_WINDOW_SECS;
        }
    }
    Cookie refreshTokenCookie = new Cookie(name, value);
    refreshTokenCookie.setMaxAge(maxAge);
    return refreshTokenCookie;
}
 
Example #3
Source File: ApiBootAuthorizationServerAutoConfiguration.java    From beihu-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 配置jwt生成token的转换
 * 使用自定义Sign Key 进行加密
 *
 * @return Jwt Access Token转换实例
 */
@Bean
@ConditionalOnProperty(prefix = API_BOOT_OAUTH_PREFIX, name = "jwt.enable", havingValue = "true")
public AccessTokenConverter jwtAccessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    converter.setSigningKey(apiBootOauthProperties.getJwt().getSignKey());
    return converter;
}
 
Example #4
Source File: OpenUserConverter.java    From open-cloud with MIT License 5 votes vote down vote up
/**
 * 转换为自定义信息
 *
 * @param map
 * @return
 */
private Object converter(Map<String, ?> map) {
    Map<String, Object> params = new HashMap<String, Object>();
    for (String key : map.keySet()) {
        if (USERNAME.equals(key)) {
            if (map.get(key) instanceof Map) {
                params.putAll((Map) map.get(key));
            }
            else  if (map.get(key) instanceof OpenUserDetails) {
               return map.get(key);
            }else {
                params.put(key, map.get(key));
            }
        } else {
            params.put(key, map.get(key));
        }
    }
    OpenUserDetails auth = BeanConvertUtils.mapToObject(params, OpenUserDetails.class);
    if (params.get(USERNAME) != null) {
        auth.setUsername(params.get(USERNAME).toString());
    }
    if (params.get(OpenSecurityConstants.OPEN_ID) != null) {
        auth.setUserId(Long.parseLong(params.get(OpenSecurityConstants.OPEN_ID).toString()));
    }
    if (params.get(OpenSecurityConstants.DOMAIN) != null) {
        auth.setDomain(params.get(OpenSecurityConstants.DOMAIN).toString());
    }
    auth.setClientId(params.get(AccessTokenConverter.CLIENT_ID).toString());
    auth.setAuthorities(getAuthorities(map));
    return auth;
}
 
Example #5
Source File: AuthorizationServerConfiguration.java    From openapi-petstore with Apache License 2.0 5 votes vote down vote up
public AuthorizationServerConfiguration(BaseClientDetails details,
                                        AuthenticationConfiguration authenticationConfiguration,
                                        ObjectProvider<TokenStore> tokenStore,
                                        ObjectProvider<AccessTokenConverter> tokenConverter,
                                        AuthorizationServerProperties properties) throws Exception {
    super(details, authenticationConfiguration, tokenStore, tokenConverter, properties);
}
 
Example #6
Source File: OAuth2AuthorizationServerConfiguration.java    From spring-security-oauth2-boot with Apache License 2.0 5 votes vote down vote up
public AuthorizationSecurityConfigurer(BaseClientDetails details,
		AuthenticationConfiguration authenticationConfiguration, ObjectProvider<TokenStore> tokenStore,
		ObjectProvider<AccessTokenConverter> tokenConverter, AuthorizationServerProperties properties)
		throws Exception {

	this.details = details;
	this.authenticationManager = authenticationConfiguration.getAuthenticationManager();
	this.tokenStore = tokenStore.getIfAvailable();
	this.tokenConverter = tokenConverter.getIfAvailable();
	this.properties = properties;
}
 
Example #7
Source File: OAuth2ResourceServer.java    From OAuth-2.0-Cookbook with MIT License 4 votes vote down vote up
@Bean
public AccessTokenConverter accessTokenConverter() {
    DefaultAccessTokenConverter converter = new DefaultAccessTokenConverter();
    converter.setUserTokenConverter(userTokenConverter());
    return converter;
}
 
Example #8
Source File: OAuth2ResourceServer.java    From OAuth-2.0-Cookbook with MIT License 4 votes vote down vote up
@Bean
public AccessTokenConverter accessTokenConverter() {
    DefaultAccessTokenConverter converter = new DefaultAccessTokenConverter();
    converter.setUserTokenConverter(userTokenConverter());
    return converter;
}
 
Example #9
Source File: JweTokenEnhancer.java    From OAuth-2.0-Cookbook with MIT License 4 votes vote down vote up
public JweTokenEnhancer(AccessTokenConverter tokenConverter,
    JweTokenSerializer tokenSerializer) {
    this.tokenConverter = tokenConverter;
    this.tokenSerializer = tokenSerializer;
}
 
Example #10
Source File: JweTokenEnhancer.java    From OAuth-2.0-Cookbook with MIT License 4 votes vote down vote up
public JweTokenEnhancer(AccessTokenConverter tokenConverter,
    JweTokenSerializer tokenSerializer) {
    this.tokenConverter = tokenConverter;
    this.tokenSerializer = tokenSerializer;
}
 
Example #11
Source File: ResourceServerConfig.java    From multi-tenant-rest-api with MIT License 4 votes vote down vote up
@Bean
public AccessTokenConverter accessTokenConverter() {
	return new DefaultAccessTokenConverter();
}
 
Example #12
Source File: CustomRemoteTokenServices.java    From microservice-integration with MIT License 4 votes vote down vote up
public void setAccessTokenConverter(AccessTokenConverter accessTokenConverter) {
    this.tokenConverter = accessTokenConverter;
}
 
Example #13
Source File: FacebookTokenServices.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public void setAccessTokenConverter(final AccessTokenConverter accessTokenConverter) {
  tokenConverter = accessTokenConverter;
}
 
Example #14
Source File: ApiBootAuthorizationServerAutoConfiguration.java    From api-boot with Apache License 2.0 3 votes vote down vote up
/**
 * Configure jwt {@link AccessTokenConverter}
 * <p>
 * If the value of the configuration "api.boot.oauth.jwt.enable" is "true"
 * Use {@link JwtAccessTokenConverter}
 *
 * @return {@link JwtAccessTokenConverter} instance
 */
@Bean
@ConditionalOnProperty(prefix = API_BOOT_OAUTH_PREFIX, name = "jwt.enable", havingValue = "true")
public AccessTokenConverter jwtAccessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    converter.setSigningKey(apiBootOauthProperties.getJwt().getSignKey());
    return converter;
}
 
Example #15
Source File: ApiBootAuthorizationServerAutoConfiguration.java    From beihu-boot with Apache License 2.0 2 votes vote down vote up
/**
 * 默认token转换
 * 不配置jwt转换时
 *
 * @return AccessTokenConverter
 */
@Bean
@ConditionalOnProperty(prefix = API_BOOT_OAUTH_PREFIX, name = "jwt.enable", havingValue = "false", matchIfMissing = true)
public AccessTokenConverter defaultAccessTokenConverter() {
    return new DefaultAccessTokenConverter();
}
 
Example #16
Source File: ApiBootAuthorizationServerAutoConfiguration.java    From api-boot with Apache License 2.0 2 votes vote down vote up
/**
 * Configure default {@link AccessTokenConverter}
 * <p>
 * If the value of the configuration "api.boot.oauth.jwt.enable" is "false" or missing
 * Use {@link DefaultAccessTokenConverter}
 *
 * @return {@link DefaultAccessTokenConverter} instance
 */
@Bean
@ConditionalOnProperty(prefix = API_BOOT_OAUTH_PREFIX, name = "jwt.enable", havingValue = "false", matchIfMissing = true)
public AccessTokenConverter defaultAccessTokenConverter() {
    return new DefaultAccessTokenConverter();
}