目前我正在使用 Spring Security 编写一个 Web 应用程序。我们有一个网络服务,可以通过用户名和密码对用户进行身份验证。
网络服务:
String[] login(String username, String password);
如何配置 Spring Security 以将提供的用户名和密码传递给 Web 服务?
我写了一个只接收用户名的 UserDetailsService
。
我认为问题出在您的 xml 上。你关闭自动配置了吗?您的类是否扩展了 AbstractUserDetailsAuthenticationProvider?
最佳答案
扩展 org.acegisecurity.providers.dao.AbstractUserDetailsAuthenticationProvider
/**
* @author rodrigoap
*
*/
public class WebServiceUserDetailsAuthenticationProvider extends
AbstractUserDetailsAuthenticationProvider {
@Override
protected UserDetails retrieveUser(String username,
UsernamePasswordAuthenticationToken authentication)
throws AuthenticationException {
//Improve this line:
String password = authentication.getCredentials().toString();
// Invoke your webservice here
GrantedAuthority[] grantedAuth = loginWebService.login(username, password);
// create UserDetails. Warning: User is deprecated!
UserDetails userDetails = new User(username, password, grantedAuth);
return userDetails;
}
}
关于java - 使用需要用户名和密码的 Web 服务通过 Spring Security 对用户进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1171103/