程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用Spring Security通过InMemoryAuthentication记录自定义用户?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何使用Spring Security通过InMemoryAuthentication记录自定义用户??

开发过程中遇到如何使用Spring Security通过InMemoryAuthentication记录自定义用户?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何使用Spring Security通过InMemoryAuthentication记录自定义用户?的解决方法建议,希望对你解决如何使用Spring Security通过InMemoryAuthentication记录自定义用户?有所启发或帮助;

像建议的那样,我最终编写了一个自定义InMemoryUserDetailsManager,并以此作为我的Spring Security配置的输入。 对任何想知道的人来说,这似乎是唯一的方法。

解决方法

我有一个受Spring Security保护的Spring MVC Web应用程序,并且我正在编写测试。我正在努力让Spring
Security在其SecurityContextHolder中检索我的(自定义)用户之一。一旦我的用户通过以下方式“插入”(java配置):

auth.inMemoryAuthentication().getUserDetailsService().createUser(myCustomUser);

然后,我可以创建相关令牌(UsernamePasswordAuthenticationToken),并要求Spring使用此令牌对我的用户进行身份验证。问题是Spring不会检索
自定义
用户实例,而是其User类的实例。当Spring通过以下方法(从Spring的InMemoryUserDetailsManager中)寻找这样的用户时:

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserDetails user = users.get(username.toLowerCase());

        if (user == null) {
            throw new UsernameNotFoundException(username);
        }

        return new User(user.getUsername(),user.getPassword(),user.isEnabled(),user.isAccountNonExpired(),user.isCredentialsNonExpired(),user.isAccountNonLocked(),user.getAuthorities());
    }

它使用我的配置提供的详细信息实例化一个新用户。

我看不到InMemoryUserDetailsManager直接返回通过“
getUserDetailsS​​ervice()。createUser”调用发送给他的内容的问题,但是可能必须有一个……无论如何,我可能在这里做错了任何想法?

大佬总结

以上是大佬教程为你收集整理的如何使用Spring Security通过InMemoryAuthentication记录自定义用户?全部内容,希望文章能够帮你解决如何使用Spring Security通过InMemoryAuthentication记录自定义用户?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: