File tree Expand file tree Collapse file tree 4 files changed +73
-22
lines changed
src/main/java/de/doubleslash/example/springboot Expand file tree Collapse file tree 4 files changed +73
-22
lines changed Original file line number Diff line number Diff line change 11package de .doubleslash .example .springboot ;
22
3+ import java .text .MessageFormat ;
4+
5+ import org .springframework .security .core .Authentication ;
6+ import org .springframework .security .core .context .SecurityContextHolder ;
7+ import org .springframework .security .core .userdetails .UserDetails ;
38import org .springframework .web .bind .annotation .RequestMapping ;
49import org .springframework .web .bind .annotation .RestController ;
510
611@ RestController
712public class HelloController {
813
9- @ RequestMapping ("/" )
10- public String index () {
11- return "Greetings from Spring Boot!" ;
12- }
14+ @ RequestMapping ("/" )
15+ public String index () {
16+
17+ Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
18+
19+ UserDetails currentUser = (UserDetails ) authentication .getPrincipal ();
20+ return MessageFormat .format ("Hello {0} {1}!" ,
21+ currentUser .getAuthorities ().stream () //
22+ .map (grantedAuth -> grantedAuth .getAuthority ())//
23+ .anyMatch (role -> role .equals ("ADMIN" )) ? "administrator" : "user" ,
24+ currentUser .getUsername ());
25+ }
1326
1427}
Original file line number Diff line number Diff line change 88@ Table (name = "Role" )
99public class Role {
1010
11- public Role () {}
12-
13- public Role (final String role ) {
14- this .role = role ;
15- }
16-
17- @ Id
18- private String role ;
19-
20- public String getRole () {
21- return role ;
22- }
23-
24- public void setRole (final String role ) {
25- this .role = role ;
26-
27- }
11+ public Role () {
12+ }
13+
14+ public Role (final String role ) {
15+ this .role = role ;
16+ }
17+
18+ @ Id
19+ private String role ;
20+
21+ public String getRole () {
22+ return role ;
23+ }
24+
25+ public void setRole (final String role ) {
26+ this .role = role ;
27+ }
28+
29+ @ Override
30+ public int hashCode () {
31+ final int prime = 31 ;
32+ int result = 1 ;
33+ result = prime * result + ((role == null ) ? 0 : role .hashCode ());
34+ return result ;
35+ }
36+
37+ @ Override
38+ public boolean equals (Object obj ) {
39+ if (this == obj )
40+ return true ;
41+ if (obj == null )
42+ return false ;
43+ if (getClass () != obj .getClass ())
44+ return false ;
45+ Role other = (Role ) obj ;
46+ if (role == null ) {
47+ if (other .role != null )
48+ return false ;
49+ } else if (!role .equals (other .role ))
50+ return false ;
51+ return true ;
52+ }
2853
2954}
Original file line number Diff line number Diff line change @@ -21,6 +21,9 @@ public class User {
2121 @ NotNull
2222 private boolean enabled = true ;
2323
24+ // Role could be an enum instead of entity.
25+ // We just use an entity here in order to show ManyToOne in a simple example.
26+ // There is an example using Role enum in the Spring documentation.
2427 @ ManyToOne (cascade = CascadeType .ALL )
2528 private Role role ;
2629
Original file line number Diff line number Diff line change @@ -13,11 +13,21 @@ public class UserInit implements CommandLineRunner {
1313
1414 @ Override
1515 public void run (final String ... args ) throws Exception {
16- final User user1 = new User ("user1" ,
16+
17+ // create a user
18+ final User user1 = new User ("user1" ,
1719 // Passwort hashen
1820 new BCryptPasswordEncoder ().encode ("bootifulPassword" ), //
1921 true , new Role ("USER" ));
2022 userRepo .save (user1 );
23+
24+ // create an admin
25+ final User admin1 = new User ("admin1" ,
26+ // Passwort hashen
27+ new BCryptPasswordEncoder ().encode ("bootifulAdminPassword" ), //
28+ true , new Role ("ADMIN" ));
29+ userRepo .save (admin1 );
2130 }
31+
2232
2333}
You can’t perform that action at this time.
0 commit comments