Spring Security란?
Spring Security는 애플리케이션의 보안(인증 및 권한 관리)을 담당하는 프레임워크.
비유: 외부 인력이 사내 시스템에 접근하는 경우, 보안 정책과 접근 권한을 설정해야 함.
Spring Security 설정 예제
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authorizeHttpRequests) ->
authorizeHttpRequests
.requestMatchers("/h2-console/**").permitAll()
.requestMatchers(HttpMethod.GET, "/api/*/posts/{id:\\d+}", "/api/*/posts", "/api/*/posts/{postId:\\d+}/comments").permitAll()
.anyRequest().authenticated()
)
.headers((headers) -> headers
.addHeaderWriter(new XFrameOptionsHeaderWriter(
XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN)))
.csrf((csrf) -> csrf.disable());
return http.build();
}
}
특정 사용자만 요청을 허용하는 방법
@WithUserDetails("user")
'Java > Spring' 카테고리의 다른 글
Web Server와 WAS (1) | 2025.02.16 |
---|---|
어노테이션(Annotation) (2) | 2025.02.09 |
OSIV (0) | 2025.02.06 |
Servlet, Dispatcher Servlet, Servlet Container (0) | 2025.02.02 |
스프링에서 잭슨 라이브러리의 직렬화/역직렬화 (1) | 2025.01.18 |