ysk(0soo)
Lifealong
ysk(0soo)
전체 방문자
오늘
어제
  • 분류 전체보기 (238)
    • Java (50)
      • whiteship-java-study (11)
      • Java (28)
      • time (6)
    • Spring (68)
      • JPA (15)
      • Spring (1)
      • SpringBoot (1)
      • SpringMVC (6)
      • Spring Security (22)
      • Jdbc (1)
      • RestDocs (14)
      • log (6)
    • Kotlin (3)
    • Web (2)
      • nginx (1)
    • Database (14)
      • MySQL (5)
      • PostgreSQL (1)
      • SQL (1)
      • Redis (4)
    • C, C++ (0)
    • Git (1)
    • Docker (2)
    • Cloud (3)
      • AWS (3)
    • 도서, 강의 (0)
      • t5 (0)
    • 기타 (7)
      • 프로그래밍 (1)
    • 끄적끄적 (0)
    • CS (14)
      • 운영체제(OS) (2)
      • 자료구조(Data Structure) (9)
    • 하루한개 (12)
      • 우아한 테크코스-10분테코톡 (12)
    • 스터디 (12)
      • 클린 아키텍처- 로버트마틴 (2)
      • JPA 프로그래밍 스터디 (10)
    • 테스트 (34)
      • JUnit (19)
      • nGrinder (2)
      • JMeter (0)
    • Infra (3)
    • 프로그래머스 백엔드 데브코스 3기 (0)
    • 디자인 패턴 (3)
    • Issue (4)
    • system (1)
      • grafana (0)
      • Prometheus (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • github

공지사항

인기 글

태그

  • restdocs custom
  • AuthenticationException
  • querydsl
  • 가상 스레드 예외 핸들링
  • 인가(Authorization) 처리
  • 동일성
  • junit5
  • StructuredConcorrency
  • node exporter basic auth
  • FilterSecurityInterceptor
  • nginx basic auth
  • VirtualThread Springboot
  • LocalDateTime
  • UserDetailsService
  • scope value
  • tree
  • 가상 스레드
  • nGrinder
  • 트랜잭션
  • DataJpaTest
  • java
  • mysql
  • 동등성
  • AccessDecisionManager
  • AccessDecisionVoter 커스텀
  • 정규표현식
  • jpa
  • 구조화된 동시성
  • 동시성 제어
  • restdocs enum

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ysk(0soo)

Lifealong

Spring/Spring Security

인증 예외(AuthenticationException)를 처리하는 AuthenticationEntryPoint

2022. 12. 15. 10:09

Spring Security 인증 예외를 처리하는 AuthenticationEntryPoint

 

AuthenticationEntryPoint 는 인증되지 않은 사용자에 대한 처리를 담당한다. (핸들러 라고 볼 수 있다)

이 때 처리는, 그냥 http 응답이라던가, 페이지 redirect 라 던가 등 개발자가 정의할 수 있다.

 

  • AuthenticationException (인증 예외)예외가 발생했을떄 처리하는 인터페이스이다.
  • 헷갈리지 말아야 하는게, AccessDeniedException( 인가 예외) 가 발생했을 때 처리하는 인터페이스가 아니다.
/**
 * Used by {@link ExceptionTranslationFilter} to commence an authentication scheme.
 */
public interface AuthenticationEntryPoint {

}

 

인증(Authentication)

  • 사용자의 신원을 확인하는 과정
    • 아이디/패스워드 기반 로그인
    • OAuth2.0 프로토콜을 통한 Social 인증

인가(Authorization)

  • 어떤 개체가 어떤 리소스에 접근할 수 있는지 또는 어떤 동작을 수행할 수 있는지를 검증하는 것, 즉 접근 권한을 얻는 일을 의미
  • 적절한 권한이 부여된 사용자들만 특정 기능 수행 또는 데이터 접근을 허용함

 

인증이 되지 않은 사용자 라는것은 아이디 패스워드가 틀렸거나해서 로그인 하지 못한 비 로그인 사용자 라는것이다.

그러므로 로그인 페이지 등을 통하여 로그인을 하고 인증을 해야 하는데, 로그인 페이지등으로 이동시켜주거나, 401 에러 응답을 보내주는 역할을 담당하는 인터페이스가 AuthenticationEntryPoint이다.

Spring Security에서 FilterSecurityInterceptor 필터가 마지막으로 인증 및 인가 처리를 하는데,

  • 인증객체 없이 보호자원에 접근 시도하면 AuthenticationException
  • 인증 후 자원 접근 권한이 없을 경우 AccessDeniedException

예외를 발생시킨다.

 

!이 때, ExceptionTranslationFilter가 이 예외들을 잡아서 처리하는데 AuthenticationException은 인증되지 않았다는 예외로써,

ExceptionTranslationFilter가 AuthenticationEntryPoint 를 이용해서 응답을 하거나, 로그인 페이지로 이동 시킨다.

  • ExceptionTranslationFilter 클래스 이며, 이 클래스는 내부적으로 AuthenticationEntryPoint 인터페이스를 필드로 가지고 있다.
protected void sendStartAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
            AuthenticationException reason) throws ServletException, IOException {
        // SEC-112: Clear the SecurityContextHolder's Authentication, as the
        // existing Authentication is no longer considered valid
        SecurityContext context = SecurityContextHolder.createEmptyContext();
        SecurityContextHolder.setContext(context);
        this.requestCache.saveRequest(request, response);

        this.authenticationEntryPoint.commence(request, response, reason); // 이부분 호출
    }

 

AuthenticationEntryPoint의 구현체는 여러가지가 있으며, 필요에 맞게 사용하거나, 인터페이스를 구현해서 커스텀해서 사용할 수도 있다.

  • 그냥 401 응답을 내려주거나
  • 에러 페이지로 보내주거나
    • response.sendRedirect("/error.html");
  • 로그인 페이지로 이동시켜주거나 (LoginUrlAuthenticationEntryPoint)
  • 커스텀 해서 사용해도 된다.

 

AuthenticationEntryPoint 구현체 적용

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return 
      http.exceptionHandling()                     
      .authenticationEntryPoint(new CustomAuthenticationEntryPoint())  // << 인증실패 시 처리                
      .accessDeniedHandler(accessDeniedHandler()) 
      .build();
}
저작자표시 비영리 (새창열림)

'Spring > Spring Security' 카테고리의 다른 글

Spring Security - SecurityContext, SecurityContextHolder, SecurityContextHolderStrategy, Authentcation  (0) 2022.12.17
FilterChainProxy  (0) 2022.12.15
DelegatingFilterProxy - Spring Security  (0) 2022.12.15
Security - WebsecurityConfigurerAdapter Deprecated 대체  (0) 2022.12.12
Spring security - Role vs Authority (hasRole? hasAuthority?)  (0) 2022.11.24
    'Spring/Spring Security' 카테고리의 다른 글
    • FilterChainProxy
    • DelegatingFilterProxy - Spring Security
    • Security - WebsecurityConfigurerAdapter Deprecated 대체
    • Spring security - Role vs Authority (hasRole? hasAuthority?)
    ysk(0soo)
    ysk(0soo)
    백엔드 개발을 좋아합니다. java kotlin spring, infra 에 관심이 많습니다. email : kim206gh@naver.com github : https://github.com/devysk

    티스토리툴바