[Spring Security] Spring Security 설정하기

2022. 10. 29. 00:57·BackEnd/Spring Security
728x90

Config 설정 클래스

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Configuration
@RequiredArgsConstructor
public class SecurityConfig {

    private final AuthenticationSuccess authenticationSuccess;
    private final AuthenticationFailure authenticationFailure;
    private final LogoutExecute logoutExecute;
    private final LogoutSuccess logoutSuccess;
    private final UserDetailsService userDetailsService;
    private final AuthenticationEntryException authenticationEntryException;
    private final AccessDeniedHandlerException accessDeniedHandlerException;

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .authorizeRequests() // 요청에 의한 보안검사 시작
                .anyRequest().authenticated(); //어떤 요청에도 보안검사를 한다.

        http.formLogin() // From 로그인 처리
                .loginPage("/member/login") // 사용자 정의 로그인 페이지
                .defaultSuccessUrl("/") // 로그인 성공 후 이동페이지
                .failureUrl("/login?error=true") // 로그인 실패 후 이동페이지
                .usernameParameter("userId") // 아이디 파라미터명
                .passwordParameter("password") // 비밀번호 파라미터명
                .loginProcessingUrl("/member/login") // 로그인 Form Action Url
                .successHandler(authenticationSuccess) // 로그인 성공 후 핸들러
                .failureHandler(authenticationFailure) // 로그인 실패 후 핸들러
                .permitAll() // .loginPage() 의 경로는 인증 없이 접근 가능
        ;
                
        http.logout() // 로그아웃 처리
                .logoutUrl("/member/logout") // 로그아웃 처리 URL(기본이 post)
                .logoutSuccessUrl("/") // 로그아웃 성공 URL
                .invalidateHttpSession(true) // 세션 무효화
                .deleteCookies("JSESSIONID") // 로그아웃 성공 시 제거할 쿠키명
                .addLogoutHandler(logoutExecute) // 로그아웃 핸들러
                .logoutSuccessHandler(logoutSuccess) // 로그아웃 성공 후 핸들러
        ; 
        
        http.exceptionHandling() // Exception 처리
                .authenticationEntryPoint(authenticationEntryException) // 인증 예외
                .accessDeniedHandler(accessDeniedHandlerException) // 인가 예외
        ;

        return http.build();
    }
    
    /**
     * 정적 자원 및 루트 페이지 ignore
     */
    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring()
                .requestMatchers(PathRequest.toStaticResources().atCommonLocations())
                .antMatchers("/", "/img/**", "/lib/**");
    }

}

 

/**
 * 로그인 성공 후 처리
 */
@Component
public class AuthenticationSuccess implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

        // AuthenticationException 발생 시 사용자의 요청 정보가 담긴 캐시가 세션에 저장됨 -> 로그인 성공 시 redirect
        RequestCache requestCache = new HttpSessionRequestCache();
        SavedRequest savedRequest = requestCache.getRequest(request, response);

        String redirectUrl = savedRequest.getRedirectUrl();
        response.sendRedirect(redirectUrl);

    }
}

 

Spring Security 제공 메서드 사용

인증 성공한 객체 정보 SecurityContextHolder.getContext().getAuthentication()
익명 사용자 isAnonymous() = ture
인증 사용자 isAuthenticated() = true
현재 사용자의 세션 만료 여부 체크 session.isExpired() = true

 

인증 / 인가 Exception 처리

인증 처리 예외 (신원 검증) AuthenticationException
인가 처리 예외 (권한 거부) AccessDeniedException

 

AuthenticationException(인증예외) 발생 시 AuthenticationEntryPoint 호출 (로그인 페이지 이동, 401 오류 코드 전달)

    - RequestCache : 추후 인증 성공 시 이전 요청 정보로 이동하기 위한 캐시 저장

    - SavedRequest : 사용자 요청 파라미터 및 헤더 정보

 

AccessDeniedException(인가예외) 발생 시 AccessDeniedHandler 호출 하여 예외 처리

 

/**
 *  인증 처리 예외 (신원 검증)
 *  RequestCache : 인증 성공 시 이전 요청 정보로 이동하기 위한 캐시 저장
 *  SavedRequest : 사용자 요청 파라미터 및 헤더 정보
 */
@Component
public class AuthenticationEntryPointException implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.sendRedirect("/login");
    }
}

 

/**
 * 인가 처리 예외 (권한 거부)
 */
@Component
public class AccessDeniedHandlerException implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        response.sendRedirect("/denied");
    }
}

 

728x90
반응형
저작자표시 (새창열림)

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

[Spring Security] 스프링 시큐리티를 이용하여 로그인, 회원가입 구현하기  (0) 2022.10.30
[Spring Secururity ERROR] 스프링 빈 순환 참조 에러 The dependencies of some of the beans i  (1) 2022.10.30
[Spring Security] 유저별 권한 설정  (0) 2022.10.28
[Spring Security] 동시 로그인 제한하기(동시 세션 제어)  (0) 2022.10.28
[Spring Security] Remember Me 인증(로그인 상태 유지)  (0) 2022.10.27
'BackEnd/Spring Security' 카테고리의 다른 글
  • [Spring Security] 스프링 시큐리티를 이용하여 로그인, 회원가입 구현하기
  • [Spring Secururity ERROR] 스프링 빈 순환 참조 에러 The dependencies of some of the beans i
  • [Spring Security] 유저별 권한 설정
  • [Spring Security] 동시 로그인 제한하기(동시 세션 제어)
콜팝
콜팝
Backend Developer 기술 블로그
  • 콜팝
    zhfvkq
    콜팝
  • 전체
    오늘
    어제
    • category (66)
      • AI (2)
      • Core (23)
        • Java & Kotlin (12)
        • Data Structure & Algorithm (5)
        • OOP & Design Pattern (3)
        • Operating System (3)
      • BackEnd (16)
        • Spring (3)
        • Spring Security (11)
        • JPA & ORM (1)
        • API & Architecture (1)
      • Database (4)
        • SQL & Tuning (2)
      • Security (2)
        • Vulnerability & CTF (1)
      • Infra (10)
        • AWS (2)
        • Docker & Kubernetes (2)
        • Linux (2)
        • Jenkins (1)
      • Tools (4)
        • Git & GitHub (2)
        • IntelliJ & IDE (2)
      • ETC (5)
      • private (0)
  • 블로그 메뉴

    • 홈
    • GitHub
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    토큰 해제
    텍스트 값 검색
    로그인 정보 기억
    자바
    claude code
    폴더 git 생성
    Outbox
    청년안심주택
    Config 설정
    Cloud Natice Architecture
    DistributedTransaction
    ajax 로그인
    친절한SQL튜닝
    아아디 기억 하기
    Linux
    스프링 시큐리티
    중복 로그인 제한
    세션 만료
    토큰
    로그인 해제
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
콜팝
[Spring Security] Spring Security 설정하기
상단으로

티스토리툴바