• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

스프링 시큐리티 주요 아키텍처 이해 - 4. 인증 저장소 관련 질문

24.01.15 16:41 작성 조회수 117

0

안녕하세요.

 

Spring Security 강의에서

 

스프링 시큐리티 주요 아키텍처 이해 - 4. 인증 저장소 강의를 듣던 중

 

SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);

 

위를 설정하여 자식 쓰레드도 동일한 Security Context를 공유하는 부분에 대해서 실습하던 중

 

아래와 같이 코드를 작성했는데 아무리 실행해도 계속 thread가 실행되는 컨트롤러에서 authentication 객체가 null 값을 가지는 것을 확인했습니다.

 

"/" 가 실행되는 index 메소드에서

 

String strategy = SecurityContextHolder.getContextHolderStrategy().getClass().getSimpleName();
System.out.println("strategy = " + strategy);

 

위를 통해서 출력 결과를 확인했을 때 "ThreadLocalSecurityContextHolderStrategy" 값이 나오는 것을 확인했습니다. 제가 판단하기로는 적용이 안 된 결과로 보였는데요

 

어느 시점에 setStrategyName를 적용해줘야 적용이 되는지 잘 모르겠습니다.

 

아래는 소스 코드 입니다.

[SecurityCofnig]

package io.security.basicsecurity;


import
org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration;
im
port org.springframework.security.config.Customizer;
i
mport org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import
org.springframework.
security.core.context.SecurityContextHolder;
import
org.springframework
.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig4 {

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {



http
.authorizeHttpRequests(auth -> {
auth
.anyRequest().authenticated();
});

http
.formLogin(Customizer.withDefaults());


SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);

return
http.build();
}
}

 

 

[Controller]

package io.security.basicsecurity;

import
jakarta.servlet.http.HttpSession;
impo
rt org.springframework.security.core.Authentication;
imp
ort org.springframework.security.core.context.SecurityContext;
im
port org.springframework.security.core.context.SecurityContextHolder;
i
mport org.springframework.security.web.context.HttpSessionSecurityContextRepository;
import
org.springframework.web.bind.annotation.GetMapping;
import
org.springframework.web.bind.annotation.RestController;

@RestController
public class SecurityController {

@GetMapping("/")
public String index(HttpSession session) {

String strategy = SecurityContextHolder.getContextHolderStrategy().getClass().getSimpleName();
System.out.println("strategy = " + strategy);

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
SecurityContext context = (SecurityContext) session.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
Authentication authentication1 = context.getAuthentication();
System.out.println("무슨 값? " + authentication1.toString());

return
"home";
}

@GetMapping("/thread")
public String thread() {

new Thread(
new Runnable() {
@Override
public void run() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if
(authentication == null) {
System.out.println("Null 값임");
}
}
}).start();



return
"thread";
}

 

답변 1

답변을 작성해보세요.

0

전체 코드를 깃헙으로 공유 부탁드립니다

실행해봐야 알 것 같습니다