1. com.pure.security.config 안에 SecurityConfig.java를 만든다.
package com.pure.security.config;
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.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity //스프링 시큐리티 필터 등록
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/user/**").authenticated()
.antMatchers("/manager/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGER')")
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
.anyRequest().permitAll() //위의 세 주소 이외의 나머지는 로그인 없이 접근 가능
.and()
.formLogin()
.loginPage("/login"); // "/login"주소가 올 때는 로그인 페이지 보여주기
}
}
여기서 antMatchers는 각 요청에 대한 보안 수준을 조절하기 위해 사용하는 메서드임.
.authenticated()는 앞의 주소로 오는 요청은 인증된 경우에만 통과시켜 준다는 뜻임.
.antMatchers(HttpMethod.Post,"/manager").authenticated() 이런식으로 특정 http메서드 요청에 대해서도 필터링을 할 수 있음.
https://m.blog.naver.com/kimnx9006/220638156019
이 곳에 가면 각종 메서드에 대한 설명이 자세하게 나와있다.
연습용으로 테스트 해보는 것이기 때문에 csrf 방지 기능은 비활성화 했음.
'취업 준비 > Spring security' 카테고리의 다른 글
6. OAuth2 구글 로그인 (0) | 2022.02.17 |
---|---|
5. 시큐리티 권한처리 (0) | 2022.02.16 |
4. 로그인 만들기 (0) | 2022.02.16 |
3. 회원가입 만들기 (0) | 2022.02.10 |
1. 프로젝트 생성 및 설정 (0) | 2022.02.10 |