[Spring] 스프링 시큐리티 설정이 @Bean 기반 구성으로 바뀐 이유
·
Spring Security
Spring Security에서 WebSecurityConfigurerAdapter를 @Bean 기반 구성으로 변경한 이유📌 서론Spring Security는 스프링 서버를 구성하면서 보안을 적용하는 데 많이 사용된다. 특히, 요즘처럼 클라이언트 측 렌더링(Client-Side Rendering, CSR)을 많이 사용하는 환경에서 JWT(JSON Web Token)를 사용하여 인증 및 인가를 구현하는 애플리케이션에서 Spring Security는 매우 중요한 역할을 한다. Spring Security 5.7부터는 시큐리티의 클래스 구성 방식이 기존과는 상당히 달라졌다. 이 글에서는 그 변화를 간단히 설명하고, 새로운 보안 구성 클래스의 작성 방법을 간단히 알아보도록 하자. 1. WebSecurityCo..
Spring Security6 - Authentication(인증)
·
Spring Security
1. 인증 관련 주요 클래스와 인터페이스 및 동작원리 Authentication: 인증 요청과 인증된 주체를 나타내는 인터페이스이다. AuthenticationManager: 실제로 인증을 처리하는 인터페이스이다. UserDetailsService: 사용자의 세부 정보를 로드하는 방법을 정의한다. PasswordEncoder: 비밀번호의 암호화 또는 해싱을 처리한다. Spring Security 6의 주요 인증 방식(Basic Authentication, Form-Based Authentication, OAuth2, JWT)은 모두 AuthenticationManager를 사용한다. AuthenticationManager는 UserDetailsService를 호출하여 사용자 정보를 로드하고, Authen..
Spring Security와 CORS: 크로스 도메인 요청 처리 기법 알아보기
·
Spring Security
웹 애플리케이션을 개발하면서 보안과 리소스 공유는 중요한 고려사항이다. 이번에는 Spring Security와 CORS(Cross-Origin Resource Sharing)에 대해서 자세히 알아보자 1. 크로스 도메인(Cross-Domain) 이해하기 1-1. 크로스 도메인 정의: 크로스 도메인은 한 도메인에서 실행되는 웹 페이지가 다른 도메인의 리소스에 접근할 때 발생하는 상황을 의미한다. 예시: http://domain-a.com에서 로딩된 웹 페이지가 http://domain-b.com의 이미지나 API에 접근하는 것이다. domain-a.com: 여기서 로딩된 웹 페이지가 다른 도메인의 리소스에 접근하려고 한다. domain-b.com: 이 도메인은 이미지나 API와 같은 리소스를 제공한다. A..
Spring Boot 3.1 & Spring Security 6: Security Config 최적화 리팩토링 (12편)
·
Spring Security
이번에는 필요없는 필터는 없애고 최적화를 진행하기 위해 Security Config를 리팩토링 했다. 1. SecurityConfig 리팩토링 앞의 게시글을 확인해보면 작성한 코드가 있기에 변경된 메서드만 설명하겠다. 일단 나는 ssl(서버 사이드 랜더링)이기 때문에 에러페이지, 메인페이지, 회원가입, 로그인, 정적 리소스는 모두 접근을 허가해줬다. 여기서 특히 바뀐건 addFilterBefore() 이것이다. jwtAuthorizationFilter 다음 바로 customAuthenticationFilter로 가도록 했다. @Bean public SecurityFilterChain filterChain( HttpSecurity http, CustomAuthenticationFilter customAut..
Spring Boot 3.1 & Spring Security 6: JWT 검증 리팩토링 (11편)
·
Spring Security
JWT 필터 스프링 시큐리티 코드 리팩토링을 진행했다. 1. JwtAuthorizationFilter 클래스를 리팩토링 했다. 기존에는 한줄로 이루어져있던 코드를 extract method로 각각 역할별로 추출했다. import com.fasterxml.jackson.databind.ObjectMapper; import com.recipia.web.config.security.jwt.TokenUtils; import com.recipia.web.domain.user.constant.RoleType; import com.recipia.web.exception.ErrorCode; import com.recipia.web.exception.RecipiaApplicationException; import io...
Spring Security 6 이해하기: 동작 원리와 보안 기능 탐구
·
Spring Security
스프링 시큐리티가 어떻게 동작하는지 궁금해서 알아봤다. 1. 최상위 필터 인터페이스인 SecurityFilterChain 확인 package org.springframework.security.web; import java.util.List; import jakarta.servlet.Filter; import jakarta.servlet.http.HttpServletRequest; /** * Defines a filter chain which is capable of being matched against an * {@code HttpServletRequest}. in order to decide whether it applies to that request. * * Used to configure a ..
Spring Boot 3.1 & Spring Security 6: 로그인 프로세스 및 JWT 토큰 동작 설명 (10편)
·
Spring Security
지금까지 만든 시큐리티의 동작을 설명하겠다. 1. 로그인 과정 로그인 페이지 접근: 사용자는 웹 브라우저에서 로그인 페이지(/login)에 접근한다. 로그인 페이지에는 사용자 이름과 비밀번호를 입력할 수 있는 폼이 있다. 로그인 요청: 사용자가 자신의 사용자 이름과 비밀번호를 입력하고 "Login" 버튼을 클릭하면, AJAX 요청을 통해 서버에 로그인 요청(/user/login)이 전송된다. 인증 필터 처리: CustomAuthenticationFilter는 로그인 요청을 가로챈다. 이 필터는 UsernamePasswordAuthenticationFilter를 확장하여 구현되었다. 요청에서 사용자 이름과 비밀번호를 추출하여 UsernamePasswordAuthenticationToken 객체를 생성한다...
Spring Boot 3.1 & Spring Security 6: 로그인 & 메인 페이지 컨트롤러 (9편)
·
Spring Security
로그인 뷰를 보여주고 action을 실행시킬 로그인 컨트롤러와 메인페이지를 보여주게될 메인 컨트롤러를 작성한다. 1. @Controller - 로그인, 메인페이지 컨트롤러 1-1. LoginController 이 코드는 사용자의 로그인 및 로그아웃을 관리하는 컨트롤러이다. Spring의 @Controller 어노테이션을 사용하여 웹 요청을 처리하는 클래스임을 나타낸다. @Slf4j @RequiredArgsConstructor @Controller public class LoginController { /** * [View] 로그인 페이지를 엽니다. */ @GetMapping("/login") public String login() { return "login"; } /** * [Action] 로그인 프로..