Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

java - Creating beans for spring-securty

Here I want to create beans for spring security, and as I know I have to create User class in that List in Role class I need to create List. Is this the correct way or any change.Give me suggestions with your professional experience. Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For example User-Role-Privilege entities:

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

Hope, it will help you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...