SecurityContextHolder
用来存储一次访问的用户信息
SecurityContextHolder
通过ThreadLocal
来实现,所以是线程安全的
SecurityContextHolder
包含了SecurityContext
,而SecurityContext
包含Autherntication
.
SecurityContext context = SecurityContextHolder.createEmptyContext();
Authentication authentication = new TestingAuthenticationToken("username", "password", "ROLE_USER");
context.setAuthentication(authentication);
SecurityContextHolder.setContext(context);
参考:
https://docs.spring.io/spring-security/site/docs/5.4.5/reference/html5/#servlet-authentication-securitycontextholder