작성
·
1.8K
0
- 학습 관련 질문을 남겨주세요. 상세히 작성하면 더 좋아요!
- 먼저 유사한 질문이 있었는지 검색해보세요.
- 서로 예의를 지키며 존중하는 문화를 만들어가요.
- 잠깐! 인프런 서비스 운영 관련 문의는 1:1 문의하기를 이용해주세요.
먼저, 유익한 강의를 나눠 주셔서 감사합니다.
springboot 3.1버전을 사용 하면서 안되는 부분에 대해서 질문드립니다.
강의 간에 진행되는 springboot 버전을 2.7 버전으로 설정 하셨는데, end of support가 11월로 예정이 되어 있더라구요,,
현재 섹션 9. OAuth 2.0 Client - Social Login (Google, Naver, KaKao) + FormLogin
파트까지 3.1 버전으로 마이그레이션 진행 하려 했으나, config의 메서드가 Deprecated
된 부분을 해결 해야했습니다.
공식 문서를 참고해서 일단 제가 바꾼 부분은 다음과 같습니다.
authorizeRequests -> authorizeHttpRequests
antMatchers -> requestMatchers
http.formLogin(), http.logout() -> 람다 사용
import io.security.oauth2.springsecurityoauth2.service.CustomOAuth2UserService;
import io.security.oauth2.springsecurityoauth2.service.CustomOidcUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
import org.springframework.security.web.SecurityFilterChain;
@RequiredArgsConstructor
@EnableWebSecurity
public class OAuth2ClientConfig {
private final CustomOAuth2UserService customOAuth2UserService;
private final CustomOidcUserService customOidcUserService;
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring().requestMatchers("/static/js/**", "/static/images/**", "/static/css/**", "/static/scss/**");
}
@Bean
SecurityFilterChain oauth2SecurityFilterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests((requests) -> requests
.requestMatchers("/api/user")
.hasAnyRole("SCOPE_profile','SCOPE_email")
// .access("hasAuthority('SCOPE_profile')")
.requestMatchers("/api/oidc")
.hasRole("SCOPE_openid")
//.access("hasAuthority('SCOPE_openid')")
.requestMatchers("/")
.permitAll()
.anyRequest().authenticated());
http.oauth2Login(oauth2 -> oauth2.userInfoEndpoint(
userInfoEndpointConfig -> userInfoEndpointConfig
.userService(customOAuth2UserService)
.oidcUserService(customOidcUserService)));
http.logout(form -> form.logoutSuccessUrl("/"));
return http.build();
}
/*@Bean // hasAuthority 일경우 정의하지 않는다
public GrantedAuthoritiesMapper grantedAuthoritiesMapper(){
SimpleAuthorityMapper simpleAuthorityMapper = new SimpleAuthorityMapper();
simpleAuthorityMapper.setPrefix("ROLE_");
return simpleAuthorityMapper;
}*/
@Bean
public GrantedAuthoritiesMapper customAuthorityMapper() {
return new CustomAuthorityMapper();
}
}
이렇게 수정을 하니 메인 페이지("/")가 무조건 로그인 페이지("/login)로 리다이렉트 되는 문제(?)를 발견 했습니다.
이 부분에 대해서 어떻게 해결해야할지 여쭤보고 싶습니다.
답변 2
1
1
답변 해주셔서 감사합니다.
oauth2Login-SocialLogin 브랜치에서 OAuth2ClientConfig 클래스와 build.gradle 클래스만 다음과 같이 수정했습니다.
소스 코드는 https://github.com/onjsdnjs/spring-security-oauth2/tree/oauth2Login-SocialLogin 위와 같습니다.
build.gradle
plugins {
id 'org.springframework.boot' version '3.1.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'io.security.oauth2'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}
tasks.named('test') {
useJUnitPlatform()
}
OAuth2ClientConfig
import io.security.oauth2.springsecurityoauth2.service.CustomOAuth2UserService;
import io.security.oauth2.springsecurityoauth2.service.CustomOidcUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
import org.springframework.security.web.SecurityFilterChain;
@RequiredArgsConstructor
@EnableWebSecurity
public class OAuth2ClientConfig {
private final CustomOAuth2UserService customOAuth2UserService;
private final CustomOidcUserService customOidcUserService;
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring().requestMatchers("/static/js/**", "/static/images/**", "/static/css/**", "/static/scss/**");
}
@Bean
SecurityFilterChain oauth2SecurityFilterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests((requests) -> requests
.requestMatchers("/api/user")
.hasAnyRole("SCOPE_profile','SCOPE_email")
// .access("hasAuthority('SCOPE_profile')")
.requestMatchers("/api/oidc")
.hasRole("SCOPE_openid")
//.access("hasAuthority('SCOPE_openid')")
.requestMatchers("/")
.permitAll()
.anyRequest().authenticated());
http.oauth2Login(oauth2 -> oauth2.userInfoEndpoint(
userInfoEndpointConfig -> userInfoEndpointConfig
.userService(customOAuth2UserService)
.oidcUserService(customOidcUserService)));
http.logout(form -> form.logoutSuccessUrl("/"));
return http.build();
}
/*@Bean // hasAuthority 일경우 정의하지 않는다
public GrantedAuthoritiesMapper grantedAuthoritiesMapper(){
SimpleAuthorityMapper simpleAuthorityMapper = new SimpleAuthorityMapper();
simpleAuthorityMapper.setPrefix("ROLE_");
return simpleAuthorityMapper;
}*/
@Bean
public GrantedAuthoritiesMapper customAuthorityMapper() {
return new CustomAuthorityMapper();
}
}
네 박민욱님께서 작성하신 코드 전체를 공유해 주시면 좋겠습니다.