Java Code Examples for org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter

The following examples show how to use org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter. These examples are extracted from open source projects. 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 Project: cloud-service   Source File: AuthorizationServerConfig.java    License: MIT License 6 votes vote down vote up
/**
 * Jwt资源令牌转换器<br>
 * 参数access_token.store-jwt为true时用到
 *
 * @return accessTokenConverter
 */
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
    JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter() {
        @Override
        public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
            OAuth2AccessToken oAuth2AccessToken = super.enhance(accessToken, authentication);
            addLoginUserInfo(oAuth2AccessToken, authentication); // 2019.07.13 将当前用户信息追加到登陆后返回数据里
            return oAuth2AccessToken;
        }
    };
    DefaultAccessTokenConverter defaultAccessTokenConverter = (DefaultAccessTokenConverter) jwtAccessTokenConverter
            .getAccessTokenConverter();
    DefaultUserAuthenticationConverter userAuthenticationConverter = new DefaultUserAuthenticationConverter();
    userAuthenticationConverter.setUserDetailsService(userDetailsService);

    defaultAccessTokenConverter.setUserTokenConverter(userAuthenticationConverter);
    // 2019.06.29 这里务必设置一个,否则多台认证中心的话,一旦使用jwt方式,access_token将解析错误
    jwtAccessTokenConverter.setSigningKey(signingKey);

    return jwtAccessTokenConverter;
}
 
Example 2
/**
 * 配置AccessToken加密方式
 */
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter(CustomUserAuthenticationConverter customUserAuthenticationConverter) {
	JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
	KeyPair keyPair = new KeyStoreKeyFactory(
			keyProperties.getKeyStore().getLocation(),
			keyProperties.getKeyStore().getSecret().toCharArray()).getKeyPair(
			keyProperties.getKeyStore().getAlias(),
			keyProperties.getKeyStore().getPassword().toCharArray());
	converter.setKeyPair(keyPair);
	//配置自定义的CustomUserAuthenticationConverter
	DefaultAccessTokenConverter accessTokenConverter = (DefaultAccessTokenConverter) converter.getAccessTokenConverter();
	accessTokenConverter.setUserTokenConverter(customUserAuthenticationConverter);
	return converter;
}
 
Example 3
/**
 * 不获取用户详细 只有用户名
 *
 * @param resources
 */
protected void notGetUser(ResourceServerSecurityConfigurer resources) {
	DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
	DefaultUserAuthenticationConverter userTokenConverter = new DefaultUserAuthenticationConverter();
	accessTokenConverter.setUserTokenConverter(userTokenConverter);

	remoteTokenServices.setRestTemplate(lbRestTemplate());
	remoteTokenServices.setAccessTokenConverter(accessTokenConverter);
	resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
		.accessDeniedHandler(AccessDeniedHandler)
		.tokenServices(remoteTokenServices);
}
 
Example 4
/**
 * 上下文中获取用户全部信息,两次调用userDetailsService,影响性能
 *
 * @param resources
 */
private void canGetUser(ResourceServerSecurityConfigurer resources) {
	DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
	DefaultUserAuthenticationConverter userTokenConverter = new DefaultUserAuthenticationConverter();
	userTokenConverter.setUserDetailsService(userDetailsService);
	accessTokenConverter.setUserTokenConverter(userTokenConverter);

	remoteTokenServices.setRestTemplate(lbRestTemplate());
	remoteTokenServices.setAccessTokenConverter(accessTokenConverter);
	resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
		.accessDeniedHandler(AccessDeniedHandler)
		.tokenServices(remoteTokenServices);
}
 
Example 5
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
    JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
    DefaultAccessTokenConverter defaultAccessTokenConverter = (DefaultAccessTokenConverter) accessTokenConverter.getAccessTokenConverter();
    DefaultUserAuthenticationConverter userAuthenticationConverter = new DefaultUserAuthenticationConverter();
    userAuthenticationConverter.setUserDetailsService(userDetailService);
    defaultAccessTokenConverter.setUserTokenConverter(userAuthenticationConverter);
    accessTokenConverter.setSigningKey(properties.getJwtAccessKey());
    return accessTokenConverter;
}
 
