▶ 설치
build.gradle 파일에 다음과 같이 추가
dependencies{
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
}
▶ 설정
스프링 시큐리티 설정을 담당할 SecurityConfig.java 파일 생성
package com.mysite.seouldensity;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
http
.authorizeHttpRequests((authorizeHttpRequests) ->
authorizeHttpRequests
.requestMatchers(new AntPathRequestMatcher("/**")).permitAll());
return http.build();
}
}
→ 코드 해석
@Configuration : 이 파일이 스프링의 환경 설정 파일임의 의미한다.
@EnableWebSecurity : 모든 요청 URL이 스프링 시큐리티의 제어를 받도록 만든다.(스프링 시큐리티를 활성화하는 역할)
@Bean : 스프링 컨테이너가 객체의 생성, 관리, 주입 등을 자동으로 처리해준다.
SecurityFilterChain 클래스는 모든 요청 URL에 이 클래스가 필터로 적용되어 URL 별로 특별한 설정을 할 수 있도록 해준다.
http
.authorizeHttpRequests((authorizeHttpRequests) ->
authorizeHttpRequests
.requestMatchers(new AntPathRequestMatcher("/**")).permitAll());
다음은 인증되지 않은 모든 페이지의 요청을 허락한다는 의미이다
→ 따라서 로그인하지 않아도 모든 페이지에 접근할 수 있다.
'SpringBoot > 스프링시큐리티' 카테고리의 다른 글
| 스프링 시큐리티 - 로그인 기능 (0) | 2024.07.16 |
|---|---|
| 스프링 시큐리티 - 회원가입 기능 (0) | 2024.07.16 |