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

The following examples show how to use org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter. 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: AuthorizationServerConfig.java    From cloud-service with MIT License 7 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
Source File: BaseResourceServerConfigurerAdapter.java    From smaker with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 不获取用户详细 只有用户名
 *
 * @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 #3
Source File: SecurityConfig.java    From codenjoy with 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 #4
Source File: ResourceServerConfiguration.java    From spring-cloud-contract-samples with Apache License 2.0 5 votes vote down vote up
/**
 * <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 #5
Source File: OauthAuthorizationServerConfig.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 配置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 #6
Source File: OpenHelper.java    From open-cloud with 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 #7
Source File: OpenHelper.java    From open-cloud with 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 #8
Source File: OpenHelper.java    From open-cloud with 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 #9
Source File: CustomJwtAccessTokenConverter.java    From spring-security with Apache License 2.0 5 votes vote down vote up
@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 File: CustomJwtAccessTokenConverter.java    From spring-security with Apache License 2.0 5 votes vote down vote up
@Override
public OAuth2Authentication extractAuthentication(Map<String, ?> map) {
    DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
    UserAuthenticationConverter userAuthenticationConverter = new CustomUserAuthenticationConverter();
    accessTokenConverter.setUserTokenConverter(userAuthenticationConverter);
    return accessTokenConverter.extractAuthentication(map);

}
 
Example #11
Source File: AuthJwtTokenStore.java    From microservices-platform with 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 #12
Source File: ResJwtTokenStore.java    From microservices-platform with 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 #13
Source File: FebsAuthorizationServerConfigure.java    From FEBS-Cloud with Apache License 2.0 5 votes vote down vote up
@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 #14
Source File: BaseResourceServerConfigurerAdapter.java    From smaker with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 上下文中获取用户全部信息,两次调用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 #15
Source File: OauthAuthorizationServerConfig.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 配置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 #16
Source File: OpenJwtTokenService.java    From open-cloud with MIT License 4 votes vote down vote up
public DefaultAccessTokenConverter getDefaultAccessTokenConverter() {
	return defaultAccessTokenConverter;
}
 
Example #17
Source File: OpenJwtTokenService.java    From open-cloud with MIT License 4 votes vote down vote up
public void setDefaultAccessTokenConverter(DefaultAccessTokenConverter defaultAccessTokenConverter) {
	this.defaultAccessTokenConverter = defaultAccessTokenConverter;
}
 
Example #18
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 #19
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 #20
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 #21
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();
}
 
Example #22
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();
}