Example 6
Source Project: microservices-platform   Source File: ResJwtTokenStore.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    converter.setVerifierKey(getPubKey());
    DefaultAccessTokenConverter tokenConverter = (DefaultAccessTokenConverter)converter.getAccessTokenConverter();
    tokenConverter.setUserTokenConverter(new CustomUserAuthenticationConverter());
    return converter;
}
 
Example 7
Source Project: microservices-platform   Source File: AuthJwtTokenStore.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
    final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    KeyPair keyPair = new KeyStoreKeyFactory
            (keyProperties.getKeyStore().getLocation(), keyProperties.getKeyStore().getSecret().toCharArray())
            .getKeyPair(keyProperties.getKeyStore().getAlias());
    converter.setKeyPair(keyPair);
    DefaultAccessTokenConverter tokenConverter = (DefaultAccessTokenConverter)converter.getAccessTokenConverter();
    tokenConverter.setUserTokenConverter(new CustomUserAuthenticationConverter());
    return converter;
}
 
Example 8
@Override
public OAuth2Authentication extractAuthentication(Map<String, ?> map) {
    DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
    UserAuthenticationConverter userAuthenticationConverter = new CustomUserAuthenticationConverter();
    accessTokenConverter.setUserTokenConverter(userAuthenticationConverter);
    return accessTokenConverter.extractAuthentication(map);

}
 
Example 9
@Override
public OAuth2Authentication extractAuthentication(Map<String, ?> map) {
    DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
    UserAuthenticationConverter userAuthenticationConverter = new CustomUserAuthenticationConverter();
    accessTokenConverter.setUserTokenConverter(userAuthenticationConverter);
    return accessTokenConverter.extractAuthentication(map);

}
 
Example 10
Source Project: open-cloud   Source File: OpenHelper.java    License: MIT License 5 votes vote down vote up
/**
 * 构建token转换器
 *
 * @return
 */
public static DefaultAccessTokenConverter buildAccessTokenConverter() {
    OpenUserConverter userAuthenticationConverter = new OpenUserConverter();
    DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
    accessTokenConverter.setUserTokenConverter(userAuthenticationConverter);
    return accessTokenConverter;
}
 
Example 11
Source Project: open-cloud   Source File: OpenHelper.java    License: MIT License 5 votes vote down vote up
/**
 * 构建自定义远程Token服务类
 *
 * @param properties
 * @return
 */
public static RemoteTokenServices buildRemoteTokenServices(OpenCommonProperties properties) {
    // 使用自定义系统用户凭证转换器
    DefaultAccessTokenConverter accessTokenConverter = buildAccessTokenConverter();
    RemoteTokenServices tokenServices = new RemoteTokenServices();
    tokenServices.setCheckTokenEndpointUrl(properties.getTokenInfoUri());
    tokenServices.setClientId(properties.getClientId());
    tokenServices.setClientSecret(properties.getClientSecret());
    tokenServices.setAccessTokenConverter(accessTokenConverter);
    log.info("buildRemoteTokenServices[{}]", tokenServices);
    return tokenServices;
}
 
Example 12
Source Project: open-cloud   Source File: OpenHelper.java    License: MIT License 5 votes vote down vote up
/**
 * 构建资源服务器JwtToken服务类
 *
 * @param properties
 * @return
 */
public static ResourceServerTokenServices buildJwtTokenServices(OpenCommonProperties properties) throws Exception {
    // 使用自定义系统用户凭证转换器
    DefaultAccessTokenConverter accessTokenConverter = buildAccessTokenConverter();
    OpenJwtTokenService tokenServices = new OpenJwtTokenService();
    // 这里的签名key 保持和认证中心一致
    JwtAccessTokenConverter converter = buildJwtTokenEnhancer(properties);
    JwtTokenStore jwtTokenStore = new JwtTokenStore(converter);
    tokenServices.setTokenStore(jwtTokenStore);
    tokenServices.setJwtAccessTokenConverter(converter);
    tokenServices.setDefaultAccessTokenConverter(accessTokenConverter);
    log.info("buildJwtTokenServices[{}]", tokenServices);
    return tokenServices;
}
 
