Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #머신러닝 #딥러닝 #AI #서버 #자동화 #SQL #기본문법 #데이터베이스 #DBMS #Oracle #MongoDB #아키텍쳐 #DB
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #대용량트래픽 #처리방법 #캐싱 #코드최적화 #db최적화 #트래픽 #로드밸런서
- 스파르타 코딩클럽 #인메모리db #h2 #연동이슈 #문제해결 #방법 #spring security #header #
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #트랜잭션전파 #transaction
- 스프링 #백엔드 #자바
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #머신러닝 #AI #서버 #자동화 #SQL #기본문법 #데이터베이스 #웹개발
- 스파르타 코딩클럽 #내배캠 #최종프로젝트 #로그인 #인증인가 #jwt? #토큰인증 #액세스토큰 #리프레시토큰 #쿠키 #파싱 #서명키의 중요성 #security context holder
- 비트 #바이트 #이진수
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #최종프로젝트 #aws s3 #프로필 이미지 수정 #자동삭제
- java5기
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #aop #관점지향프로그래밍 #유지보수
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #ci #ioc #의존성
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #머신러닝 #AI #서버 #자동화 #SQL #KDT #기본문법 #데이터베이스 #Computer #Science #CPU #메모리
- 프로세스 #CPU #시공유 #커널
- ci/cd파이프라인
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #springmvc패턴 #model #view #controller
- 쓰레드 #쓰레드풀 #프로세스
- 인가 #
- 챌린저스 #bod
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #OSI #ISO #AI #서버 #자동화 #SQL #기본문법 #데이터베이스 #DBMS #Oracle #MongoDB #아키텍쳐 #DB
- 보안 #이슈
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #머신러닝 #AI #서버 #자동화 #SQL #기본문법 #데이터베이스
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #머신러닝 #딥러닝 #AI #서버 #자동화 #SQL #기본문법 #데이터베이스
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #ci #cd
- spring security #jwt 토큰 #json web token #token #직렬화 #인증
- 공간복잡도 #공간자원 #캐시메모리 #SRAM #DRAM #시간복잡도
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #get방식 #post방식 #http프로토콜 #클라이언트 #백엔드 #api
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #tdd #테스트 주도개발 #테스트코드 #유닛테스트
- 스파르타 코딩클럽 #내배캠 #최종프로젝트 #4개월삭제 #국비지원 #자바
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #소셜로그인 #네이버 #기능구현 #vue.js #spring boot #네이버로그인 #연동하기
Archives
- Today
- Total
요리사에서 IT개발자로
스프링 시큐리티 내부 구조 Security Filter Chain 등록, 여러개 등록, 특정 요청 허가 본문
커스텀 SecurityFilterChain 등록하는 방법
스프링 시큐리티 의존성을 추가하면
기본적인 DefaultSecurityFilterChain 하나가 등록됨
원하는 SecurityFilterChain 등록을 하기위해
SecuirtyFilterChain을 리턴하는
@Bean 메소드를 등록하면 된다.(한개 이상 등록이 가능하다)
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain1(HttpSecurity http) throws Exception{
return http.build();
}
}
한개 이상 등록하는 방법으로는 동일하게 하면된다.
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain1(HttpSecurity http) throws Exception{
return http.build();
}
@Bean
public SecurityFilterChain filterChain2(HttpSecurity http) throws Exception{
return http.build();
}
}
FilterChainProxy에서 등록확인하는 방법
디버그로 실행을하고
포스트맨으로 나의 해당 포트에 요청을 보내면
위와 같이 size가 2로 나오는 것을 확인할 수 있다.
위와 같이 설정한 것에 대한 문제점은
둘다 수행 될 수 있다는 점이다.
그래서 할 수 있는 방법
위에는 login 경로를 requestMatchers로 설정을 해놓고
2번째 Filterchain에는
다른 경로를 맵핑해 놓으면 로직에 맞게 수행된다.
@Bean
public SecurityFilterChain filterChain1(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((auth) -> auth
.requestMatchers("/user").permitAll());
return http.build();
}
@Bean
public SecurityFilterChain filterChain2(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((auth) -> auth
.requestMatchers("/admin").permitAll());
return http.build();
}
위와 같이 설정하면 될꺼같지만
시큐리티의 매핑자체가 없어서 잘못된 응답이 발생한다.
filterChain1이 filterChain2보다 먼저 등록되어있고
SecurityFilterChain에 대한 경로 설정을 추가적으로 하지않았기 때문에 그렇다.
해결하는 방법은
http
.securityMatchers((auth) -> auth.requestMatchers("/user"));
경로를 매핑해주면된다.
매핑을 해주면
@Bean
public SecurityFilterChain filterChain1(HttpSecurity http) throws Exception {
http
.securityMatchers((auth) -> auth.requestMatchers("/user"));
http
.authorizeHttpRequests((auth) -> auth
.requestMatchers("/user").permitAll());
return http.build();
}
@Bean
public SecurityFilterChain filterChain2(HttpSecurity http) throws Exception {
http
.securityMatchers((auth) -> auth.requestMatchers("/admin"));
http
.authorizeHttpRequests((auth) -> auth
.requestMatchers("/admin").authenticated());
return http.build();
}
경로로 접근을 /admin 경로로 접근을 하였을 때 오류가 안나는 것을 확인할 수있다.
또 다른 방법은 @Order() 를 이용하는 방법이다.
N개의 SecurityFilterChain을 등록한 뒤
등록되는 순서를 직접 선정하고 싶은 경우
@Bean
@Order(1)
public SecurityFilterChain filterChain1(HttpSecurity http) throws Exception{
return http.build();
}
@Bean
@Order(2)
public SecurityFilterChain filterChain2(HttpSecurity http) throws Exception {
return http.build();
}
위와 같이 @Order() 어노테이션 안의 숫자를 명시해주면된다.
마지막으로 특정 요청은 필터를 거치지 않도록 할 수 있다.
굳이 경로로 접근을 하지않아도
정적자원 (이미지, CSS)의 경우는 필터를 통과하지 않도록 설정할 수 있다.
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return web -> web.ignoring().requestMatchers("/img/**");
}
위와 같이 설정을 하게되면 서버의 자원을 들이지 않고 접근이 가능하다.
0번인덱스로 접근이 되기에 아주 빠르게 응답을 진행할 수 있다.
https://substantial-park-a17.notion.site/4-SecurityFilterChain-e9626964dc1a4f47aab0d257c73055d6
https://www.youtube.com/watch?v=QoPXJ2LEqTc&list=PLJkjrxxiBSFCFM0pjDwm6F98veieD0MER&index=4
반응형
'Spring' 카테고리의 다른 글
스파르타 부트캠프 스프링 부트 U(update)D(delete) 다락방 (3) | 2024.06.13 |
---|---|
스파르타 부트캠프 스프링 부트 프로젝트 생성 및 CR (다락방) (1) | 2024.06.12 |
Spring Security 내부 구조 DelegatingFilterProxy와 FilterChainProxy (0) | 2024.06.11 |
Spring Boot PostMan사용, JWT 인증,인가 (로그인, 로그아웃, 회원탈퇴) (1) | 2024.06.10 |
Spring Security JWT 인증 방식과 Security 동작원리 (0) | 2024.06.05 |