在Spring引导安全性中从数据库访问多个角色

时间:2017-06-22 06:41:56

标签: spring spring-boot spring-security roles

我有3个表USERS(有用户详细信息),ROLES(有角色的详细信息),USER_ROLES(将user_id映射到role_id)。我想检索用户的角色。当我将它们作为实体导入时,我的代码如下所示:

User.java

package com.eaiesb.entities;

import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.List;


/**
 * The persistent class for the USERS database table.
 * 
 */
@Entity
@Table(name="USERS")
@NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.TABLE)
    @Column(name="USER_ID")
    private String userId;

    private String column1;

    private String column2;

    private String emailid;

    private String gender;

    private String name;

    private String password;

    private BigDecimal sno;

    private String status;

    //bi-directional many-to-many association to Role
    @ManyToMany
    @JoinTable(
        name="USER_ROLES"
        , joinColumns={
            @JoinColumn(name="USER_ID")
            }
        , inverseJoinColumns={
            @JoinColumn(name="ROLE_ID")
            }
        )
    private List<Role> roles;

    public User() {
    }

    public String getUserId() {
        return this.userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getColumn1() {
        return this.column1;
    }

    public void setColumn1(String column1) {
        this.column1 = column1;
    }

    public String getColumn2() {
        return this.column2;
    }

    public void setColumn2(String column2) {
        this.column2 = column2;
    }

    public String getEmailid() {
        return this.emailid;
    }

    public void setEmailid(String emailid) {
        this.emailid = emailid;
    }

    public String getGender() {
        return this.gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public BigDecimal getSno() {
        return this.sno;
    }

    public void setSno(BigDecimal sno) {
        this.sno = sno;
    }

    public String getStatus() {
        return this.status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List<Role> getRoles() {
        return this.roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

}

“Role.java”看起来也很相似。我想在UserDetails的getAuthoritites()中添加用户的角色,但我无法,因为它需要String但我只能将角色作为List类型。

尝试使用常规Java方法转换为String,但没有任何帮助。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您的方法<group recstatus="1"> <sourcedid> <source>system_owner_28f57240-5e2b-44af-8e62-fbf9aa6b6165</source> <id>basic_groups_69782a81-042d-4717-b9e4-18abacb306b7</id> </sourcedid> <grouptype> <scheme>Unique</scheme> <typevalue level="0">RECORD</typevalue> </grouptype> <description> <short>Ipsum dipsum</short> </description> <relationship> <sourcedid> <source>system_owner_28f57240-5e2b-44af-8e62-fbf9aa6b6165</source> <id>69782a81-042d-4717-b9e4-18abacb306b7</id> </sourcedid> <label>Ipsum dipsum</label> </relationship> </group> 应如下所示:

getAuthorities(User)