작은 프로젝트를 개발하다가 인증 부분을 작성하는 중
SecurityFilterChain에서 .authorizeRequests() 설정에 대하여 메모하면 좋을 거 같아서 작성함.
- permitAll() : 인증 없이 모두 접근 허용
- authenticated() : 로그인(인증)한 사용자만 접근 허용
- hasRole("ROLE_USER") : 특정 권한(역할)이 있는 사용자만 접근 허용
1. anyRequest()에 .authenticated() 설정
SecurityFilterChain 작성할 때,
.authorizeRequests() 에서 .anyRequest()를 .authenticated()로 설정하면
Spring Security에 의해 로그인 창이 뜬다.
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable())
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeRequests(auth -> auth
.requestMatchers("/").permitAll()
.anyRequest().authenticated() // .authenticated() 로 설정
);
return http.build();
}
2. anyRequest()에 .permitAll() 설정
이번에는 .permitAll()로 설정하니 로그인창은 뜨지 않는다.
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable())
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeRequests(auth -> auth
.requestMatchers("/").permitAll()
.anyRequest().permitAll() //permitAll()로 설정
)
;
return http.build();
}
메모
- 회원가입 기능을 구현해야했고, 아직 권한을 설정할 필요는 없을 거 같아서 permitAll()로 설정하여 개발 중.
'자료 보관함 > Java | SpringBoot' 카테고리의 다른 글
[Java] Java 배열에서 stream() 이해하기: filter, map, forEach 정리 (0) | 2025.05.18 |
---|---|
[Spring] AOP 관련 내용과 예시 - chat gpt (0) | 2024.11.25 |
[SpringBoot] JWT 토큰 발행 받기 (0) | 2024.11.08 |
[SpringBoot] Throwable cause, Throwable (0) | 2024.11.07 |
[SpringBoot] @JsonInclude (0) | 2024.11.07 |