![](/img/trans.png)
[英]Using Spring security 3 to authenticate against REST a user only with username
[英]Using password from spring security to authenticate REST call
我有一个带有Spring Security和LDAP认证集成的spring boot Web应用程序。 该Web应用程序内部进行REST调用。 这些REST调用具有用户名密码验证。 该用户名密码与Spring Security使用的相同。 无论如何,我可以获得Spring Security认证的用户名密码,以便在REST调用中使用。 如果不是这种方式,还有其他方法可以实现。
提前致谢。
我认为有一种非常不错的方式适合您的情况。
默认情况下,在进行身份验证后,Spring Security不会将密码存储在内存中,因此您需要更改密码。 使用Java config,添加configure(AuthenticationManagerBuilder)
方法:
auth.eraseCredentials(false);
然后,您可以使用以下命令获取当前用户的用户名和密码:
String username = SecurityContextHolder.getContext().getAuthentication().getName();
Object rawPassword = SecurityContextHolder.getContext().getAuthentication().getCredentials();
Spring Security是根据安全属性中的规则执行的。
这意味着您只需要启用spring-security,唯一的问题是,如果未获得授权,它将进入一个Restful客户端无法理解的“未授权”页面。 但是,如果Restful客户端已通过身份验证并被授予有效会话,则它将能够通过Security_check并访问受保护的页面。
我猜想Spring安全性就像AOP一样工作,所以每个受保护的页面都有一个Security_check横切面,如果存在身份验证,该横切面仅允许访问该页面。
无论如何,我解决了编写自定义AuthenticationProvider的问题,该自定义AuthenticationProvider将执行LDAP身份验证并为将来的REST调用获取用户名密码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.