// UserDetails userDetails = UserDetailsService.loadUserByUsername("user");
UserDetails userDetails = org.springframework.security.core.userdetails.User
.withUsername("user")
.password("password")
.passwordEncoder(s -> PasswordEncoderFactories.createDelegatingPasswordEncoder().encode(s))
.roles("USER")
.build();
if (userDetails != null) {
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authentication);
}
正常情况下,UserDetails
通过UserDetailsService.loadUserByUsername("")
获取,上述代码可用于:
1、模拟用户登录
2、接口调试时为用户授权
3、业务之外的虚拟授权
4、and so on...
本文由 新逸Cary 创作,如果您觉得本文不错,请随意赞赏
采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
原文链接:https://blog.xinac.cn/archives/spring-security-authentication.html
最后更新:2022-07-22 10:05:56
Update your browser to view this website correctly. Update my browser now