Bonjour,

Je suis entrain de réalisé la partie sécurité de mon application réalisé avec spring thymeleaf.

Dans les regle de gestion utilisé, nous avons une relation du type ManyToMany entre l'entité User et roles qui doit donner naissance à une autre entité au niveau de la base de donnée nommées user_roles car un user peut avoir un ou plusieurs rôle.De même un rôle peut être utilisé par un ou plusieurs user.


Voici la structure de mes deux entités:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
 
package com.wangi.aeropro.entites;
 
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
 
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
 
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
 
@Entity(name="users")
public class User implements Serializable {
 
	@Id	
	@NotNull
	@Size(min=3,max=15)
	private String name;
 
	@NotNull
	@Size(min = 4)
	private String password;
	private boolean actived;
 
	@ManyToMany
	@JoinTable(name = "USERS_ROLES")
	private Collection<Role> roles;
 
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}	
 
 
	public User(String name, String password, boolean actived) {
		super();
		this.name = name;
		this.password = password;
		this.actived = actived;
	}
 
	public User(String name) {
		super();
		this.name = name;
	}
 
 
	public String getName() {
		return name;
	}
 
 
	public void setName(String name) {
		this.name = name;
	}
 
 
	public String getPassword() {
		return password;
	}
 
 
	public void setPassword(String password) {
		this.password = password;
	}
 
 
	public boolean isActived() {
		return actived;
	}
 
 
	public void setActived(boolean actived) {
		this.actived = actived;
	}
 
 
	public Collection<Role> getRoles() {
		return roles;
	}
 
 
	public void setRoles(Collection<Role> roles) {
		this.roles = roles;
	}
 
 
}
 
 
package com.wangi.aeropro.entites;
import java.io.Serializable;
import java.util.List;
 
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
 
import org.hibernate.validator.constraints.NotEmpty;
 
@Entity
public class Role implements Serializable{
	@Id
	@NotNull
	@Size(min=3,max=15)
	private String name;
	@NotNull
	@Size(min=5,max=30)
	private String desciption;	
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public Role(String name, String desciption) {
		super();
		this.name = name;
		this.desciption = desciption;
	}
 
	public Role() {
	}
 
	public Role(String name) {
		this.name = name;
	}
 
	public String getDesciption() {
		return desciption;
	}
 
	public void setDesciption(String desciption) {
		this.desciption = desciption;
	}
 
 
}
Ceci nous a permis de réalisé notre couche DAO comprenant nos interfaces:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
package com.wangi.aeropro.dao;
 
 
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
 
import com.wangi.aeropro.entites.User;
 
public interface UserRepository extends JpaRepository<User,String>{
 
 
}
 
 
package com.wangi.aeropro.dao;
 
import java.util.List;
 
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
 
import com.wangi.aeropro.entites.Categorie;
import com.wangi.aeropro.entites.Role;
 
public interface RoleRepository extends JpaRepository<Role,String>{
 
 
}

Je voudrais savoir comment réalisé une interface User-Roles pour alimenter la table qui est générée dans la base de données.