我在Spring中创建身份验证服务。
我使用UserDetailsService来获取表单变量,但是我发现loadUserByUsername只有一个变量-- userName。
如何获取密码?
public class userAuthentication implements UserDetailsService{
private @Autowired
ASPWebServicesUtils aspWebServicesUtils;
@Override
public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
//how to get password ?
User user = new User("test", "test", true, true, true, true, getAuthorities(true));
return user;
}
private List<GrantedAuthority> getAuthorities(boolean isAdmin){
List<GrantedAuthority> authorityList = new ArrayList<GrantedAuthority>(2);
authorityList.add(new SimpleGrantedAuthority("USER_ROLE"));
if(isAdmin){
authorityList.add(new SimpleGrantedAuthority("ADMIN_ROLE"));
}
return authorityList;
}
//...
}谢谢
发布于 2013-02-18 22:56:19
如果查看User对象,则构造函数中的第二个参数是密码。
UserDetailsService用于从后端结构(如数据库)加载用户。当用户尝试使用用户名和密码登录时,将调用loadUserByUsername方法,然后该服务负责加载用户定义并将其返回给安全框架。所需的详细信息包括username、password、accountNonExpired、credentialsNonExpired、accountNonLocked和authorities等数据。
spring security收到用户对象后,将根据用户输入的密码和其他数据(如用户帐户状态(accountNonExpired、credentialsNonExpired等))验证用户
发布于 2015-09-29 04:03:21
检索用户信息和提供身份验证信息的一些标准(开箱即用)机制包括:
如果以上方法不能满足您的需求,并且您需要一个自定义的解决方案,您可以创建并配置一个新的身份验证提供者,如下所示:
安全配置:
@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(new CustomAuthenticationProvider());
}
....
}身份验证提供程序:
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
String name = authentication.getName();
// You can get the password here
String password = authentication.getCredentials().toString();
// Your custom authentication logic here
if (name.equals("admin") && password.equals("pwd")) {
Authentication auth = new UsernamePasswordAuthenticationToken(name,
password);
return auth;
}
return null;
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}发布于 2013-02-18 22:50:32
我相信UserDetailsService应该用于从一些后端存储、数据库、平面文件等获取UserDetails对象。一旦拥有了UserDetails,spring security (或您)必须将其与用户提供的用户名(或其他主体)和密码(凭据)进行比较,以便对该用户进行身份验证。
我认为你没有按照预期的方式使用它。
https://stackoverflow.com/questions/14938823
复制相似问题