토이 프로젝트 스터디 #1

appti·2022년 5월 28일
0

토이 프로젝트 스터디 #1

  • 스터디 진행 날짜 : 5/28

토이 프로젝트 진행 사항

  • 스프링 시큐리티와 jwt를 활용한 회원가입 & 토큰 기반 로그인 진행

내용

@EnableWebSecurity

@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    ...
}
  • 이전 프로젝트에서는 @EnableWebSecurity 사용

  • org.springframework.boot.autoconfigure.security.servlet.SpringBootWebSecurityConfiguration으로 인해 스프링 시큐리티가 클래스패스에 있다면 자동으로 @EnableWebSecurity 추가
    • 직접 @EnableWebSecurity 애노테이션을 추가해줄 필요 없음

WebSecurityConfigurerAdapter

  • Spring 5.0.0부터 WebSecurityConfigurerAdapter deprecated
    • 자바 8이 기본 버전이 되면서 interface default method를 활용하면 어댑터를 사용할 필요가 없기 때문이라고 함

@Configuration
@RequiredArgsConstructor
public class SecurityConfig {

    private final CustomUserDetailsService userDetailsService;

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .httpBasic().disable()
                .formLogin().disable()
                .csrf().disable()
                .sessionManagement((session) ->
                        session
                                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                )
                .authorizeHttpRequests((authz) ->
                        authz
                                .anyRequest().authenticated()
                )
                .exceptionHandling((handler) ->
                        handler
                                .accessDeniedHandler(new CustomAccessDeniedHandler())
                                .authenticationEntryPoint(new CustomAuthenticationEntryPoint())
                )
                .addFilterBefore(
                        new CustomAuthenticationFilter(userDetailsService),
                        UsernamePasswordAuthenticationFilter.class
                );

        return http.build();
    }
}
  • 스프링 빈으로 SecurityFilterChain을 직접 등록
  • Lambda DSL을 통해 체인 패턴을 위한 and() 없이 체이닝 진행

리소스 설정

@Override
public void configure(WebSecurity web) {
    web.ignoring()
            .requestMatchers(PathRequest.toStaticResources().atCommonLocations());
}
  • 기존 방식
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
    return (web) -> web.ignoring().requestMatchers(PathRequest.toStaticResources().atCommonLocations());
}
  • 기존 방식에서 WebSecurityCustomizer를 직접 등록
  • ignore 사용 시 WARN 로그 발생
@Bean
@Order(0)
public SecurityFilterChain resource(HttpSecurity http) throws Exception {
    http
            .requestMatchers((matchers) ->
                    matchers
                            .requestMatchers(PathRequest.toStaticResources().atCommonLocations())
            )
            .authorizeRequests((authz) ->
                    authz
                            .anyRequest().permitAll()
            )
            .requestCache(RequestCacheConfigurer::disable)
            .securityContext(AbstractHttpConfigurer::disable)
            .sessionManagement(AbstractHttpConfigurer::disable);

    return http.build();
}
  • Spring Security issue 10938에서 WARN 로그가 발생하는 원인을 파악하고, 제시한 방식대로 두 번째 SecurityFilterChain을 스프링 빈으로 등록하는 방식으로 수정

스터디 내용

소셜 로그인 redirect_url 404

  • 팀원이 진행한 카카오 아이디로 로그인 로직 중 redirect_url404 에러가 발생

spring:
  security:
    oauth2:
      client:
        registration:
          kakao:
            ...
            redirect-uri: http://localhost:8080/board/login/oauth2/code/kakao
            ...
  • 원인은 redirect-uri

  • Spring Boot and OAuth2에서 확인할 수 있듯이 redirect uri는 템플릿이 정해져있고, 그 템플릿을 지키지 않아 발생한 문제
...
redirect-uri: http://localhost:8080/oauth2/code/kakao
...
  • 템플릿에 맞춰 수정해 해결
profile
안녕하세요

0개의 댓글