분류 전체보기 77

[스프링 시큐리티] SecurityContextPersistenceFilter

SecurityContextRepository를 사용하여 Repository 전략에 맞게 SecurityContext를 읽어오거나 초기화 기본 전략은 HttpSessionSecurityContextRepository - HttpSession을 사용한다 This filter MUST be executed BEFORE any authentication processing mechanisms. Authentication processing mechanisms (e.g. BASIC, CAS processing filters etc) expect the SecurityContextHolder to contain a valid SecurityContext by the time they execute. 기본적으로 인..

스터디-Spring 2022.01.30

[스프링 시큐리티] @Async & WebAsyncManagerIntegrationFilter

Async 웹 MVC를 지원하는 필터 스프링 MVC의 Async 기능(핸들러에서 Callable을 리턴할 수 있는 기능)을 사용할 때에도 SecurityContext를 공유하도록 도와주는 필터 해당 필터는 AsyncManager에 Interceptor를 등록하고 핵심 기능은 SecurityContextCallableProcessingInterceptor 에서 구현 PreProcess: SecurityContext를 설정 Callable: 다른 쓰레드이지만 그 안에서는 동일한 SecurityContext를 참조할 수 있음 PostProcess: SecurityContext를 clear @Async를 사용하는 서비스를 호출하는 경우, Thread가 다르기 때문에 SecurityContext를 공유받지 못한다..

스터디-Spring 2022.01.21

[스프링 시큐리티] 스프링 시큐리티 아키텍처 정리

AbstractSecurityWebApplicationInitializer 또는 SecurityFilterAutoConfiguration로 자동으로 DeligatingFilterProxy를 등록 요청을 FilterChainProxy로 위임함 Filter들은 WebSecurity, HttpSecurity를 통해 만들어짐 The WebSecurity is created by WebSecurityConfiguration to create the FilterChainProxy known as the Spring Security Filter Chain (springSecurityFilterChain). The springSecurityFilterChain is the Filter that the Delegatin..

스터디-Spring 2022.01.17

[스프링 시큐리티] ExceptionTranslationFilter

필터 체인에서 발생하는 AccessDeniedException과 AuthenticationException을 처리하는 필터 private void handleAccessDeniedException(HttpServletRequest request, HttpServletResponse response, FilterChain chain, AccessDeniedException exception) throws ServletException, IOException { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); boolean isAnonymous = this.authenticationTrustResol..

스터디-Spring 2022.01.17

[스프링 시큐리티] FilterSecurityIntercepter

AccessDecisionManager를 사용하여 Access Control 또는 예외 처리하는 필터, 대부분의 경우 FilterChainProxy에 제일 마지막 필터로 들어있다. Authorization(인가)를 시도한다 이때 accessDecisionManage.decide메서드를 호출하는데, AccessDeniedException이 발생하면 그전 단계인 ExceptionTranslationFilter에서 예외를 처리한다. private void attemptAuthorization(Object object, Collection attributes, Authentication authenticated) { try { this.accessDecisionManager.decide(authenticated..

스터디-Spring 2022.01.17

[스프링 시큐리티] AccessDecisionManager

public interface AccessDecisionManager { void decide(Authentication authentication, Object object, Collection configAttributes) throws AccessDeniedException, InsufficientAuthenticationException; boolean supports(ConfigAttribute attribute); boolean supports(Class clazz); } Authorization(인가)를 위한, Access Control 결정을 내리는 인터페이스, 구현체 3가지를 기본으로 제공한다. AffirmativeBased: 여러 Voter중에 한명이라도 허용하면 허용, 기본 전략. C..

스터디-Spring 2022.01.13

[스프링 시큐리티] DelegatingFilterProxy와 FilterChainProxy

https://docs.spring.io/spring-security/reference/servlet/architecture.html Architecture :: Spring Security Spring Security’s Servlet support is based on Servlet Filters, so it is helpful to look at the role of Filters generally first. The picture below shows the typical layering of the handlers for a single HTTP request. The client sends a request to the appl docs.spring.io 서블릿에는 Filter가 존재함, 특정..

스터디-Spring 2022.01.13

[스프링 시큐리티] Filter와 FilterChainProxy

FilterChainProxy는 SecurityFilterChain 인스턴스로 구성되어 있는 리스트를(filterChains) 사용하며, 각 인스턴스에는 RequestMatcher와 일치하는 요청에 적용해야하는 필터 목록이 포함되어 있다 FormLogin, BasicLogin 설정시 아래와 같다. WebAsyncManagerIntergrationFilter SecurityContextPersistenceFilter HeaderWriterFilter CsrfFilter LogoutFilter UsernamePasswordAuthenticationFilter(formlogin) DefaultLoginPageGeneratingFilter(formlogin) DefaultLogoutPageGeneratingFi..

스터디-Spring 2022.01.12

[스프링 시큐리티] Authentication과 SecurityContextHolder

AuthenticationManager가 인증을 마친 뒤 리턴 받은 Authentication 객체는? SecurityContextPersistenceFilter -> CsrfFilter -> LogoutFilter -> AbstractAuthenticationProcessingFilter-> UsernamePasswordAuthenticationFilter SecurityContextPersistenceFilter SecurityContext를 HTTP session에서 캐시(기본 전략)하여 여러 요청에서 Authentication을 공유하는 필터 SecurityContextRepository를 교체하여 세션을 HTTP session이 아닌 다른 곳에 저장하는 것도 가능 UsernamePasswordA..

스터디-Spring 2022.01.11

[스프링 시큐리티] ThreadLocal

java.lang 패키지에서 제공하는 쓰레드 범위 변수 스코프가 쓰레드 이므로 각 쓰레드 내에서만 공유 같은 쓰레드라면 서로 다른 메서드에서도 사용 가능 SecurityContextHolder의 기본 전략 public class AccountContext { private static final ThreadLocal ACCOUNT_THREAD_LOCAL = new ThreadLocal(); public static void setAccount(Account account) { ACCOUNT_THREAD_LOCAL.set(account); } public static Account getAccount() { return ACCOUNT_THREAD_LOCAL.get(); } }

스터디-Spring 2022.01.10