[Spring Security] HttpSecurity 설정

우잼·2023년 9월 19일
post-thumbnail

1. Security


대부분의 시스템에는 회원의 관리를 하고 있고, 그에 따른 인증(Authentication)과 인가(Authorization)에 대한 처리를 한다. Spring에서는 Spring Security라는 별도의 프레임워크에서 관련된 기능을 제공한다.

❗ Spring 기반의 애플리케이션의 보안(인증과 권한, 인가 등)을 담당하는 스프링 하위 프레임워크 ‘인증’ 과 ‘권한’에 대한 부분을 Filter 흐름에 따라 처리한다. Filter는 가장 먼저 URL 요청을 받는다.

  • Filter 기반으로 동작
  • DispatcherServlet을 호출하기 전에 처리됨

    아직 Security 라는 프레임워크를 잘 알지 못해서 FilterChainProxy 에 대해서는 더 자세히 공부하고 벨로그에 쓸 예정..

2. HttpSecurity


2.1. HttpSecurity란?

spring security의 각종 설정은 HttpSecurity로 한다.

  • 리소스(URL) 접근 권한 설정
  • 인증 전체 흐름에 필요한 Login, Logout 페이지 인증완료 후 페이지 인증 실패시 이동페이지 설정
  • 인증 로직을 커스텀하기위한 필터 설정
  • 기타 csrf, 강제 https 호출 등등 거의 모든 스프링시큐리티 설정
  • 리소스(URL)의 권한 설정
  • 특정 리소스의 접근 허용 또는 특정 권한을 가진 사용자만 접근을 가능하게 할 수 있음

2.2. 설정

authorizeRequests() → authorizeHttpRequests()

  • security 처리에 HttpServletRequest를 이용한다는 것을 의미
// 스프링부트 3.1 이전
http.authorizeRequests()

// 스프링부트 3.1 이후
http.authorizeHttpRequests((auth) -> auth ...)

antMatchers → requestMatchers

  • 특정 리소스에 대해서 권한을 설정
// 스프링부트 3.1 이전
.antMatchers("/login**","/web-resources/**","/actuator/**")

// 스프링부트 3.1 이후
.requestMatchers("/login**","/web-resources/**","/actuator/**")

permitAll

  • requestMatchers로 설정한 리소스의 접근을 인증절차 없이 허용
.requestMatchers("/login**", "/web-resources/**", "/actuator/**").permitAll()

hasAnyRole

  • 리소스 ‘admin’ 으로 시작하는 모든 URL은 인증후 ADMIN 레벨의 권한을 가진 사용자만 접근 허용
.requestMatchers("/admin/**").hasAnyRole("ADMIN")

hasAuthority(String authority)

  • ROLE_USER, ROLE_ADMIN 등 지정한 권한 허용

hasAnyAuthority(String... authorities)

  • ROLE_USER, ROLE_ADMIN 등 지정한 권한이 하나라도 있으면 허용

hasIpAddress(String ipaddressExpression)

  • 지정한 ip 주소 허용

anonymous()

  • 익명 사용자 허용

rememberMe()

  • rememberMe 인증 사용자 허용

denyAll()

  • 모두 허용 안함

authenticated()

  • 인증된 사용자 허용

fullyAuthenticated()

  • 인증된 사용자 허용, rememberMe 인증 제외

3. Reference


스프링시큐리티 설정값 의미

[Spring Security] 스프링시큐리티 설정값들의 역할과 설정방법(2)

'authorizeRequests()' is deprecated 해결 || Spring Security Configuration

profile
나는 재민

1개의 댓글

comment-user-thumbnail
2024년 11월 6일

진짜 복 받으세요 사랑합니다

답글 달기