java - 如何从Spring Security获取密码?

标签 java spring apache spring-security cxf

我正在使用带有 WebClient 的 Apache CXF,并且我想将我的凭据发送到服务器

    WebClient client = WebClient.create("http://localhost:8084", "username", "password", null)

问题是我无法获取密码值。

以下方法无效

    SecurityContextHolder.getContext().getAuthentication().getCredentials()

最佳答案

假设您已登录,并且需要获取从数据库中检索到的公司名称。首先,您需要实现 UserDetails 的对象,该对象将保存在 UserDetailsS​​ervice 中并添加您需要的任何变量(公司等)。

public class CustomUserDetails implements UserDetails{      
    private String password;
    private String username;
    private String companyName;
}

与使用自定义 Userdetails 实现进行类型转换相比。

CustomUserDetails customDetails (CustomUserDetails)SecurityContextHolder.getContext().getAuthentication().getCredentials();

getCredentials 将返回您保存到凭证的对象。无论输入什么类型,您都可以将其返回到您保存的类(class)。

关于java - 如何从Spring Security获取密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27176208/

相关文章:

java - 使用 @PreAuthorize 时 Spring SpelExpression 似乎没有读取我的 bean

node.js - 使用 htaccess 重定向到 nodejs 应用程序

windows - Tomcat 服务因两个已部署的应用程序而停止

java - 将 int 从 .txt 传输到数组的更有效方法

java - 我的二进制文件中缺少一个 "0"- 我该如何修复它?

spring - 使用 mapstruct 将嵌套的 DTO 对象映射到单个项目列表

java - 找不到媒体类型=应用程序/xml 的 MessageBodyWriter

apache - 在Htaccess中在Mod_rewrite之前运行ErrorDocument

Java:从方法返回数组

java - 按主要词分组