OAuth 2.0 Resource Server MAC & RSA 토큰 검증 - @AuthenticationPrincipal 참조
Last updated
Last updated
@RestController
public class IndexController {
@GetMapping("/")
public String index() {
return "index";
}
@GetMapping("/api/user")
public Authentication user(Authentication authentication, @AuthenticationPrincipal Jwt principal) throws URISyntaxException {
JwtAuthenticationToken jwtAuthenticationToken = (JwtAuthenticationToken) authentication;
String sub = (String) jwtAuthenticationToken.getTokenAttributes().get("sub");
String email = (String) jwtAuthenticationToken.getTokenAttributes().get("email");
String scope = (String) jwtAuthenticationToken.getTokenAttributes().get("scope");
String sub1 = principal.getClaimAsString("sub");
String token = principal.getTokenValue();
//principal 에 담겨 있는 토큰을 활용하는 코드
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Bearer " + token);
// RequestEntity<String> request = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8082"));
// ResponseEntity<String> response = restTemplate.exchange(request, String.class);
// String body = response.getBody();
return authentication;
}
}