Search in sources :

Example 1 with OAuth2TokenCustomizer

use of org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer in project spring-authorization-server by spring-projects.

the class OAuth2ConfigurerUtils method getTokenGenerator.

@SuppressWarnings("unchecked")
static <B extends HttpSecurityBuilder<B>> OAuth2TokenGenerator<? extends OAuth2Token> getTokenGenerator(B builder) {
    OAuth2TokenGenerator<? extends OAuth2Token> tokenGenerator = builder.getSharedObject(OAuth2TokenGenerator.class);
    if (tokenGenerator == null) {
        tokenGenerator = getOptionalBean(builder, OAuth2TokenGenerator.class);
        if (tokenGenerator == null) {
            JwtGenerator jwtGenerator = getJwtGenerator(builder);
            OAuth2AccessTokenGenerator accessTokenGenerator = new OAuth2AccessTokenGenerator();
            OAuth2TokenCustomizer<OAuth2TokenClaimsContext> accessTokenCustomizer = getAccessTokenCustomizer(builder);
            if (accessTokenCustomizer != null) {
                accessTokenGenerator.setAccessTokenCustomizer(accessTokenCustomizer);
            }
            OAuth2RefreshTokenGenerator refreshTokenGenerator = new OAuth2RefreshTokenGenerator();
            if (jwtGenerator != null) {
                tokenGenerator = new DelegatingOAuth2TokenGenerator(jwtGenerator, accessTokenGenerator, refreshTokenGenerator);
            } else {
                tokenGenerator = new DelegatingOAuth2TokenGenerator(accessTokenGenerator, refreshTokenGenerator);
            }
        }
        builder.setSharedObject(OAuth2TokenGenerator.class, tokenGenerator);
    }
    return tokenGenerator;
}
Also used : JwtGenerator(org.springframework.security.oauth2.server.authorization.JwtGenerator) OAuth2RefreshTokenGenerator(org.springframework.security.oauth2.server.authorization.OAuth2RefreshTokenGenerator) OAuth2TokenGenerator(org.springframework.security.oauth2.server.authorization.OAuth2TokenGenerator) DelegatingOAuth2TokenGenerator(org.springframework.security.oauth2.server.authorization.DelegatingOAuth2TokenGenerator) OAuth2TokenClaimsContext(org.springframework.security.oauth2.server.authorization.OAuth2TokenClaimsContext) OAuth2AccessTokenGenerator(org.springframework.security.oauth2.server.authorization.OAuth2AccessTokenGenerator) DelegatingOAuth2TokenGenerator(org.springframework.security.oauth2.server.authorization.DelegatingOAuth2TokenGenerator)

Example 2 with OAuth2TokenCustomizer

use of org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer in project best-cloud by shanzhaozhen.

the class AuthorizationServerConfig method addCustomOAuth2ResourceOwnerPasswordAuthenticationProvider.

/**
 * 基于默认授权服务器设置中追加 password 模式
 * @param http
 */
private void addCustomOAuth2ResourceOwnerPasswordAuthenticationProvider(HttpSecurity http) {
    AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class);
    // 弃用JwtEncoder和关联的类以准备在0.3.0发布中删除(请参阅gh-594)。
    // 该0.3.0版本将使用Spring Security 5.6JwtEncoder中引入的。
    JwtEncoder jwtEncoder = OAuth2ConfigurerUtils.getJwtEncoder(http);
    OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer = OAuth2ConfigurerUtils.getJwtCustomizer(http);
    OAuth2ResourceOwnerPasswordAuthenticationProvider resourceOwnerPasswordAuthenticationProvider = new OAuth2ResourceOwnerPasswordAuthenticationProvider(authenticationManager, OAuth2ConfigurerUtils.getAuthorizationService(http), jwtEncoder);
    if (jwtCustomizer != null) {
        resourceOwnerPasswordAuthenticationProvider.setJwtCustomizer(jwtCustomizer);
    }
    http.authenticationProvider(resourceOwnerPasswordAuthenticationProvider);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) OAuth2ResourceOwnerPasswordAuthenticationProvider(org.shanzhaozhen.authorize.config.oauth2.authentication.OAuth2ResourceOwnerPasswordAuthenticationProvider) JwtEncoder(org.springframework.security.oauth2.jwt.JwtEncoder)

Example 3 with OAuth2TokenCustomizer

use of org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer in project spring-authorization-server by spring-projects.

the class OAuth2ConfigurerUtils method getJwtGenerator.

private static <B extends HttpSecurityBuilder<B>> JwtGenerator getJwtGenerator(B builder) {
    JwtGenerator jwtGenerator = builder.getSharedObject(JwtGenerator.class);
    if (jwtGenerator == null) {
        JwtEncoder jwtEncoder = getJwtEncoder(builder);
        if (jwtEncoder != null) {
            jwtGenerator = new JwtGenerator(jwtEncoder);
            OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer = getJwtCustomizer(builder);
            if (jwtCustomizer != null) {
                jwtGenerator.setJwtCustomizer(jwtCustomizer);
            }
            builder.setSharedObject(JwtGenerator.class, jwtGenerator);
        }
    }
    return jwtGenerator;
}
Also used : JwtGenerator(org.springframework.security.oauth2.server.authorization.JwtGenerator) JwtEncodingContext(org.springframework.security.oauth2.server.authorization.JwtEncodingContext) JwtEncoder(org.springframework.security.oauth2.jwt.JwtEncoder)

Aggregations

JwtEncoder (org.springframework.security.oauth2.jwt.JwtEncoder)2 JwtGenerator (org.springframework.security.oauth2.server.authorization.JwtGenerator)2 OAuth2ResourceOwnerPasswordAuthenticationProvider (org.shanzhaozhen.authorize.config.oauth2.authentication.OAuth2ResourceOwnerPasswordAuthenticationProvider)1 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)1 DelegatingOAuth2TokenGenerator (org.springframework.security.oauth2.server.authorization.DelegatingOAuth2TokenGenerator)1 JwtEncodingContext (org.springframework.security.oauth2.server.authorization.JwtEncodingContext)1 OAuth2AccessTokenGenerator (org.springframework.security.oauth2.server.authorization.OAuth2AccessTokenGenerator)1 OAuth2RefreshTokenGenerator (org.springframework.security.oauth2.server.authorization.OAuth2RefreshTokenGenerator)1 OAuth2TokenClaimsContext (org.springframework.security.oauth2.server.authorization.OAuth2TokenClaimsContext)1 OAuth2TokenGenerator (org.springframework.security.oauth2.server.authorization.OAuth2TokenGenerator)1