Project Reactor로 실행되는 백그라운드 작업이 있는데, @PreAuthorize를 통과하기 위해 인증 된 사용자와 함께 실행해야하는 관련 작업이 있습니다. 주석이 달린 메소드.스프링 부트 및 스프링 보안을 사용하여 백그라운드 작업 인증
나는이 같은 일을 해요 :
Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(login, password));
SecurityContextHolder.getContext().setAuthentication(authentication);
을하지만 AuthenticationManager에 전화로 추적 할 때, 나는 봄 부팅의 기본 InMemoryUserDetailsService보다는 내 사용자 정의 인증 구성을 사용하고 있는지 찾을 수 있습니다. 이것은 웹 요청 스레드에서 또는 백그라운드 스레드에서 인증을 실행하는지 여부에 관계없이 발생합니다.
가 관련된 경우 나도 몰라,하지만 난 (다른 사람의 사이에서)이 주석으로, 통합 테스트에서이 코드를 실행하고 있습니다 :
이 문제 외에도@SpringApplicationConfiguration(classes=MyAppConfiguration.class)
@WebAppConfiguration
@IntegrationTest({"server.port:0"})
, 내 테스트가한다 인증 된 웹 요청을 내 서버에 보내고 그 인증은 정상적으로 처리됩니다. 그래서 적어도 내 시스템의 웹 부분이 올바른 인증 구성을 사용하고 있다는 것을 알고 있습니다. 그것은 테스트 implementatiton없이 말하기 어렵다하지만 당신은 통합 테스트에서 실행되는 중요한
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(jsr250Enabled=true, prePostEnabled=true)
public abstract class BaseSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public LocalUserDetailsService localUserDetailsService;
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(localUserDetailsService);
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable().httpBasic()
.and()
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
}
안녕하십니까, 응답 해 주셔서 감사합니다. 이것은 두 가지 이유 때문에 도움이되지 않습니다. 1) 저는'MockMvc'을 사용하지 않고 있습니다. 실제 서버를 실행하고 있으며 Spring의'HttpEntity' API를 사용하여 실제 HTTP 호출을하고 있습니다. 2) 실제 HTTP 요청은 정상적으로 작동하고 올바르게 인증됩니다. 잘못된 인증 구성이나 잘못된 전체 Spring 컨텍스트를 얻는 백그라운드 스레드입니까? – JBCP
여기 스프링 컨텍스트 그 자체로 다루지 않는다고 생각합니다. SecurityContext는 보안 컨 스트레인 트를 스레드에게 제시합니다. 배경에 다른 스레드가 있으면 자체 securityContext를 가지므로 서로 다른 제약 조건 집합 – mariubog
에서 작동합니다. 해당 스레드 내의 SecurityContextHolder에 액세스하여 수정을 시도하거나 테스트 전용 인 경우 보안 전략을 전역 적으로 지정하는 GlobalSecurityContextHolderStrategy를 사용할 수 있습니다 – mariubog