Skip to content

Commit debd8f5

Browse files
committed
get extra info in controller
1 parent 0eeb283 commit debd8f5

File tree

1 file changed

+9
-1
lines changed
  • spring-security-oauth-resource/src/main/java/org/baeldung/web/controller

1 file changed

+9
-1
lines changed

spring-security-oauth-resource/src/main/java/org/baeldung/web/controller/FooController.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
44
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
55

6+
import java.util.Map;
7+
68
import org.baeldung.web.dto.Foo;
79
import org.springframework.http.HttpStatus;
810
import org.springframework.security.access.prepost.PreAuthorize;
11+
import org.springframework.security.core.Authentication;
12+
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
913
import org.springframework.stereotype.Controller;
1014
import org.springframework.web.bind.annotation.PathVariable;
1115
import org.springframework.web.bind.annotation.RequestBody;
@@ -25,7 +29,11 @@ public FooController() {
2529
@PreAuthorize("#oauth2.hasScope('foo') and #oauth2.hasScope('read')")
2630
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}")
2731
@ResponseBody
28-
public Foo findById(@PathVariable final long id) {
32+
public Foo findById(@PathVariable final long id, Authentication auth) {
33+
OAuth2AuthenticationDetails oauthDetails = (OAuth2AuthenticationDetails) auth.getDetails();
34+
Map<String, Object> details = (Map<String, Object>) oauthDetails.getDecodedDetails();
35+
System.out.println("User organization is " + details.get("organization"));
36+
2937
return new Foo(Long.parseLong(randomNumeric(2)), randomAlphabetic(4));
3038
}
3139

0 commit comments

Comments
 (0)