blob: c7844a04489937fb3866ec005d1760080e445335 (
plain)
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
|
package com.example.resourceserver.security;
import java.security.Principal;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/security/users")
public class UserRestController {
private UserService userService;
public UserRestController(UserService userService) {
this.userService = userService;
}
/**
* What is in Principal?
*
* Note that we are returning the details just for testing (here and in some
* other methods below). But what is typically done is not returning such
* details but use the details within the method itself to determine who is the
* calling user and the roles of that user.
*
* @param principal
* @return
*/
@GetMapping("/test")
public String test(Principal principal) {
return principal.toString();
}
/**
* principal.getName() returns the OAuth2 authenticated user name. Can be
* trusted within this method.
*
* @param principal
* @return
*/
@GetMapping("/test-username")
public String testName(Principal principal) {
return principal.getName();
}
/**
* Roles returned by authentication.getAuthorities() may not always have custom
* roles.
*
* @param authentication
* @return
*/
@GetMapping("/test-roles-auth")
public Collection<String> getRoles(Authentication authentication) {
return authentication.getAuthorities().stream()
.map(GrantedAuthority::getAuthority)
.collect(Collectors.toList());
}
/**
* Inspect raw JWT claims directly.
*
* @param jwt
* @return
*/
@GetMapping("/test-roles-jwt")
public Map<String, Object> getClaims(@AuthenticationPrincipal Jwt jwt) {
return jwt.getClaims(); // Extract custom JSON fields containing roles
}
/**
* Within a method, use the X-User-Roles HTTP header to determine the user
* roles.
*
* Not very secure because unverifiable headers may be introduced by attackers.
* So to trust the roles so received, this service should have been
* deployed in an access restricted network environment.
*/
@GetMapping("/test-roles-headers")
public String getRolesFromHeaders(@RequestHeader("X-User-Name") String username,
@RequestHeader("X-User-Roles") String roles) {
return "Roles: " + roles + ", username: " + username;
}
}
|