1. Spring Security
처음 프로젝트를 만들었을때 Spring Security 적용을 하였다.
Spring Security는 Spring 기반 애플리케이션에 인증과 권한 부여를 모두 제공해준다.
적용을 하고 프로젝트를 실행하면
모든 요청에 인증을 요구하기 때문에 첫 화면부터 인증하라는 로그인 화면이 뜰 것이다.
username : user
password는 내 인텔리제이 consol 화면에 보여준다.
'Using generated security password: 3d2c1d4c-8436-464c-9cd5-85d340fb8814'
모든 요청에 인증을 할 수도 없을뿐더러 나를 제외한 다른 사용자도 사용 할 수 없으므로 설정을 변경해야 한다.
2. websecurityconfigureradapter 문제 해결
SecurityConfiguration인 클래스를 생성하고 이 클래스에 websecurityconfigureradapter을 상속받아
configure 메서드를 오버라이딩을 하여 설정들을 변경하였다.
하지만...왠걸..?
SpringSecurity 5.7 이상부터는 더 이상 websecurityconfigureradapter 지원해주지 않는다...
SecurityFilterChain bean을 생성하여 HttpSecurity를 구성해야 한다.
변경 전
- websecurityconfigureradapter을 상속 받아 httpSecurity를 오버라이딩 하여 사용하였다.
@Override
protected void configure(HttpSecurity http) throws Exception {
http.
.authorizeRequests()
.mvcMatchers("/")
.permitAll();
}
변경 후
-SecurityFilterChain bean을 생성하여 사용한다.
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.mvcMatchers("/")
.permitAll();
return http.build();
3. 코드 설명
@Configuration
@EnableWebSecurity
public class SecurityConfiguration{
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.mvcMatchers("/")
.permitAll()
// '/'인 경로인 mvc는 모든 사용자가 접근 할 수 있다.
.and()
.authorizeRequests()
.antMatchers("/h2-console/**")
.permitAll()
// '/h2-console'로 시작하는 url은 모든 사용자가 접근 할 수 있다.
.and()
.csrf().disable()
.headers().frameOptions().disable();
return http.build();
}
}
@Configuration
- @Bean을 등록하고자 함을 명시해주는 어노테이션
- 스프링 컨테이너는 @Configuration 클래스를 찾아 그 안에 있는 @Bean이 있는 메소드를 찾아 빈을 생성해준다.
@EnableWebSecurity
- 웹 보안 활성화를 위한 어노테이션
.authorizeRequests
-특정한 경로에 특정한 권한을 가진 사용자만 접근을 가능하게 해준다.
.mvcMatchers
- 특정 경로를 지정해서 권한 설정을 가능하게 해준다.
- MVC매핑
- ex) .mvcMatchers("/index") -> /index, /index.html 매핑
.antMatchers
- 특졍 경로를 지정해서 권한 설정을 가능하게 해준다.
- URL 매핑
- ex) .antMatchers("/index") -> /index URL 매핑
.permitAll
- 모든 사용자가 접근 할 수 있다.
.csrf().disable().headers().frameOptions().disable()
- h2-consol화면을 사용하기 위해 disable 해준다.
'Springboot' 카테고리의 다른 글
[Sprong Boot]Swagger 사용하기 (0) | 2023.02.02 |
---|---|
[Spring Boot]OAuth2 (NAVER, KAKAO, GOOGLE LOGIN) (0) | 2023.01.31 |
Annotation 생성하기 @CurrentMember (0) | 2023.01.31 |
Repository 생성, Repository TestCode 작성하기 (0) | 2023.01.09 |
Jpa Domain 작성하기 (0) | 2023.01.09 |