如何使用已经存在于数据库中的另一个命名(配置文件)来自定义表角色(特权)?

时间:2019-01-25 08:59:13

标签: java spring-boot spring-security

我正在开发具有现有数据库和实体的全栈应用程序(spring-boot,reactjs),我面临的问题是关于 User @PreAuthorize(“ hasProfil('),我也必须与之合作的角色角色(雇员个人资料) Admin')“),运行时将其忽略,因为应该是 @PreAuthorize(” hasRole('ROLE_ADMIN')“),如何使Spring Security与员工和配置文件一起工作用户和角色?

这是应该作为用户类的类:

 public class Employe implements Serializable {
        @Id
        @Column(unique=true, nullable=false, precision=6)
        private Long id;
        @JsonIgnore
        @Column(precision=6)
        private BigDecimal cv;
        @Column(length=254)
        private String nom;
        @Column(length=254)
        private String prenom;
        @JsonIgnore
        @Column(length=254)
        private String login;
        @JsonIgnore
        @Column(length=254)
        private String mp;
        @JsonIgnore
        @Column(length=254)
        private String mail;
       @ManyToOne(optional=false)
       @JoinColumn(name="pro_id", nullable=false)
       private Profil profil;

这是应该作为角色的Profil类:

public class Profil implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(nullable = false)
    private Integer id;
    @Size(max = 254)
    @Column(length = 254)
    private String libelle;
    @Size(max = 254)
    @Column(length = 254)
    private String description;
    @Column(name = "lock_flag")
    private Integer lockFlag;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "profil")
    private Collection<Employe> employeCollection;

    public Profil() {
    }

    public Profil(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLibelle() {
        return libelle;
    }

    public void setLibelle(String libelle) {
        this.libelle = libelle;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getLockFlag() {
        return lockFlag;
    }

    public void setLockFlag(Integer lockFlag) {
        this.lockFlag = lockFlag;
    }

    @XmlTransient
    public Collection<Employe> getEmployeCollection() {
        return employeCollection;
    }

    public void setEmployeCollection(Collection<Employe> employeCollection) {
        this.employeCollection = employeCollection;
    }

这是返回UserDetails的方法创建:

  public static UserPrincipal create(Employe user) {
        Profil p = user.getProfil();
        Set<Profil> listProfil= new HashSet<>();
        listProfil.add(p);
        List<GrantedAuthority> authorities = listProfil.stream().map(role ->
                new SimpleGrantedAuthority(role.getLibelle())
        ).collect(Collectors.toList());

        return new UserPrincipal(
              user.getId(),
              user.getNom(),
              user.getPrenom(),
              user.getNumCin(),
              user.getLogin(),
              user.getMail(),
              user.getMp(),
              user.getTelephone(),
              authorities
        );
    }

CustomUserDetailsS​​ervice类:

@Service
public class CustomUserDetailsService implements UserDetailsService {
    @Autowired
    EmployeRepository employeRepository; 
    private static final Logger logger = LoggerFactory.getLogger(CustomUserDetailsService.class);

    @Override
    @Transactional
    public UserDetails loadUserByUsername(String loginOrEmail) throws UsernameNotFoundException {
        // Let people login with either username or email

        Employe user = employeRepository.findByLoginOrMail(loginOrEmail, loginOrEmail).orElseThrow(() -> {
            logger.error("Utilisateur non trouvé");
            return new UsernameNotFoundException("User not found with username or email : " + loginOrEmail);
        });

        return UserPrincipal.create(user);
    }

    @Transactional
    public UserDetails loadUserById(Long id) {
        Employe user = employeRepository.findById(id).orElseThrow(() -> new ResourceNotFoundExceptionn("User", "id", id));

        return UserPrincipal.create(user);
    }

0 个答案:

没有答案