Spring OAuth 2 매번 생 성 되 는 access 해결token 과 refreshtoken 은 모두 새로운 문제 입 니 다.

4777 단어 SpringSecurityOAuth2
Spring OAuth 2 매번 생 성 되 는 access 해결token 과 refreshtoken 은 모두 새로운 문제 입 니 다.
문제 설명
저희 프로젝트 에 서 는 Spring OAuth 2 를 사 용 했 지만 Spring OAuth 2 를 이용 하여 access 를 생 성 하고 있 습 니 다.token 과 refreshtoken 을 전달 할 때마다 username 과 password 가 전 달 됩 니 다. 이에 대응 하 는 token 은 새로운 값 입 니 다. 만 료 시간 도 최신 기본 값 89400 초 입 니 다. 매번 에 token 을 다시 만 들 었 다 는 뜻 입 니 다.
이 유 는 기본 InMemory 를 사용 하여 생 성 된 token 을 메모리 에 저장 하고 지속 되 지 않 았 기 때 문 입 니 다.
해결 방법
생 성 된 token 을 redis 에 저장 하여 지속 시 키 면 됩 니 다.
package com.syc.cloud.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

@Configuration
@EnableAuthorizationServer
public class OauthServerConfig extends AuthorizationServerConfigurerAdapter {

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        //super.configure(clients);

        clients.inMemory()
                .withClient("oauth-client")
                .secret("123456")
                .scopes("server")
                .authorizedGrantTypes("password","refresh_token")
                .accessTokenValiditySeconds(24*3600);
    }

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter(){
        JwtAccessTokenConverter converter=new JwtAccessTokenConverter();
        ClassPathResource resource=new ClassPathResource("syc-jwt.jks");
        KeyStoreKeyFactory factory=new KeyStoreKeyFactory(resource,"syc123".toCharArray());
        converter.setKeyPair(factory.getKeyPair("syc-jwt"));
        return converter;
    }

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean
    public TokenStore tokenStore(){
        //return new JwtTokenStore(jwtAccessTokenConverter());
        // RedisToken  JwtToken,      access_token refresh_token      .
        return new RedisTokenStore(redisConnectionFactory);
    }

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        //super.configure(endpoints);
        endpoints.tokenStore(tokenStore())
        .tokenEnhancer(jwtAccessTokenConverter())
        //.tokenServices(tokenServices())
        .authenticationManager(authenticationManager);
        //       refresh token      ,true:reuse;false:no reuse
        //.reuseRefreshTokens(false);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.tokenKeyAccess("permitAll()");
        security.checkTokenAccess("isAuthenticated()");
        security.allowFormAuthenticationForClients();
        //  Encoded password does not look like BCrypt  
        //  springsecurity        ,                
        //https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-storage-updated
        security.passwordEncoder(NoOpPasswordEncoder.getInstance());
    }
}

좋은 웹페이지 즐겨찾기