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

我有一个使用Spring Security保护的Spring MVC Web应用程序,我正在编写测试.我很难在SpringContextHolder中获取Spring Security检索到的一个(自定义)用户.
一旦我用户被“插入”(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”调用发送给他的内容的问题但是必须有一个……
无论如何,我可能在这里做错了什么,任何想法?

最佳答案
像建议的那样,我最终编写了一个自定义的InMemoryUserDetailsmanager,我用它提供了我的Spring Security配置.
对于任何想知道的人来说,这似乎是唯一的方法.

大佬总结

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

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

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