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

The following examples show how to use org.springframework.security.oauth2.provider.token.TokenEnhancer. 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: PcAuthorizationServerConfig.java    From paascloud-master with Apache License 2.0 6 votes vote down vote up
/**
 * Configure.
 *
 * @param endpoints the endpoints
 *
 * @throws Exception the exception
 */
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
	endpoints.tokenStore(tokenStore)
			.authenticationManager(authenticationManager)
			.userDetailsService(userDetailsService);

	if (jwtAccessTokenConverter != null && jwtTokenEnhancer != null) {
		TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
		List<TokenEnhancer> enhancers = new ArrayList<>();
		enhancers.add(jwtTokenEnhancer);
		enhancers.add(jwtAccessTokenConverter);
		enhancerChain.setTokenEnhancers(enhancers);
		endpoints.tokenEnhancer(enhancerChain).accessTokenConverter(jwtAccessTokenConverter);
	}
}
 
Example #2
Source File: AuthorizationServerConfigration.java    From Taroco with Apache License 2.0 6 votes vote down vote up
/**
 * jwt 生成token 定制化处理
 * <p>
 * 额外信息(这部分信息不关乎加密方式), 添加到随token一起的additionalInformation当中
 *
 * @return TokenEnhancer
 */
@Bean
public TokenEnhancer tokenEnhancer() {
    return (accessToken, authentication) -> {
        final Authentication userAuthentication = authentication.getUserAuthentication();
        if (userAuthentication == null) {
            return accessToken;
        }
        Map<String, Object> additionalInfo = new LinkedHashMap<>(accessToken.getAdditionalInformation());
        final Object principal = userAuthentication.getPrincipal();
        User user;
        if (principal instanceof User) {
            user = (User) principal;
        } else {
            final String username = (String) principal;
            user = (User) userNameUserDetailsService.loadUserByUsername(username);
        }
        additionalInfo.put(SecurityConstants.LICENSE_KEY, SecurityConstants.LICENSE);
        additionalInfo.put(SecurityConstants.USER_NAME_HEADER, user.getUsername());
        additionalInfo.put(SecurityConstants.USER_ID_HEADER, user.getUserId());
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
        return accessToken;
    };
}
 
Example #3
Source File: AuthorizationServerConfig.java    From black-shop with Apache License 2.0 6 votes vote down vote up
/**
 * token增强
 *
 * @return TokenEnhancer
 */
@Bean
public TokenEnhancer tokenEnhancer() {
	return (accessToken, authentication) -> {
		if ("client_credentials"
				.equals(authentication.getOAuth2Request().getGrantType())) {
			return accessToken;
		}

		final Map<String, Object> additionalInfo = new HashMap<>(1);
		SecurityUserDetail securityUserDetail = (SecurityUserDetail) authentication.getUserAuthentication().getPrincipal();
		additionalInfo.put("username", securityUserDetail.getUsername());
		((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
		return accessToken;
	};
}
 
Example #4
Source File: AuthorizationServerConfiguration.java    From fw-spring-cloud with Apache License 2.0 6 votes vote down vote up
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints.tokenStore(tokenStore)
            .authenticationManager(authenticationManager)
            .userDetailsService(userDetailsService);
    //扩展token返回结果
    if (jwtAccessTokenConverter != null && jwtTokenEnhancer != null) {
        TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
        List<TokenEnhancer> enhancerList = new ArrayList();
        enhancerList.add(jwtTokenEnhancer);
        enhancerList.add(jwtAccessTokenConverter);
        tokenEnhancerChain.setTokenEnhancers(enhancerList);
        //jwt
        endpoints.tokenEnhancer(tokenEnhancerChain)
                .accessTokenConverter(jwtAccessTokenConverter);
    }
}
 
Example #5
Source File: FwAuthorizationConfiguration.java    From fw-cloud-framework with MIT License 5 votes vote down vote up
/**
 * jwt 生成token 定制化处理
 * 
 * @return TokenEnhancer
 */
@Bean
public TokenEnhancer tokenEnhancer() {
	return (accessToken, authentication) -> {
		final Map<String, Object> additionalInfo = new HashMap<>(1);
		additionalInfo.put("license", SecurityConstant.LICENSE);
		((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
		return accessToken;
	};
}
 
Example #6
Source File: UaaConfiguration.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    //pick up all  TokenEnhancers incl. those defined in the application
    //this avoids changes to this class if an application wants to add its own to the chain
    Collection<TokenEnhancer> tokenEnhancers = applicationContext.getBeansOfType(TokenEnhancer.class).values();
    TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
    tokenEnhancerChain.setTokenEnhancers(new ArrayList<>(tokenEnhancers));
    endpoints
        .authenticationManager(authenticationManager)
        .tokenStore(tokenStore())
        .tokenEnhancer(tokenEnhancerChain)
        .reuseRefreshTokens(false);             //don't reuse or we will run into session inactivity timeouts
}
 
Example #7
Source File: AuthServerConfig.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
	super.configure(endpoints);
	endpoints.authenticationManager(authenticationManagerBean);
	endpoints.tokenStore(tokenStore());
	endpoints.tokenEnhancer(new TokenEnhancer() {

		@Override
		public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
			if (authentication.getPrincipal() instanceof User) {
				final User user = (User) authentication.getPrincipal();

				final Set<String> scopes = new HashSet<String>();
				for (GrantedAuthority authority : user.getAuthorities()) {
					final String role = authority.getAuthority();

					if (role.startsWith("ROLE_")) {
						scopes.add(role.substring(5).toLowerCase());
					}
					else {
						scopes.add(role.toLowerCase());
					}
				}
				((DefaultOAuth2AccessToken) accessToken).setScope(scopes);

			}
			return accessToken;
		}
	});
}
 
