我正在使用带有 WebClient 的 Apache CXF,并且我想将我的凭据发送到服务器
WebClient client = WebClient.create("http://localhost:8084", "username", "password", null)
问题是我无法获取密码值。
以下方法无效
SecurityContextHolder.getContext().getAuthentication().getCredentials()
最佳答案
假设您已登录,并且需要获取从数据库中检索到的公司名称。首先,您需要实现 UserDetails 的对象,该对象将保存在 UserDetailsService 中并添加您需要的任何变量(公司等)。
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/