简体   繁体   English

不同集合中的密码和用户名(春季安全性)

[英]Password and username in distinct collections (Spring security)

I'm trying to implement authentication with Spring Security and my problem is that I'm using a MongoDB base where the username and the associated password are in two distinct collections. 我正在尝试通过Spring Security实现身份验证,我的问题是我使用的是MongoDB数据库,其中用户名和关联的密码位于两个不同的集合中。 So when I implement UserDetails, I can't return the password properly. 因此,当我实现UserDetails时,无法正确返回密码。 Here is what I've tried: 这是我尝试过的:

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;
    }
}

Unfortunately, I can't access my UserPassService from an entity like this. 不幸的是,我无法从这样的实体访问UserPassService。 So how can I access the password ? 那么,如何访问密码?

Thanks in advance ! 提前致谢 !

You cant use @Autowired on spring components in entity class as a field. 您不能在实体类中的spring组件上使用@Autowired作为字段。 Use like this ; 这样使用;

private transient UserPassService userPassService;

and add setter method of this ; 并添加此方法的setter方法;

@Autowired
public void setUserPassService(UserPassService userPassService) {
    this.userPassService = userPassService;
}

Beaware of creatation of Entity class with new is making this service is null. 注意使用new创建Entity类会使此服务为null。 If you create with new , call setUserPassService method with autowired object of UserPassService. 如果使用new创建,请使用与UserPassService自动关联的对象调用setUserPassService方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM