【发布时间】:2018-10-09 08:32:26
【问题描述】:
我正在尝试使用 Spring Security 实现身份验证,我的问题是我使用的是 MongoDB 基础,其中用户名和关联的密码位于两个不同的集合中。所以当我实现 UserDetails 时,我无法正确返回密码。这是我尝试过的:
package com.example.springmongo.user;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
@Document(collection = "users")
public class User implements UserDetails {
/**
*
*/
private static final long serialVersionUID = -2217225560457250699L;
@Autowired
private UserPassService userPassService;
@Id
private String id;
@Field(value = "iduser")
private Long iduser;
@Field(value = "name_complete")
private String name_complete;
@Field(value = "mail")
private String mail;
@Field(value = "active")
private Double active;
@Field(value = "creationDate")
private String creationDate;
@Field(value = "last_login")
private String last_login;
public User() {
super();
}
public User(String id, Long iduser, String name_complete, String mail, Double active, String creationDate, String last_login) {
super();
this.id = id;
this.iduser = iduser;
this.name_complete = name_complete;
this.mail = mail;
this.active = active;
this.creationDate = creationDate;
this.last_login = last_login;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Long getIduser() {
return iduser;
}
public void setIduser(Long iduser) {
this.iduser = iduser;
}
public String getName_complete() {
return name_complete;
}
public void setName_complete(String name_complete) {
this.name_complete = name_complete;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public Double getActive() {
return active;
}
public void setActive(Double active) {
this.active = active;
}
public String getCreationDate() {
return creationDate;
}
public void setCreationDate(String creationDate) {
this.creationDate = creationDate;
}
public String getLast_login() {
return last_login;
}
public void setLast_login(String last_login) {
this.last_login = last_login;
}
@Override
public String toString() {
return "name: " + this.name_complete + ", mail: " + this.mail + ", id: " + this.id;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getPassword() {
return this.userPassService.getUserPassword(this.iduser);
}
@Override
public String getUsername() {
return this.getMail();
}
@Override
public boolean isAccountNonExpired() {
if (this.getActive() != null && this.getActive().equals(1.0)) {
return true;
}
return false;
}
@Override
public boolean isAccountNonLocked() {
if (this.getActive() != null && this.getActive().equals(1.0)) {
return true;
}
return false;
}
@Override
public boolean isCredentialsNonExpired() {
if (this.getActive() != null && this.getActive().equals(1.0)) {
return true;
}
return false;
}
@Override
public boolean isEnabled() {
if (this.getActive() != null && this.getActive().equals(1.0)) {
return true;
}
return false;
}
}
很遗憾,我无法从这样的实体访问我的 UserPassService。那么如何获取密码呢?
提前致谢!
【问题讨论】:
-
为什么需要密码?通常,很少有特定操作需要此哈希,并且在大多数情况下,元组将传递给 UsernamePasswordAuthenticationFilter 等。
-
authentificationProvider使用getPassword()函数进行登录。
标签: java spring mongodb spring-security