【发布时间】:2016-10-21 09:18:10
【问题描述】:
我正在使用 Spring 安全性和注释通过数据库和 Ldap 对用户进行身份验证。 详细地说,由于 Ldap 不允许检索属性,我通过 Ldap 搜索检查用户(唯一代码)和密码是否正确,然后使用我的数据库加载权限。因此,我数据库中的所有用户都存在于 Ldap 中,但如果用户存在于 Ldap 中而不是我的数据库中,我将显示一个特定页面。 这是实际代码:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
@PropertySource(value = { "classpath:application.properties" })
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
AuthenticationConfiguration authenticationConfiguration;
@Configuration
protected static class AuthenticationConfiguration implements
AuthenticationProvider {
@Autowired
private UserServices userServices;
@Autowired
LdapServices ldapServices;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
String name = authentication.getName();
String password = authentication.getCredentials().toString();
boolean isFind = ldapServices.ldapSearch(name, password);
if (isFind){
com.domain.User user = userServices.getByUsersEnabled(name);
if (user!=null)
authorities.add(new SimpleGrantedAuthority("ROLE_"+user.getRole().getRole()));
return new UsernamePasswordAuthenticationToken(name, password, authorities);
}
else return null;
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
@Autowired
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationConfiguration);
}
...web services authentication
我有一个简单的用户,我想添加一些信息,例如姓名和电子邮件。我读到我必须实现UserDetailsService 接口的UserDetails 和loadUserByUsername,但是如何将loadUserByUsername 与我的代码合并?通过这种方式,我可以显示姓名而不是用户代码。谢谢
【问题讨论】:
标签: java spring spring-mvc authentication spring-security