기존에 Springboot 1.5.19 로 개발했던 어플리케이션을 2.x로 변경해야하는 상황이 생겼다.

 

먼저 최대한 기존 소스를 수정을 안하는 쪽으로 하는 방향으로 결정.

Springboot 1.5.19 에서는 Oauth2 서버가 구동중인데....

2.2.x 버전이상으로 변경 할 경우에는 기존소스를 유지 할 수가 없었다.

 

2.2.x 버전이상에서는 기존 Oauth2 구현한것을 사용하지못하고

다시 만들어야 하는 상황이다. 다시 만들경우 이미 운영되고 있는 Oauth2 통신도

정상적으로 잘 될지도 모르는 상황...

 

다시 새로 구현해야하는 상황에서 열심히 찾아본결과...

2.1.4 기준에서는 Oauth2 서버를 유지하며 변경이 가능하였다.

 

먼저 Gradle.. 

  Gradle Version을 기존에 4.7 버전으로 사용하고 있었다.

  Springboot2에서는 Gradle 4이상을 사용하면 된다고 하지만..

  다른 블로그 및 검색 결과 5.0 이상을 추천 한다고 하여...  현재 Gradle 6.4.1로 세팅 하였다.

 

buildscript {
	ext {
		springBootVersion = '2.1.4.RELEASE'
	}
	repositories {
		mavenCentral()
		
		maven { url 'https://repo.spring.io/snapshot' }
		//If you are using a milestone or release candidate version
		//현재는 SNAPSHOT 버전이기 때문에 필요 없습니다.
		//maven { url 'https://repo.spring.io/milestone' }
	}
	dependencies {
		classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
	}
}

subprojects {
	apply plugin: 'java'
	apply plugin: 'idea'
	apply plugin: 'eclipse-wtp'
	apply plugin: 'org.springframework.boot'
	apply plugin: 'io.spring.dependency-management'
	
	sourceCompatibility = '8'

	repositories {
		mavenCentral()
	}
   
	dependencyManagement {
		imports {
			mavenBom "org.springframework.boot:spring-boot-starter-parent:${springBootVersion}"
		}
	}

	dependencies {
		annotationProcessor 'org.projectlombok:lombok:1.18.8'
		compileOnly 'org.projectlombok:lombok:1.18.8'

		implementation 'org.springframework.boot:spring-boot-devtools'
		implementation 'org.springframework.boot:spring-boot-starter-web'
		implementation 'org.springframework.boot:spring-boot-starter-security'
		implementation 'org.springframework.boot:spring-boot-starter-data-rest'
		implementation 'org.springframework.boot:spring-boot-starter-jdbc'
		implementation 'org.springframework.boot:spring-boot-starter-json'

		implementation 'org.springframework.cloud:spring-cloud-starter-security:2.1.4.RELEASE'
		implementation 'org.springframework.cloud:spring-cloud-starter-oauth2:2.1.4.RELEASE'
		implementation 'org.springframework.security:spring-security-jwt'		
		
		testCompile 'org.springframework.boot:spring-boot-starter-test'
		
		runtime 'org.springframework.boot:spring-boot-properties-migrator'
	}
}

   

 

 

@Configuration
@EnableAuthorizationServer
public class JwtOAuth2AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
	@Value("${oauth2.resource.id}")
	private String resourceId;

	@Autowired
	private AuthenticationManager authenticationManager;

	@Autowired
	DataSource dataSource;

	@Bean
	public JwtAccessTokenConverter accessTokenConverter() {
		JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
		converter.setSigningKey(secretKey);

		return converter;
	}

	@Override
	public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
		endpoints.tokenStore(tokenStore())
				.pathMapping("/oauth/token", "/api/oauth/token")
				.allowedTokenEndpointRequestMethods(HttpMethod.POST)
				.authenticationManager(authenticationManager)
				.accessTokenConverter(accessTokenConverter())
		;
}

 

 

@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled=true)
protected class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
	@Value("${oauth2.resource.id}")
	private String resourceId;

	@Override
	public void configure(ResourceServerSecurityConfigurer resources) {
		resources
			.resourceId(resourceId)
			.tokenServices(tokenServices())
			.tokenStore(tokenStore())
			.authenticationEntryPoint(new AuthExceptionEntryPoint())
			.accessDeniedHandler(new CustomAccessDeniedHandler())
		;
	}

	@Override
	public void configure(HttpSecurity http) throws Exception {
		http
			.authorizeRequests()
			.antMatchers("/api/**").authenticated()
			.antMatchers(
				"/resources/**"
				).permitAll()
			.requestMatchers(CorsUtils::isPreFlightRequest).permitAll()
			.anyRequest().hasRole("CLIENT").and().cors()
		;
	}
}

 

 

 

 


 

  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기