File tree 1 file changed +22
-0
lines changed
src/main/java/com/bezkoder/springjwt
1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change 1
1
package com .bezkoder .springjwt ;
2
2
3
+ import com .bezkoder .springjwt .models .ERole ;
4
+ import com .bezkoder .springjwt .models .Role ;
5
+ import com .bezkoder .springjwt .repository .RoleRepository ;
6
+ import org .springframework .beans .factory .annotation .Autowired ;
3
7
import org .springframework .boot .SpringApplication ;
4
8
import org .springframework .boot .autoconfigure .SpringBootApplication ;
5
9
10
+ import javax .annotation .PostConstruct ;
11
+ import java .util .Optional ;
12
+
6
13
@ SpringBootApplication
7
14
public class SpringBootSecurityJwtApplication {
8
15
16
+ @ Autowired
17
+ private RoleRepository roleRepository ;
18
+
9
19
public static void main (String [] args ) {
10
20
SpringApplication .run (SpringBootSecurityJwtApplication .class , args );
11
21
}
12
22
23
+ @ PostConstruct
24
+ public void init () {
25
+ // Create roles if they don't exist
26
+ for (ERole role : ERole .values ()) {
27
+ Optional <Role > optionalRole = roleRepository .findByName (role );
28
+ if (!optionalRole .isPresent ()) {
29
+ Role newRole = new Role ();
30
+ newRole .setName (role );
31
+ roleRepository .save (newRole );
32
+ }
33
+ }
34
+ }
13
35
}
You can’t perform that action at this time.
0 commit comments