Spring Boot

[OAuth] OAuth2 ์„ค์ • ํŒŒ์ผ ์ž‘์„ฑ

์šฐ์ฃผ๋ฌผ๊ณ ๊ธฐ 2025. 5. 11. 23:43
๋ฐ˜์‘ํ˜•

 

OAuth2๋ฅผ ๊ธฐ์กด JWT์™€ ํ•จ๊ป˜ ์“ฐ๋ ค๋ฉด

๊ธฐ์กด์— ์ž‘์„ฑํ•ด๋‘” ์„ค์ •์ด ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ์„ค์ •์„ ์จ์•ผํ•จ.

 

 

1. ๊ธฐ์กด ํผ ๋กœ๊ทธ์ธ ๋ฐฉ์‹์— ์ป๋˜ WebSecurityConfig.java ํŒŒ์ผ ์ „๋ถ€ ์ฃผ์„์ฒ˜๋ฆฌ~~~~~

 

 

 

2 . config ํŒจํ‚ค์ง€์— WebOauthSecurityConfig.java ์ƒ์„ฑ

@RequiredArgsConstructor
@Configuration
public class WebOAuthSecurityConfig {

    private final OAuth2UserCustomService oAuth2UserCustomService;
    private final TokenProvider tokenProvider;
    private final RefreshTokenService refreshTokenService;
    private final UserService userService;

    @Bean
    public WebSecurityCustomizer configure() {  // ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ ๊ธฐ๋Šฅ ๋น„ํ™œ์„ฑํ™”
        return (web) -> web.ignoring()
                .requestMatchers(
                        new AntPathRequestMatcher("/img/**"),
                        new AntPathRequestMatcher("/css/**"),
                        new AntPathRequestMatcher("/jss/**")
                        );
    }

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

        // ํ† ํฐ ๋ฐฉ์‹์œผ๋กœ ์ธ์ฆ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ธฐ์กด์— ์‚ฌ์šฉํ•˜๋˜ ํผ ๋กœ๊ทธ์ธ, ์„ธ์…˜ ๋น„ํ™œ์„ฑํ™”
        return http
                .csrf(AbstractHttpConfigurer::disable)
                .httpBasic(AbstractHttpConfigurer::disable)
                .formLogin(AbstractHttpConfigurer::disable)
                .logout(AbstractHttpConfigurer::disable)
                .sessionManagement(management -> management.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                .addFilterBefore(tokenAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
                .authorizeRequests(auth -> auth
                        .requestMatchers(new AntPathRequestMatcher("/api/token")).permitAll()
                        .requestMatchers(new AntPathRequestMatcher("/api/**")).authenticated().anyRequest().permitAll())
                .oauth2Login(oauth2 -> oauth2.loginPage("/login")
                        .authorizationEndpoint(authorizationEndpoint -> authorizationEndpoint.authorizationRequestRepository(oAuth2AuthorizationRequestBasedOnCookieRepository()))
                        .userInfoEndpoint(userInfoEndpoint -> userInfoEndpoint.userService(oAuth2UserCustomService))
                        .successHandler(oAuth2SuccessHandler())
                )
                .exceptionHandling(exceptionHandling -> exceptionHandling
                        .defaultAuthenticationEntryPointFor(
                                new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED),
                                new AntPathRequestMatcher("/api/**")
                        ))
                .build();
    }

    @Bean
    public OAuth2SuccessHandler oAuth2SuccessHandler() {
        return new Oauth2SuccessHandler(
                tokenProvider,
                refreshTokenRepository,
                oAuth2AuthorizationRequestBasedOnCookieRepository(),
                userService
        );
    }

    @Bean
    public TokenAuthenticationFilter tokenAuthenticationFilter() {
        return new TokenAuthenticationFilter(tokenProvider);
    }

    @Bean
    public OAuth2AuthorizationRequestBasedOnCookieRepository oAuth2AuthorizationRequestBasedOnCookieRepository() {
        return new OAuth2AuthorizationRequestBasedOnCookieRepository();
    }

    @Bean
    public BCryptPasswordEncoder bCryptpasswordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

 

 

 

 

 

๊ฐ‘์ž๊ธฐ ๋ฐฉ๋Œ€ํ•ด์ง„ ์–‘์— ๋‘ํ†ต์ด ์˜ค๋Š” ์ค‘

ํ•˜๋ฃจ ๋‚ ์žก๊ณ  ์ด๋ถ€๋ถ„ ๋‹ค ๊ณต๋ถ€ํ•ด์•ผํ• ๋“ฏ

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

๋ฐ˜์‘ํ˜•