[Spring Security] Security Config 파일 만들기
2024. 1. 4. 00:05ㆍ백엔드/Spring Boot
강의에서는 WebSecurityConfigurerAdapter를 extends 하는 방식으로 개발을 하게 되어 있는데 Spring boot 3 버전 이상부터는 Spring security 6 버전을 dependency 하게 되었고, WebSecurityConfigurerAdapter 가 Deprecated(사용할 수 없음) 되었다.
문법은 람다 형식으로 변경되었다.
공식문서 : https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter/
# Security Config 파일을 만들어보자
- @EnableWebSecurity : Spring Security를 사용하여 웹 보안을 활성화
- Customizer.withDefaults() : 설정 기본값을 사용
- authorizeHttpRequests() : 특정 리소스의 접근 허용 또는 특정 권한을 가진 사용자만 접근하도록 설정
- anyRequest() : 모든 요청
- authenticated() : 인증된 사용자만 접근 가능 , 인증 필수
- httpBasic() : 사용자 인증방법으로 HTTP Basic Authentication을 사용
- formLogin() : 사용자 인증방법으로 폼 형식의 로그인을 활성화
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(
(auth) -> auth.anyRequest().authenticated()
)
.formLogin(Customizer.withDefaults()); // or .httpBasic(Customizer.withDefaults())
return http.build();
}
}
# 환경설정 파일(application.properties)에 계정 정보를 등록하자
spring.security.user.name=user
spring.security.user.password=1234
실행 화면
Reference
인프런 '스프링 시큐리티 - Spring Boot 기반으로 개발하는 Spring Security' - 정수원
스프링 시큐리티 강의 - 인프런
초급에서 중.고급에 이르기까지 스프링 시큐리티의 기본 개념부터 API 사용법과 내부 아키텍처를 학습하게 되고 이를 바탕으로 실전 프로젝트를 완성해 나감으로써 스프링 시큐리티의 인증과
www.inflearn.com
'백엔드 > Spring Boot' 카테고리의 다른 글
[Spring Security] Logout 구현하기 (1) | 2024.01.04 |
---|---|
[Spring Security] Form Login 구현하기 (2) | 2024.01.04 |
[Spring Security] 의존성 추가해보기 (1) | 2024.01.03 |
JWT를 적용한 로그인 구현하기 (4) | 2024.01.03 |
DTO ↔ Entity 변환하기 (1) | 2023.11.16 |