ahnnyung ,/Spring

[Spring] Spring Security?

hi,ho 2020. 11. 19. 15:23
반응형

[Spring] Spring Security

Spring에서 제공해주는 인증(Authentication)과 인가(Authorization)에 대한 처리를 해주는 Framework이다. Spring Security를 사용하지 않으면 직접 세션을 체크하고 redirect 작업을 해주어야하지만, Spring Security는 filter를 기반으로 동작하기 때문에 springMVC와 별개로 동작한다.

Spring Security 주요 용어

  1. 접근 주체(Principal)
    보호된 리소스에 접근하는 사용자 - Authentication으로 추상화

  2. 인증(Authenticate)

    보호된 리소스에 접근한 대상에 대해 이 유저가 누구인지, 애플리케이션의 작업을 수행해도 되는 주체인지 확인하는 과정. 해당 사용자가 본인이 맞는지를 확인하는 절차 - 주요 컴포넌트 - AuthenticationManager, AuthenticationProvider

  3. 인가(Authorize)
    해당 리소스에 대해 접근 가능한 권한을 가지고 있는지 확인하는 과정 - AccessDecisionManager, AccessDecisionVoter

  4. GrantedAuthority
    인증된 사용자의 인증정보(역할 등)을 표현

  5. SecurityContext

    접근 주체(Authentication)와 인증정보(GrantedAuthority)을 담고 있는 Context. - ThreadLocal에 보관되며, SecurityContextHolder를 통해 접근할 수 있음

Spring Security Filter

주요Filter 정리

  • SecurityContextPersistenceFilter
    - SecurityContextRepository를 통해 SecurityContext를 Load/Save 처리

  • LogoutFilter
    - 로그아웃 URL(기본값: /logout)로의 요청을 감시하여 해당 사용자를 로그아웃 시킴

  • UsernamePasswordAuthenticationFilter
    - ID/비밀번호 기반 Form 인증 요청 URL(기본값: /login)을 감시하여 사용자를 인증함

  • ExceptionTransiationFilter
    - 요청을 처리하는 중에 발생할 수 있는 예외를 위임하거나 전달

  • FilterSecurityInterceptor

    - 접근 권한 확인을 위해 요청을 AccessDecisionManager로 위임

    • 이 필터가 실행되는 시점에는 사용자가 인증됐다고 판단

Spring Security 인증 처리 요약

인증(Authentication) 처리를 위한 핵심 컴포넌트

  • UsernamePasswordAuthenticationFilter
    • 사용자 인증 요청을 Authentication 인터페이스로 추상화하고, AuthenticationManager를 호출한다.
    • Authentication 인터페이스에서 제공하는 핵심 메소드
      • Object getPrincipal() - 인증 아이디
      • Object getCredentials() - 인증 비밀번호
      • Collection<? extends GrantedAuthority> getAuthorities() - 인증된 사용자의 권한 목록
      • Object getDetails() - 인증된 사용자의 부가정보
  • AuthenticaitonManager
    • 사용자 아이디/비밀번호를 인증하기 위해 적절한 AuthenticationProvider 찾아 처리를 위임한다.
  • AuthenticationProvider
    • 실질적으로 사용자 인증을 처리하고, 인증 결과를 Authentication 인터페이스로 반환한다.

Spring Security 인가 처리 요약

인가(Authorization) 처리를 위한 핵심 컴포넌트

  • AccessDecisionManager
    - 인증된 사용자의 보호 리소스 접근 여부를 판단한다. 3개의 기본 구현을 제공한다.
    - AffirmativeBased: 접근을 승인하는 voter가 1개 이상
    - ConsesusBased: 과반수
    - UnanimouseBased: 모든 voter가 승인해야 함

  • AccessDecisionVoter

    - AccessDecisionManager는 다수의 AccessDecisionVoter로 구성된다.

    • 각각의 Voter는 주어진 정보를 기반으로 승인(ACCESS_GRANTED), 거절(ACCESS_DENIED), 보류(ACCESS_ABSTAIN)를 반환한다.
      • RoleVoter는 보호 리소스에 접근하기 위한 구너환을 사용자가 지니고 있는지 확인함
        • WebExpressionVoter는 SpEL 표현식에 따른 웹 접근제어를 처리함

인가 처리에 대한 기준을 조건에 따라 ACCESS_GRANTED, ACCESS_DENIED, ACCESS_ABSTAIN 3개 중 승인, 거절, 보류를 반환해주면 된다.

반응형