Skip to content

Commit 562a36d

Browse files
U-DOUBLESLASH\mfischerU-DOUBLESLASH\mfischer
authored andcommitted
getting username and user role in HelloController
1 parent c2f059a commit 562a36d

File tree

4 files changed

+73
-22
lines changed

4 files changed

+73
-22
lines changed
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
package 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;
38
import org.springframework.web.bind.annotation.RequestMapping;
49
import org.springframework.web.bind.annotation.RestController;
510

611
@RestController
712
public 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
}

src/main/java/de/doubleslash/example/springboot/Role.java

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,47 @@
88
@Table(name = "Role")
99
public 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
}

src/main/java/de/doubleslash/example/springboot/User.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff 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

src/main/java/de/doubleslash/example/springboot/UserInit.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)