작업이 비동기 적으로 작동하므로 사용자의 세션에 액세스 할 수 없습니다. 작업이 사용자 세션에 액세스 할 수 있도록 솔루션을 찾아야합니다 (사용자가 아직 그 순간 로그인 한 경우).스프링 보안 : 비동기 작업에서 인증 된 사용자에게 액세스하는 방법
사용자 세션
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
public class UserPrincipal implements UserDetails {
private User user;
private Collection<SimpleGrantedAuthority> grantedAuthorities;
public UserPrincipal(User user) {
Assert.notNull(user);
this.user = user;
Set<SimpleGrantedAuthority> authorities = new LinkedHashSet<>();
for (Role role : user.getRoles()) {
authorities.add(new SimpleGrantedAuthority(role.getName().toUpperCase(Locale.ENGLISH)));
}
grantedAuthorities = Collections.unmodifiableCollection(authorities);
}
}
추상 작업 클래스
public abstract class Job implements Runnable {
protected Logger logger = LoggerFactory.getLogger(getClass());
protected Job() {
}
@Override
public final void run() {
logger.debug("starting work");
/* code goes here */
logger.debug("work is done");
}
}
작업 클래스
@Component
@Scope(value = "prototype")
public class ProcessLoggingJob extends Job {
@Override
protected void work(Map<String, Object> context) throws Exception {
// need to access user session here
}
, 당신은 acheive하려고하는 어떤 세션 – BlackJoker
에서 기록 로그인 정보] 어떤 HTTP 세션의 생성을 추적하고 파괴하는 HttpSessionListener을 구현을? – Akshay
정답은 여기에있다 : http://stackoverflow.com/questions/5246428/spring-security-and-async – Ritesh