Example #8
Source File: AuthServerConfig.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
	super.configure(endpoints);
	endpoints.authenticationManager(authenticationManagerBean);
	endpoints.tokenStore(tokenStore());
	endpoints.tokenEnhancer(new TokenEnhancer() {

		@Override
		public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
			if (authentication.getPrincipal() instanceof User) {
				final User user = (User) authentication.getPrincipal();

				final Set<String> scopes = new HashSet<String>();
				for (GrantedAuthority authority : user.getAuthorities()) {
					final String role = authority.getAuthority();

					if (role.startsWith("ROLE_")) {
						scopes.add(role.substring(5).toLowerCase());
					}
					else {
						scopes.add(role.toLowerCase());
					}
				}
				((DefaultOAuth2AccessToken) accessToken).setScope(scopes);

			}
			return accessToken;
		}
	});
}
 
Example #9
Source File: AuthorizationServerConfig.java    From SpringAll with MIT License 5 votes vote down vote up
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
    TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
    List<TokenEnhancer> enhancers = new ArrayList<>();
    enhancers.add(tokenEnhancer);
    enhancers.add(jwtAccessTokenConverter);
    enhancerChain.setTokenEnhancers(enhancers);
    endpoints.authenticationManager(authenticationManager)
            .tokenStore(jwtTokenStore)
            .accessTokenConverter(jwtAccessTokenConverter)
            .userDetailsService(userDetailService);
}
 
