为spring-securty创建bean

时间:2016-12-02 11:56:10

标签: java spring-security

这里我想为spring安全性创建bean,而且我知道我必须在Role类的List中创建User类,我需要创建List。这是正确的方法还是任何改变。请根据您的专业经验给我建议。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

例如User-Role-Privilege实体:

  @Entity
  public class CmsAccount {
            @Id
            @GeneratedValue
            private Long id;
            @Column(nullable = false, unique = true)
            private String userId;
            @Column(nullable = false)
            private String password;
            private String fullName;
            private String countryCode;
            private String siteCode;
            @ManyToMany(fetch = FetchType.EAGER)
            @JoinTable(
                    name = "cmsuser_roles",
                    joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
                    inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
            private Collection<Role> roles = new ArrayList<>();
            @NotNull
            private Boolean suspended = false;    
  }





 @Entity
  public class Role {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;

        @Column(unique = true)
        private String name;

        @ManyToMany(mappedBy = "roles")
        private Collection<CustomerAccount> customerAccounts;

        @ManyToMany(fetch = FetchType.EAGER)
        @JoinTable(
                name = "roles_privileges",
                joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"),
                inverseJoinColumns = @JoinColumn(name = "privilege_id", referencedColumnName = "id"))
        private Collection<Privilege> privileges = new ArrayList<>();

        public void addCustomer(CustomerAccount customer) {
            customerAccounts.add(customer);
        }

        public void addPrivilege(Privilege priv) {
            privileges.add(priv);
        }

    }



@Entity  
 public class Privilege {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;

        @Column(unique = true)
        private String name;

        public Privilege(String name) {
            this.name = name;
        }    
    }

希望,它会帮助你