토이 프로젝트 스터디 #1
토이 프로젝트 진행 사항
- 스프링 시큐리티와 jwt를 활용한 회원가입 & 토큰 기반 로그인 진행
내용
@EnableWebSecurity
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {
...
}
- 이전 프로젝트에서는
@EnableWebSecurity
사용
org.springframework.boot.autoconfigure.security.servlet.SpringBootWebSecurityConfiguration
으로 인해 스프링 시큐리티가 클래스패스에 있다면 자동으로 @EnableWebSecurity
추가
- 직접
@EnableWebSecurity
애노테이션을 추가해줄 필요 없음
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();
}
스터디 내용
소셜 로그인 redirect_url 404
- 팀원이 진행한 카카오 아이디로 로그인 로직 중
redirect_url
가 404
에러가 발생
spring:
security:
oauth2:
client:
registration:
kakao:
...
redirect-uri: http://localhost:8080/board/login/oauth2/code/kakao
...
...
redirect-uri: http://localhost:8080/oauth2/code/kakao
...