Example #10
Source File: AuthorizationServerConfig.java    From smaker with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public TokenEnhancer tokenEnhancer() {
	return (accessToken, authentication) -> {
		final Map<String, Object> additionalInfo = new HashMap<>(1);
		additionalInfo.put("license", SecurityConstants.PROJECT_LICENSE);
		((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
		return accessToken;
	};
}
 
Example #11
Source File: PigAuthorizationConfig.java    From pig with MIT License 5 votes vote down vote up
/**
 * jwt 生成token 定制化处理
 *
 * @return TokenEnhancer
 */
@Bean
public TokenEnhancer tokenEnhancer() {
    return (accessToken, authentication) -> {
        final Map<String, Object> additionalInfo = new HashMap<>(2);
        additionalInfo.put("license", SecurityConstants.PIG_LICENSE);
        UserDetailsImpl user = (UserDetailsImpl) authentication.getUserAuthentication().getPrincipal();
        if (user != null) {
            additionalInfo.put("userId", user.getUserId());
        }
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
        return accessToken;
    };
}
 
Example #12
Source File: UaaConfiguration.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    //pick up all  TokenEnhancers incl. those defined in the application
    //this avoids changes to this class if an application wants to add its own to the chain
    Collection<TokenEnhancer> tokenEnhancers = applicationContext.getBeansOfType(TokenEnhancer.class).values();
    TokenEnhancerChain tokenEnhancerChain=new TokenEnhancerChain();
    tokenEnhancerChain.setTokenEnhancers(new ArrayList<>(tokenEnhancers));
    endpoints
        .authenticationManager(authenticationManager)
        .tokenStore(tokenStore())
        .tokenEnhancer(tokenEnhancerChain)
        .reuseRefreshTokens(false);             //don't reuse or we will run into session inactivity timeouts
}
 
Example #13
Source File: AuthJwtTokenStore.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
/**
 * jwt 生成token 定制化处理
 * 添加一些额外的用户信息到token里面
 *
 * @return TokenEnhancer
 */
@Bean
public TokenEnhancer tokenEnhancer() {
    return (accessToken, authentication) -> {
        final Map<String, Object> additionalInfo = new HashMap<>(1);
        Object principal = authentication.getPrincipal();
        //增加id参数
        if (principal instanceof SysUser) {
            SysUser user = (SysUser)principal;
            additionalInfo.put("id", user.getId());
        }
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
        return accessToken;
    };
}
 
Example #14
Source File: Oauth2AuthorizationServerConfig.java    From spring-security-oauth2-demo with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 令牌增强器
 *
 * @return TokenEnhancer
 */
@Bean
public TokenEnhancer tokenEnhancer() {
    return (accessToken, authentication) -> {
        Map<String, Object> additionalInfo = new HashMap<>(1);
        additionalInfo.put("organization", authentication.getName());
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
        return accessToken;
    };
}
 
Example #15
Source File: OAuth2Config.java    From spring-cloud-study with Apache License 2.0 5 votes vote down vote up
@Override
    public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        /**
         * 普通 jwt 模式
         */
//         endpoints.tokenStore(jwtTokenStore)
//                .accessTokenConverter(jwtAccessTokenConverter)
//                .userDetailsService(kiteUserDetailsService)
//                /**
//                 * 支持 password 模式
//                 */
//                .authenticationManager(authenticationManager);

        /**
         * jwt 增强模式
         */
        TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
        List<TokenEnhancer> enhancerList = new ArrayList<>();
        enhancerList.add(jwtTokenEnhancer);
        enhancerList.add(jwtAccessTokenConverter);
        enhancerChain.setTokenEnhancers(enhancerList);
        endpoints.tokenStore(jwtTokenStore)
                .userDetailsService(kiteUserDetailsService)
                /**
                 * 支持 password 模式
                 */
                .authenticationManager(authenticationManager)
                .tokenEnhancer(enhancerChain)
                .accessTokenConverter(jwtAccessTokenConverter);

        /**
         * redis token 方式
         */
//        endpoints.authenticationManager(authenticationManager)
//                .tokenStore(redisTokenStore)
//                .userDetailsService(kiteUserDetailsService);

    }
 
Example #16
Source File: OAuth2Config.java    From spring-cloud-study with Apache License 2.0 5 votes vote down vote up
@Override
    public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        /**
         * 普通 jwt 模式
         */
//         endpoints.tokenStore(jwtTokenStore)
//                .accessTokenConverter(jwtAccessTokenConverter)
//                .userDetailsService(kiteUserDetailsService)
//                /**
//                 * 支持 password 模式
//                 */
//                .authenticationManager(authenticationManager);

        /**
         * jwt 增强模式
         */
        TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
        List<TokenEnhancer> enhancerList = new ArrayList<>();
        enhancerList.add(jwtTokenEnhancer);
        enhancerList.add(jwtAccessTokenConverter);
        enhancerChain.setTokenEnhancers(enhancerList);
        endpoints.tokenStore(jwtTokenStore)
                .userDetailsService(kiteUserDetailsService)
                /**
                 * 支持 password 模式
                 */
                .authenticationManager(authenticationManager)
                .tokenEnhancer(enhancerChain)
                .accessTokenConverter(jwtAccessTokenConverter);

        /**
         * redis token 方式
         */
