새소식

반응형
WEB/Spring Security

[Spring Security] SecurityContextHolder와 사용자 정보

  • -
반응형

2023-01-24


사진: Unsplash 의 Ali Kazal


1. 정의

 

SecurityContextHolder는 기본적으로 security context에 접근하는 것을 도와주는 클래스라고 볼 수 있다. 또한 해당 클래스를 호출하여 실행할 때마다 동일 스레드에서 가져온다. 추가적으로 Spring Security 가 자체적으로 메모리를 관리하기 때문에 메모리 누수를 걱정하지 않아도 된다고 한다. 이제 SecurityContextHolder 가 어떻게 사용자 정보를 가져오는지 보자.


2. 방법

 

아래의 코드는 현재 로그인한 유저 정보를 가져오는 구체적인 코드이다. 해당코드에서  SecurityContextHolder 는 security context의 정보를 가져와 security context 가 가지고 있는 사용자의 정보를 조회하여 변수에 할당하는 것을 보여준다.

 

Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
 
if (principal instanceof UserDetails) {
  String username = ((UserDetails)principal).getUsername();
} else {
  String username = principal.toString();
}

 

여기서 getPrincipal() 메서드는 UserDetail를 리턴하게 되는데 이 UserDetail에 로그인한 사용자 정보가 있어 정보를 얻을 수 있다. 다만 위의 코드는 Spring에서 추구하는 의존관계주입에 있어서는 좋은 코드처럼 보이지 않는다. Controller에서는 어떤 방법으로 사용자의 정보를 받아올 수 있는지 보자.


https://javarevisited.blogspot.com/2018/02/what-is-securitycontext-and-SecurityContextHolder-Spring-security.html#axzz7rHiyt7an

 

MVC 패턴 에서는 아래와 같이 직접 request 객체로 받아 처리할 수 있다.

 

import java.security.Principal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
 
@Controller
public class MVCController {
 
  @RequestMapping(value = "/username", method = RequestMethod.GET)
  @ResponseBody
  public String currentUserName(Principal principal) {
     return principal.getName();
  }
 
}

import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
 
@Controller
public class SpringMVCController {
 
  @RequestMapping(value = "/username", method = RequestMethod.GET)
  @ResponseBody
  public String currentUserName(Authentication authentication) {
     return authentication.getName();
  }
}

 

Principal 객체와 Authentication 객체를 통해서도 사용자 정보를 받아올 수 있으며, 현재의 프로젝트 구조에 따라 커스텀에서 사용하면 된다.


출처 : https://javarevisited.blogspot.com/2018/02/what-is-securitycontext-and-SecurityContextHolder-Spring-security.html#axzz7rHiyt7an

 

What is SecurityContext and SecurityContextHolder in Spring Security?

A blog about Java, Programming, Algorithms, Data Structure, SQL, Linux, Database, Interview questions, and my personal experience.

javarevisited.blogspot.com

https://www.javacodegeeks.com/2018/02/securitycontext-securitycontextholder-spring-security.html

 

What is SecurityContext and SecurityContextHolder in Spring Security? - Java Code Geeks - 2023

The SecurityContext and SecurityContextHolder are two fundamental classes of Spring Security. The SecurityContext is used to store the details of the

www.javacodegeeks.com


메인 이미지 출처 : 사진: UnsplashAli Kazal  

반응형
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.