본문 바로가기
개발/Security

[SpringBoot] Security 로그인 인증, 인가(3) - handler

by 코딩하는 흰둥이 2023. 4. 30.
반응형

이전글 https://greed-yb.tistory.com/224

 

[SpringBoot] Security 로그인 인증, 인가(2) - SecurityConfig

이전글 https://greed-yb.tistory.com/223 [SpringBoot] Security 로그인 인증, 인가(1) - 환경설정 Java 17 Maven Spring Boot 3.0.3 Spring Security 3.0.4 Oracle 11g Mybatis IntelliJ Ultimate DBeaver Thymeleaf 폴더 구조 더보기 Security 한번

greed-yb.tistory.com

 

 

WebAccessDeniedHandler(인가)

AccessDeniedHandler는 인증은 되었지만 서버에 요청을 할 때 액세스가

가능한지 권한을 체크 후 액세스 할 수 없는 요청을 했을시 동작된다.

package com.example.practice.security.handler;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import java.io.IOException;

public class WebAccessDeniedHandler implements AccessDeniedHandler {

    private String errorPage = "/denied";

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException, ServletException {
        String deniedUrl = errorPage + "?exception=" + accessDeniedException.getMessage();
        response.sendRedirect(deniedUrl);
    }

// WebSecurityConfig 에서 @Bean 으로 AccessDeniedHandler 메서드를 이용하여 WebAccessDeniedHandler 를 생성할때 에러페이지를 set해주는 메서드
//    public void setErrorPage(String errorPage){
//        this.errorPage = errorPage;
//    }
}

setErrorPage 메서드는 WebSecurityConfig 에서 exception 처리할때

.accessDeniedHandler(new WebAccessDeniedHandler()) -> .accessDeniedHandler(webAccessDeniedHandler()) 로 

변경하여 아래 와 같이 사용할 수 있다 참고 할것

 

@EnableWebSecurity
@Configuration
public class WebSecurityConfig {

    @Bean
    public AccessDeniedHandler accessDeniedHandler() {
        WebAccessDeniedHandler webAccessDeniedHandler = new WebAccessDeniedHandler();
        webAccessDeniedHandler.setErrorPage("/denied");
        return webAccessDeniedHandler;
    }



    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

        http.csrf().disable();
        ..
        ..
        ..
                // exception 처리
        http
                .exceptionHandling()
                // 인가 권한이 없는 사용자 접근 시
                .accessDeniedHandler(webAccessDeniedHandler())

 

 

 

WebAuthenticationEntryPoint(인증)

 

AuthenticationEntryPoint는 인증이 되지않은 유저가 요청을 했을때 동작된다.

package com.example.practice.security.handler;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import java.io.IOException;

public class WebAuthenticationEntryPoint implements AuthenticationEntryPoint {
    private String errorPage;
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException) throws IOException, ServletException {

        // 이동하려는 주소와 error message 전송
        //String deniedUrl = errorPage + "?exception=" + authException.getMessage();
        response.sendRedirect("/login");
    }

// 인증이 되지 않았을 경우 무조건 login page로 이동하려고 사용하지 않음
//    public void setErrorPage(String errorPage){
//        this.errorPage = errorPage;
//    }
}

AuthenticationEntryPoint 도 WebSecurityConfig 에서 @Bean 을 추가하여 사용해도 된다

댓글