일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
- HTTP
- 인프런강의
- 자바스크립트recude
- 인프런인강
- .NET
- 자바스크립트함수
- c#
- 인프런무료강좌
- EntityFramework
- 제로초
- 고차함수
- 콜백함수
- 비주얼스튜디오
- 자바스크립트객체리터럴
- 인프런자바스크립트
- 객체리터럴
- 자바스크립트틱택토
- 자바스크립트파라미터
- 자바스크립트
- 객체의비교
- sort
- 틱택토구현
- NPM
- 인터넷프로토콜
- Blazor
- slice
- 인프런강좌
- 인프런
- 코딩
- 이벤트리스너
- Today
- Total
샐님은 개발중
Spring Security 프레임워크의 기본 설정 확인 본문
1. 기본 보안 필터
Spring boot 웹 어플리케이션을 실행하면 Spring Security에 의해 모든 요청이 증명되기를 원하는데
이것은 SpringBootWebSecurityConfiguration.java 에 있는 메소드에 의해 발생된다.
@ConditionalOnDefaultWebSecurity
static class SecurityFilterChainConfiguration {
SecurityFilterChainConfiguration() {
}
@Bean
@Order(2147483642)
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests((requests) -> {
((AuthorizeHttpRequestsConfigurer.AuthorizedUrl)requests.anyRequest()).authenticated();
});
http.formLogin(Customizer.withDefaults());
http.httpBasic(Customizer.withDefaults());
return (SecurityFilterChain)http.build();
}
}
java 문서에 의하면
The default configuration for web security. It relies on Spring Security's content-negotiation strategy to determine what sort of authentication to use. If the user specifies their own SecurityFilterChain bean, this will back-off completely and the users should specify all the bits that they want to configure as part of the custom security configuration.
웹 보안을 위한 기본 구성입니다. 사용할 인증 유형을 결정하기 위해 Spring Security의 콘텐츠 협상 전략을 사용합니다. 사용자가 자신의 SecurityFilterChain Bean을 지정하는 경우 이 설정은 일방적인 개입을 완전 중단되며 사용자는 맞춤형 보안 설저의 일부로 원하는 모든 부분을 지정해야 합니다.
2. 커스텀 필터 생성
- config 패키지 생성 후 , ProjectsecurityConfig.java 파일 생성
package wonCom.EaszBankBackend.config;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
import static org.springframework.security.config.Customizer.withDefaults;
@Configuration //이 클래스 안에 특정 설정을 정의 , 시작단계에서 이 클래스 안에 우리가 정의한 모든 bean 을 스캔함.
public class ProjectSecurityConfig {
@Bean
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
// http.authorizeHttpRequests((requests) -> requests.anyRequest().authenticated()); // 모든 요청이 디폴트로 보호됨
// http.formLogin(withDefaults());
// http.httpBasic(withDefaults());
// return http.build();
http.authorizeHttpRequests((requests) -> requests
.requestMatchers("/myAccount","/myBalance","/myLoans","/myCards").authenticated() // 보호됨 , /myAccount/** : myAccount 가 기본 경로인 모든 경로는 보호됨
.requestMatchers("/notices","/contact").permitAll()) // 자격증명 없이 허가됨
.formLogin(Customizer.withDefaults())
.httpBasic(Customizer.withDefaults());
return http.build();
}
}
3. 모든 요청 거부 처리
인증 -> 인가
인증 : 자격증명 요구 (로그인) -> 인가 (요청을 거부함. 403에러발생)
/**
* Configuration to deny all the requests
*/
http.authorizeHttpRequests(requests -> requests.anyRequest().denyAll())
.formLogin(Customizer.withDefaults())
.httpBasic(Customizer.withDefaults());
return http.build();
4. 모든 요청 허용 처리
/**
* Configuration to permit all the requests
*/
http.authorizeHttpRequests(requests -> requests.anyRequest().permitAll())
.formLogin(Customizer.withDefaults())
.httpBasic(Customizer.withDefaults());
return http.build();
'Spring Security - udemy' 카테고리의 다른 글
[섹션3] 유저 정의와 관리 - InMemoryUserDetailManager를 사용한 유저 설정 (0) | 2024.08.16 |
---|---|
[섹션3] 유저 정의와 관리 - InMemoryUserDetailManager를 사용한 유저 설정 (0) | 2024.08.14 |
Spring Security 내부 흐름 (0) | 2024.08.14 |
서플릿과 필터 (0) | 2024.08.14 |
Spring Security 사용 이유 (0) | 2024.08.14 |