Spring Security JWT 13 : CORS 설정
·
Spring Security/JWT
자료CORS | 스프링 시큐리티 공식 자료 바로가기 CORS :: Spring SecuritySpring Framework provides first class support for CORS. CORS must be processed before Spring Security, because the pre-flight request does not contain any cookies (that is, the JSESSIONID). If the request does not contain any cookies and Spring Security isdocs.spring.io CORS란교차 출처 리소스 공유 위키피디아 바로가기 교차 출처 리소스 공유 - 위키백과, 우리 모두의 백과사전위키백과, 우리 모두의 백..
Spring Security JWT 12 : 세션 정보
·
Spring Security/JWT
JWTFilter를 통과한 뒤 세션 확인@Controller@ResponseBodypublic class MainController { @GetMapping("/") public String mainP() { String name = SecurityContextHolder.getContext().getAuthentication().getName(); return "Main Controller : "+name; }}세션 현재 사용자 아이디SecurityContextHolder.getContext().getAuthentication().getName();세션 현재 사용자 roleAuthentication authentication = SecurityContextHol..
Spring Security JWT 11 : JWT 검증 필터
·
Spring Security/JWT
JWT 검증 필터스프링 시큐리티 filter chain에 요청에 담긴 JWT를 검증하기 위한 커스텀 필터를 등록해야 한다. 해당 필터를 통해 요청 헤더 Authorization 키에 JWT가 존재하는 경우 JWT를 검증하고 강제로 SecurityContextholder에 세션을 생성한다. (이 세션은 STATELESS 상태로 관리되기 때문에 해당 요청이 끝나면 소멸된다.)JWTFilter 구현JWTFilterpublic class JWTFilter extends OncePerRequestFilter { private final JWTUtil jwtUtil; public JWTFilter(JWTUtil jwtUtil) { this.jwtUtil = jwtUtil; } @O..
Spring Security JWT 10 : 로그인 성공 JWT 발급
·
Spring Security/JWT
로그인 성공7~9 강을 통해 로그인 로직, JWTUtil 클래스를 생성하였다. 이제 로그인이 성공했을 경우 JWT를 발급하기 위한 구현을 진행한다.JWTUtil 주입LoginFilter : JWTUtil 주입public class LoginFilter extends UsernamePasswordAuthenticationFilter { private final AuthenticationManager authenticationManager; //JWTUtil 주입 private final JWTUtil jwtUtil; public LoginFilter(AuthenticationManager authenticationManager, JWTUtil jwtUtil) { this.auth..
Spring Security JWT 9 : 발급 및 검증 클래스
·
Spring Security/JWT
JWT 발급과 검증로그인시 -> 성공 -> JWT 발급접근시 -> JWT 검증JWT에 관해 발급과 검증을 담당할 클래스가 필요하다. 따라서 JWTUtil이라는 클래스를 생성하여 JWT 발급, 검증 메소드를 작성한다.JWT생성 원리JWT.IO 공식 홈페이지https://jwt.io/ JWT.IOJSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.jwt.io문자열 형태가 있는데 . 을 기준으로 Header.Payload.Signature 구분 -> 외부에 데이터 전달. JWT는 Header.Payload.Signature 구조로 이루어져 있다. 각 요소는..
Spring Security JWT 8 : DB기반 로그인 검증 로직
·
Spring Security/JWT
DB 기반 로그인 검증이번에는 DB에서 AuthenticationManager까지 로직을 구현한다. 구현은 UserDetails, UserDetailsService, UserRepository의 회원 조회 메소드를 진행한다.UserRepositoryUserRepositorypackage com.example.springjwt.repository;import com.example.springjwt.entity.UserEntity;import org.springframework.data.jpa.repository.JpaRepository;public interface UserRepository extends JpaRepository { Boolean existsByUsername(String usern..
Spring Security JWT 7 : 로그인 필터 구현
·
Spring Security/JWT
자료스프링 시큐리티 필터 참고 자료https://docs.spring.io/spring-security/reference/servlet/architecture.html Architecture :: Spring SecurityThe Security Filters are inserted into the FilterChainProxy with the SecurityFilterChain API. Those filters can be used for a number of different purposes, like exploit protection,authentication, authorization, and more. The filters are executed in a specidocs.spring.io로그인..
Spring Web MVC - 모델(Model) 폼 데이터 처리
·
Spring
Spring MVC에서 폼 데이터를 처리하는 다양한 방식을 통해 모델을 이해MVC 패턴 이해Model: 데이터와 비즈니스 로직을 담당. 컨트롤러에서 데이터를 준비하여 뷰로 전달.View: 사용자 인터페이스(UI)를 담당. Model 데이터를 바탕으로 화면 렌더링.Controller: 사용자의 요청을 처리하고, 적절한 데이터를 준비하여 뷰를 반환.폼 데이터 흐름클라이언트가 HTML 폼 데이터를 전송.컨트롤러가 데이터를 수신하고 처리.데이터를 뷰로 전달하여 사용자에게 결과를 반환.수업코드package com.example.model.controller;import com.example.model.dto.FormTO;import jakarta.servlet.http.HttpServletRequest;impor..
Spring Web MVC - 세션(Session)
·
Spring
https://jakarta.ee/specifications/servlet/5.0/apidocs/jakarta/servlet/http/httpsession HttpSession (Jakarta Servlet API documentation)public interface HttpSession Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user. The servlet container uses this interface to create a session between an HTTP client and an HTjakarta...
Spring Web MVC - 쿠키(Cookie)
·
Spring
https://jakarta.ee/specifications/servlet/5.0/apidocs/jakarta/servlet/http/cookie Cookie (Jakarta Servlet API documentation)All Implemented Interfaces: Serializable, Cloneable public class Cookie extends Object implements Cloneable, Serializable Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the sjakarta.ee 1. 쿠키란?..