//        endpoints.authenticationManager(authenticationManager)
//                .tokenStore(redisTokenStore)
//                .userDetailsService(kiteUserDetailsService);

    }
 
Example #17
Source File: SophiaAuthorizationServerConfig.java    From sophia_scaffolding with Apache License 2.0 4 votes vote down vote up
/**
 * 注入自定义token生成方式(jwt)
 */
@Bean
public TokenEnhancer tokenEnhancer() {
    return new JwtTokenEnhancer();
}
 
Example #18
Source File: OAuth2AuthorizationServerConfig.java    From osiam with MIT License 4 votes vote down vote up
@Bean
public TokenEnhancer osiamTokenEnhancer() {
    return new OsiamTokenEnhancer();
}
 
Example #19
Source File: Oauth2AuthorizationServerApplication.java    From spring-oauth2-jwt-jdbc with MIT License 4 votes vote down vote up
/**
 * Enhances the json response for the /token endpoint with the openid id_token param.
 *
 */
@Bean
public TokenEnhancer tokenEnhancer() {
    return new OpenApiTokenEnhancer();
}
 
Example #20
Source File: OAuth2AuthorizationServerConfigJwt.java    From spring-security-oauth with MIT License 4 votes vote down vote up
@Bean
public TokenEnhancer tokenEnhancer() {
    return new CustomTokenEnhancer();
}
 
Example #21
Source File: OAuth2AuthorizationServerConfig.java    From spring-security-oauth with MIT License 4 votes vote down vote up
@Bean
public TokenEnhancer tokenEnhancer() {
    return new CustomTokenEnhancer();
}
 
Example #22
Source File: OAuth2AuthorizationServerConfigInMemory.java    From spring-security-oauth with MIT License 4 votes vote down vote up
@Bean
public TokenEnhancer tokenEnhancer() {
    return new CustomTokenEnhancer();
}
 
Example #23
Source File: OAuth2Config.java    From konker-platform with Apache License 2.0 4 votes vote down vote up
@Bean
public TokenEnhancer tokenEnhancer() {
	return new CustomTokenEnhancer();
}
 
Example #24
Source File: AuthServerOAuth2Config.java    From Building-Web-Apps-with-Spring-5-and-Angular with MIT License 4 votes vote down vote up
@Bean
public TokenEnhancer tokenEnhancer() {
    return new CustomTokenEnhancer();
}
 
Example #25
Source File: AuthServerOAuth2Config.java    From Building-Web-Apps-with-Spring-5-and-Angular with MIT License 4 votes vote down vote up
@Bean
public TokenEnhancer tokenEnhancer() {
    return new CustomTokenEnhancer();
}
 
Example #26
Source File: OAuth2AuthorizationServer.java    From OAuth-2.0-Cookbook with MIT License 4 votes vote down vote up
@Bean
public TokenEnhancer tokenEnhancer() {
    return new JweTokenEnhancer(accessTokenConverter(),
        new JweTokenSerializer(symmetricKey()));
}
 
Example #27
Source File: JwtTokenConfig.java    From spring-cloud-study with Apache License 2.0 4 votes vote down vote up
@Bean
public TokenEnhancer jwtTokenEnhancer(){
   return new JWTokenEnhancer();
}
 
Example #28
Source File: JWTokenConfig.java    From SpringAll with MIT License 4 votes vote down vote up
@Bean
public TokenEnhancer tokenEnhancer() {
    return new JWTokenEnhancer();
}
 
Example #29
Source File: JwtTokenConfig.java    From spring-cloud-study with Apache License 2.0 4 votes vote down vote up
@Bean
public TokenEnhancer jwtTokenEnhancer(){
   return new JWTokenEnhancer();
}
 
Example #30
Source File: TokenStoreConfig.java    From paascloud-master with Apache License 2.0 4 votes vote down vote up
/**
 * Jwt token enhancer token enhancer.
 *
 * @return the token enhancer
 */
@Bean
@ConditionalOnBean(TokenEnhancer.class)
public TokenEnhancer jwtTokenEnhancer() {
	return new TokenJwtEnhancer();
}