세션 제어 필터
SessionManagementFilter
- 세션 관리
- 인증 시 사용자의 세션정보를 등록, 조회, 삭제 등의 세션 이력을 관리
- 동시적 세션 제어
- 동일 계정으로 접속이 허용되는 최대 세션 수를 제한
- 세션 고정 보호
- 인증 할 때마다 세션 쿠키를 새로 발급하여 공격자의 쿠키 조작을 방지
- 세션 생성 정책
- Always, If_Required, Never, Statless
ConcurrentSessionfilter
- 매 요청 마다 현재 사용자의 세션 만료 여부 체크
- 세션이 만료되었을 경우 즉시 만료 처리
- session.isExpired() == true
- 로그아웃
- 즉지 오류 페이지 응답
ConcurrentSessionControlAuthenticationStrategy
: 동시적 세션제어 담당. 최대세션 갯수 확인ChangeSessionIdAuthenticationStrategy
: 세션고정보호 담당RegisterSessionAuthenticationStrategy
: 사용자의 세션을 등록하고 저장
인가 API
권한설정
- 선언적 방식
- URL
- Method
- 동적방식 - DB 연동 프로그래밍
- URL
- Method
URL 방식
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.securityMatcher("/**") // "/**" 이하의 URL에서만 시큐리티 작동하도록
.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/", "/main", "/users").permitAll() // 모든 사용자가 접근가능
.requestMatchers("/admin").hasRole("ADMIN") // 해당 권한만 접근 가능
.requestMatchers(antMatcher("/manager")).hasRole("MANAGER")
.anyRequest().authenticated() // 나머지 URL은 인증이 필요함.
)
.formLogin(withDefaults());
return http.build();
}
메소드 | 동작 |
---|---|
authenticated() | 인증된 사용자의 접근을 허용 |
fullyAuthenticated() | 인증된 사용자의 접근을 허용, RememberMe 인증 제외 |
permitAll() | 무조건 접근을 허용 |
denyAll() | 무조건 접근을 허용하지 않음 |
anonymous() | 익명사용자의 접근을 허용(RoleUSER같은 가은 권한말고 익명의 권한) |
rememberMe() | 기억하기를 통해 인증된 사용자의 접근을 허용 |
access(String) | 주어진 SpEL 표현식의 평가 결과가 true이면 접근을 허용 |
hasRole(String) | 사용자가 주어진 역할이 있다면 접근을 허용 |
hasAuthority(String) | 사용자가 주어진 권한이 있다면 |
hasAnyRole(String...) | 사용자가 주어진 권한이 있다면 접근을 허용 |
hasAnyAuthority(String...) | 사용자가 주어진 권한 중 어떤 것이라도 있다면 접근을 허용 |
hasIpAddress(String) | 주어진 IP로부터 요청이 왔다면 접근을 허용 |
예외 처리 및 요청 캐시 필터
제일 마지막 필터인 FilterSecurityIntercepter
에서 throw
ExceptionTranslationFilter
AuthenticationException
- 인증 예외 처리
- AuthenticationEntryPoint 호출
- 로그인 페이지 이동, 401 오류 코드 전달 등
- 인증 예외가 발생하기 전의 요청 정보를 저장
RequestCache
- 사용자의 이전 요청 정보를 세션에 저장하고 이를 꺼내 오는 캐시 메카니즘SaveRequest
- 사용자가 요청했던 request파라미터 값들, 그 당시의 헤더값들 등이 저장
- AuthenticationEntryPoint 호출
- 인증 예외 처리
AccessDeniedException
- 인가 예외 처리
AccessDeninedHandler
에서 예외 처리하도록 제공
- 인가 예외 처리
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint())
.accessDeninedHandler(accessDeniedHandeler())
return http.build();
}
'Spring > Security' 카테고리의 다른 글
스프링 시큐리티 - 2 (0) | 2022.12.05 |
---|---|
스프링 시큐리티 - 1 (0) | 2022.12.02 |