Skip to content

Commit

Permalink
modify user controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Doha2012 committed Dec 21, 2017
1 parent debd8f5 commit 553d94f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;

import java.util.Map;

import org.baeldung.web.dto.Foo;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -29,11 +25,7 @@ public FooController() {
@PreAuthorize("#oauth2.hasScope('foo') and #oauth2.hasScope('read')")
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}")
@ResponseBody
public Foo findById(@PathVariable final long id, Authentication auth) {
OAuth2AuthenticationDetails oauthDetails = (OAuth2AuthenticationDetails) auth.getDetails();
Map<String, Object> details = (Map<String, Object>) oauthDetails.getDecodedDetails();
System.out.println("User organization is " + details.get("organization"));

public Foo findById(@PathVariable final long id) {
return new Foo(Long.parseLong(randomNumeric(2)), randomAlphabetic(4));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,24 @@

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
@Autowired
private TokenStore tokenStore;

@PreAuthorize("#oauth2.hasScope('read')")
@RequestMapping(method = RequestMethod.GET, value = "/users/extra")
@ResponseBody
public Map<String, Object> getExtraInfo(OAuth2Authentication auth) {
final OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) auth.getDetails();
final OAuth2AccessToken accessToken = tokenStore.readAccessToken(details.getTokenValue());
System.out.println(accessToken);
return accessToken.getAdditionalInformation();
public Map<String, Object> getExtraInfo(Authentication auth) {
OAuth2AuthenticationDetails oauthDetails = (OAuth2AuthenticationDetails) auth.getDetails();
Map<String, Object> details = (Map<String, Object>) oauthDetails.getDecodedDetails();
System.out.println("User organization is " + details.get("organization"));
return details;
}
}

0 comments on commit 553d94f

Please sign in to comment.