@WithUserDetails似乎不起作用

@WithUserDetails似乎不起作用,第1张

@WithUserDetails似乎不起作用

我目前无法测试,但这是一个 可能的 解决方案。

查看@WithUserDetails实现:

@WithSecurityContext(factory = WithUserDetailsSecurityContextFactory.class)public @interface WithUserDetails {    ...}final class WithUserDetailsSecurityContextFactory implements        WithSecurityContextFactory<WithUserDetails> {    private BeanFactory beans;    @Autowired    public WithUserDetailsSecurityContextFactory(BeanFactory beans) {        this.beans = beans;    }    public SecurityContext createSecurityContext(WithUserDetails withUser) {        String beanName = withUser.userDetailsServiceBeanName();        UserDetailsService userDetailsService = StringUtils.hasLength(beanName)     ? this.beans.getBean(beanName, UserDetailsService.class)     : this.beans.getBean(UserDetailsService.class);        String username = withUser.value();        Assert.hasLength(username, "value() must be non empty String");        UserDetails principal = userDetailsService.loadUserByUsername(username);        Authentication authentication = new UsernamePasswordAuthenticationToken(     principal, principal.getPassword(), principal.getAuthorities());        SecurityContext context = SecurityContextHolder.createEmptyContext();        context.setAuthentication(authentication);        return context;    }}

您可以按照相同的模式创建您选择的安全上下文

@Target({ElementType.METHOD, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Inherited@documented@WithSecurityContext(factory = WithoutUserFactory.class)public @interface WithoutUser {}public class WithoutUserFactory implements WithSecurityContextFactory<WithoutUser> {    public SecurityContext createSecurityContext(WithoutUser withoutUser) {        return SecurityContextHolder.createEmptyContext();    }}

其他可获得的注释

WithAnonymousUser
WithMockUser
WithSecurityContext
(和
WithUserDetails



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5641551.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存