1- package com .baeldung .um .persistence .setup ;
2-
3- import java .util .Set ;
4-
5- import org .slf4j .Logger ;
6- import org .slf4j .LoggerFactory ;
7- import org .springframework .beans .factory .annotation .Autowired ;
8- import org .springframework .context .ApplicationListener ;
9- import org .springframework .context .annotation .Profile ;
10- import org .springframework .context .event .ContextRefreshedEvent ;
11- import org .springframework .stereotype .Component ;
12-
13- import com .baeldung .common .spring .util .Profiles ;
14- import com .baeldung .um .persistence .model .Privilege ;
15- import com .baeldung .um .persistence .model .Role ;
16- import com .baeldung .um .persistence .model .User ;
17- import com .baeldung .um .service .IPrivilegeService ;
18- import com .baeldung .um .service .IRoleService ;
19- import com .baeldung .um .service .IUserService ;
20- import com .baeldung .um .util .Um ;
21- import com .baeldung .um .util .Um .Privileges ;
22- import com .baeldung .um .util .Um .Roles ;
23- import com .google .common .base .Preconditions ;
24- import com .google .common .collect .Sets ;
25-
26- /**
27- * This simple setup class will run during the bootstrap process of Spring and will create some setup data <br>
28- * The main focus here is creating some standard privileges, then roles and finally some default principals/users
29- */
30- @ Component
31- @ Profile (Profiles .PRODUCTION )
32- public class SecuritySetup implements ApplicationListener <ContextRefreshedEvent > {
33- private final Logger logger = LoggerFactory .getLogger (SecuritySetup .class );
34-
35- private boolean setupDone ;
36-
37- @ Autowired
38- private IUserService userService ;
39-
40- @ Autowired
41- private IRoleService roleService ;
42-
43- @ Autowired
44- private IPrivilegeService privilegeService ;
45-
46- public SecuritySetup () {
47- super ();
48- }
49-
50- //
51-
52- /**
53- * - note that this is a compromise - the flag makes this bean statefull which can (and will) be avoided in the future by a more advanced mechanism <br>
54- * - the reason for this is that the context is refreshed more than once throughout the lifecycle of the deployable <br>
55- * - alternatives: proper persisted versioning
56- */
57- @ Override
58- public final void onApplicationEvent (final ContextRefreshedEvent event ) {
59- if (!setupDone ) {
60- logger .info ("Executing Setup" );
61-
62- createPrivileges ();
63- createRoles ();
64- createUserss ();
65-
66- setupDone = true ;
67- logger .info ("Setup Done" );
68- }
69- }
70-
71- // Privilege
72-
73- private void createPrivileges () {
74- createPrivilegeIfNotExisting (Privileges .CAN_PRIVILEGE_READ );
75- createPrivilegeIfNotExisting (Privileges .CAN_PRIVILEGE_WRITE );
76-
77- createPrivilegeIfNotExisting (Privileges .CAN_ROLE_READ );
78- createPrivilegeIfNotExisting (Privileges .CAN_ROLE_WRITE );
79-
80- createPrivilegeIfNotExisting (Privileges .CAN_USER_READ );
81- createPrivilegeIfNotExisting (Privileges .CAN_USER_WRITE );
82- }
83-
84- final void createPrivilegeIfNotExisting (final String name ) {
85- final Privilege entityByName = privilegeService .findByName (name );
86- if (entityByName == null ) {
87- final Privilege entity = new Privilege (name );
88- privilegeService .create (entity );
89- }
90- }
91-
92- // Role
93-
94- private void createRoles () {
95- final Privilege canPrivilegeRead = privilegeService .findByName (Privileges .CAN_PRIVILEGE_READ );
96- final Privilege canPrivilegeWrite = privilegeService .findByName (Privileges .CAN_PRIVILEGE_WRITE );
97- final Privilege canRoleRead = privilegeService .findByName (Privileges .CAN_ROLE_READ );
98- final Privilege canRoleWrite = privilegeService .findByName (Privileges .CAN_ROLE_WRITE );
99- final Privilege canUserRead = privilegeService .findByName (Privileges .CAN_USER_READ );
100- final Privilege canUserWrite = privilegeService .findByName (Privileges .CAN_USER_WRITE );
101-
102- Preconditions .checkNotNull (canPrivilegeRead );
103- Preconditions .checkNotNull (canPrivilegeWrite );
104- Preconditions .checkNotNull (canRoleRead );
105- Preconditions .checkNotNull (canRoleWrite );
106- Preconditions .checkNotNull (canUserRead );
107- Preconditions .checkNotNull (canUserWrite );
108-
109- createRoleIfNotExisting (Roles .ROLE_ADMIN , Sets .<Privilege > newHashSet (canUserRead , canUserWrite , canRoleRead , canRoleWrite , canPrivilegeRead , canPrivilegeWrite ));
110- }
111-
112- final void createRoleIfNotExisting (final String name , final Set <Privilege > privileges ) {
113- final Role entityByName = roleService .findByName (name );
114- if (entityByName == null ) {
115- final Role entity = new Role (name );
116- entity .setPrivileges (privileges );
117- roleService .create (entity );
118- }
119- }
120-
121- // User/User
122-
123- final void createUserss () {
124- final Role roleAdmin = roleService .findByName (Roles .ROLE_ADMIN );
125-
126- // createUserIfNotExisting(SecurityConstants.ADMIN_USERNAME, SecurityConstants.ADMIN_PASS, Sets.<Role> newHashSet(roleAdmin));
127- createUserIfNotExisting (Um .ADMIN_EMAIL , Um .ADMIN_PASS , Sets .<Role > newHashSet (roleAdmin ));
128- }
129-
130- final void createUserIfNotExisting (final String loginName , final String pass , final Set <Role > roles ) {
131- final User entityByName = userService .findByName (loginName );
132- if (entityByName == null ) {
133- final User entity = new User (loginName , pass , roles );
134- userService .create (entity );
135- }
136- }
137-
1+ package com .baeldung .um .service ;
2+
3+ import java .util .Set ;
4+
5+ import org .slf4j .Logger ;
6+ import org .slf4j .LoggerFactory ;
7+ import org .springframework .beans .factory .annotation .Autowired ;
8+ import org .springframework .context .ApplicationListener ;
9+ import org .springframework .context .event .ContextRefreshedEvent ;
10+ import org .springframework .stereotype .Component ;
11+
12+ import com .baeldung .um .persistence .model .Privilege ;
13+ import com .baeldung .um .persistence .model .Role ;
14+ import com .baeldung .um .persistence .model .User ;
15+ import com .baeldung .um .util .Um ;
16+ import com .baeldung .um .util .Um .Privileges ;
17+ import com .baeldung .um .util .Um .Roles ;
18+ import com .google .common .base .Preconditions ;
19+ import com .google .common .collect .Sets ;
20+
21+ /**
22+ * This simple setup class will run during the bootstrap process of Spring and will create some setup data <br>
23+ * The main focus here is creating some standard privileges, then roles and finally some default principals/users
24+ */
25+ @ Component
26+ public class SecuritySetup implements ApplicationListener <ContextRefreshedEvent > {
27+ private final Logger logger = LoggerFactory .getLogger (SecuritySetup .class );
28+
29+ private boolean setupDone ;
30+
31+ @ Autowired
32+ private IUserService userService ;
33+
34+ @ Autowired
35+ private IRoleService roleService ;
36+
37+ @ Autowired
38+ private IPrivilegeService privilegeService ;
39+
40+ public SecuritySetup () {
41+ super ();
42+ }
43+
44+ //
45+
46+ /**
47+ * - note that this is a compromise - the flag makes this bean statefull which can (and will) be avoided in the future by a more advanced mechanism <br>
48+ * - the reason for this is that the context is refreshed more than once throughout the lifecycle of the deployable <br>
49+ * - alternatives: proper persisted versioning
50+ */
51+ @ Override
52+ public final void onApplicationEvent (final ContextRefreshedEvent event ) {
53+ if (!setupDone ) {
54+ logger .info ("Executing Setup" );
55+
56+ createPrivileges ();
57+ createRoles ();
58+ createUserss ();
59+
60+ setupDone = true ;
61+ logger .info ("Setup Done" );
62+ }
63+ }
64+
65+ // Privilege
66+
67+ private void createPrivileges () {
68+ createPrivilegeIfNotExisting (Privileges .CAN_PRIVILEGE_READ );
69+ createPrivilegeIfNotExisting (Privileges .CAN_PRIVILEGE_WRITE );
70+
71+ createPrivilegeIfNotExisting (Privileges .CAN_ROLE_READ );
72+ createPrivilegeIfNotExisting (Privileges .CAN_ROLE_WRITE );
73+
74+ createPrivilegeIfNotExisting (Privileges .CAN_USER_READ );
75+ createPrivilegeIfNotExisting (Privileges .CAN_USER_WRITE );
76+ }
77+
78+ final void createPrivilegeIfNotExisting (final String name ) {
79+ final Privilege entityByName = privilegeService .findByName (name );
80+ if (entityByName == null ) {
81+ final Privilege entity = new Privilege (name );
82+ privilegeService .create (entity );
83+ }
84+ }
85+
86+ // Role
87+
88+ private void createRoles () {
89+ final Privilege canPrivilegeRead = privilegeService .findByName (Privileges .CAN_PRIVILEGE_READ );
90+ final Privilege canPrivilegeWrite = privilegeService .findByName (Privileges .CAN_PRIVILEGE_WRITE );
91+ final Privilege canRoleRead = privilegeService .findByName (Privileges .CAN_ROLE_READ );
92+ final Privilege canRoleWrite = privilegeService .findByName (Privileges .CAN_ROLE_WRITE );
93+ final Privilege canUserRead = privilegeService .findByName (Privileges .CAN_USER_READ );
94+ final Privilege canUserWrite = privilegeService .findByName (Privileges .CAN_USER_WRITE );
95+
96+ Preconditions .checkNotNull (canPrivilegeRead );
97+ Preconditions .checkNotNull (canPrivilegeWrite );
98+ Preconditions .checkNotNull (canRoleRead );
99+ Preconditions .checkNotNull (canRoleWrite );
100+ Preconditions .checkNotNull (canUserRead );
101+ Preconditions .checkNotNull (canUserWrite );
102+
103+ createRoleIfNotExisting (Roles .ROLE_ADMIN , Sets .<Privilege > newHashSet (canUserRead , canUserWrite , canRoleRead , canRoleWrite , canPrivilegeRead , canPrivilegeWrite ));
104+ }
105+
106+ final void createRoleIfNotExisting (final String name , final Set <Privilege > privileges ) {
107+ final Role entityByName = roleService .findByName (name );
108+ if (entityByName == null ) {
109+ final Role entity = new Role (name );
110+ entity .setPrivileges (privileges );
111+ roleService .create (entity );
112+ }
113+ }
114+
115+ // User/User
116+
117+ final void createUserss () {
118+ final Role roleAdmin = roleService .findByName (Roles .ROLE_ADMIN );
119+
120+ // createUserIfNotExisting(SecurityConstants.ADMIN_USERNAME, SecurityConstants.ADMIN_PASS, Sets.<Role> newHashSet(roleAdmin));
121+ createUserIfNotExisting (Um .ADMIN_EMAIL , Um .ADMIN_PASS , Sets .<Role > newHashSet (roleAdmin ));
122+ }
123+
124+ final void createUserIfNotExisting (final String loginName , final String pass , final Set <Role > roles ) {
125+ final User entityByName = userService .findByName (loginName );
126+ if (entityByName == null ) {
127+ final User entity = new User (loginName , pass , roles );
128+ userService .create (entity );
129+ }
130+ }
131+
138132}
0 commit comments