[英]Password and username in distinct collections (Spring security)
我正在嘗試通過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。 那么,如何訪問密碼?
提前致謝 !
您不能在實體類中的spring組件上使用@Autowired
作為字段。 這樣使用;
private transient UserPassService userPassService;
並添加此方法的setter方法;
@Autowired
public void setUserPassService(UserPassService userPassService) {
this.userPassService = userPassService;
}
注意使用new
創建Entity
類會使此服務為null。 如果使用new
創建,請使用與UserPassService自動關聯的對象調用setUserPassService
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.