Spring OAuth 2 매번 생 성 되 는 access 해결token 과 refreshtoken 은 모두 새로운 문제 입 니 다.
4777 단어 SpringSecurityOAuth2
문제 설명
저희 프로젝트 에 서 는 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());
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[Spring] Spring Security - JWT강력한 사용자 인증 및 Access 제어 framework이다. 이는 Java 애플리케이션에 인증 및 권한 부여를 제공하는데 중점을 두었으며 다양한 필터를 사용하여 커스터마이징이 가능하다. '인증(Authentica...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.