Example 13
/**
 * 配置AccessToken加密方式
 */
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter(CustomUserAuthenticationConverter customUserAuthenticationConverter) {
	JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
	KeyPair keyPair = new KeyStoreKeyFactory(
			keyProperties.getKeyStore().getLocation(),
			keyProperties.getKeyStore().getSecret().toCharArray()).getKeyPair(
			keyProperties.getKeyStore().getAlias(),
			keyProperties.getKeyStore().getPassword().toCharArray());
	converter.setKeyPair(keyPair);
	//配置自定义的CustomUserAuthenticationConverter
	DefaultAccessTokenConverter accessTokenConverter = (DefaultAccessTokenConverter) converter.getAccessTokenConverter();
	accessTokenConverter.setUserTokenConverter(customUserAuthenticationConverter);
	return converter;
}
 
Example 14
/**
 * <p>
 * Configures jwt related access token converter to allow enhanced user details to be
 * converted.
 * </p>
 * @param jwtAccessTokenConverter the converter to configure
 */
@Autowired
public void configure(JwtAccessTokenConverter jwtAccessTokenConverter) {
	notNull(jwtAccessTokenConverter, "jwtAccessTokenConverter");
	DefaultAccessTokenConverter defaultAccessTokenConverter = (DefaultAccessTokenConverter) jwtAccessTokenConverter
			.getAccessTokenConverter();
	defaultAccessTokenConverter
			.setUserTokenConverter(new UserAuthenticationConverter());
}
 
Example 15
Source Project: codenjoy   Source File: SecurityConfig.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Bean
public TokenStore tokenStore() {
    JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
    DefaultAccessTokenConverter tokenConverter = new DefaultAccessTokenConverter();
    tokenConverter.setUserTokenConverter(oAuth2UserAuthenticationConverter);
    accessTokenConverter.setAccessTokenConverter(tokenConverter);

    return new JwkTokenStore(clientProperties.getProvider().get("dojo").getJwkSetUri(),
            accessTokenConverter);
}
 
Example 16
Source Project: open-cloud   Source File: OpenJwtTokenService.java    License: MIT License 4 votes vote down vote up
public DefaultAccessTokenConverter getDefaultAccessTokenConverter() {
	return defaultAccessTokenConverter;
}
 
Example 17
Source Project: open-cloud   Source File: OpenJwtTokenService.java    License: MIT License 4 votes vote down vote up
public void setDefaultAccessTokenConverter(DefaultAccessTokenConverter defaultAccessTokenConverter) {
	this.defaultAccessTokenConverter = defaultAccessTokenConverter;
}
 
Example 18
Source Project: OAuth-2.0-Cookbook   Source File: OAuth2ResourceServer.java    License: MIT License 4 votes vote down vote up
@Bean
public AccessTokenConverter accessTokenConverter() {
    DefaultAccessTokenConverter converter = new DefaultAccessTokenConverter();
    converter.setUserTokenConverter(userTokenConverter());
    return converter;
}
 
Example 19
Source Project: OAuth-2.0-Cookbook   Source File: OAuth2ResourceServer.java    License: MIT License 4 votes vote down vote up
@Bean
public AccessTokenConverter accessTokenConverter() {
    DefaultAccessTokenConverter converter = new DefaultAccessTokenConverter();
    converter.setUserTokenConverter(userTokenConverter());
    return converter;
}
 
Example 20
Source Project: multi-tenant-rest-api   Source File: ResourceServerConfig.java    License: MIT License 4 votes vote down vote up
@Bean
public AccessTokenConverter accessTokenConverter() {
	return new DefaultAccessTokenConverter();
}
 
Example 21
/**
 * 默认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 22
/**
 * 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();
}