org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient Java Examples

The following examples show how to use org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MessagingController.java    From messaging-app with Apache License 2.0 7 votes vote down vote up
@PostMapping
public String save(@RegisteredOAuth2AuthorizedClient("messaging") OAuth2AuthorizedClient messagingClient,
					@Valid Message message,
					@AuthenticationPrincipal OidcUser oidcUser) {
	message.setFromId(oidcUser.getClaimAsString("user_name"));
	message = this.webClient
			.post()
			.uri(this.messagesBaseUri)
			.contentType(MediaType.APPLICATION_JSON)
			.syncBody(message)
			.attributes(oauth2AuthorizedClient(messagingClient))
			.retrieve()
			.bodyToMono(Message.class)
			.block();
	return "redirect:/messages/sent";
}
 
Example #2
Source File: FlowABCTokenExchangeController.java    From oauth2-protocol-patterns with Apache License 2.0 6 votes vote down vote up
@GetMapping
public String flowABC_TokenExchange(@RegisteredOAuth2AuthorizedClient("client-ab") OAuth2AuthorizedClient clientAB,
									OAuth2AuthenticationToken oauth2Authentication,
									HttpServletRequest request,
									Map<String, Object> model) {

	ServiceCallResponse serviceACallResponse = callService(ServicesConfig.SERVICE_A, clientAB);

	MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
	params.put(FLOW_TYPE_PARAMETER, Collections.singletonList(FLOW_TYPE_TOKEN_EXCHANGE));
	ServiceCallResponse serviceBCallResponse = callService(ServicesConfig.SERVICE_B, clientAB, params);

	String modelAttr = "flowABCCall_" + FLOW_TYPE_TOKEN_EXCHANGE;
	model.put(modelAttr, fromUiApp(oauth2Authentication, request, serviceACallResponse, serviceBCallResponse));
	model.put("flowActive", true);

	return "index";
}
 
Example #3
Source File: FlowABCClientCredentialsController.java    From oauth2-protocol-patterns with Apache License 2.0 6 votes vote down vote up
@GetMapping
public String flowABC_ClientCredentials(@RegisteredOAuth2AuthorizedClient("client-ab") OAuth2AuthorizedClient clientAB,
										OAuth2AuthenticationToken oauth2Authentication,
										HttpServletRequest request,
										Map<String, Object> model) {

	ServiceCallResponse serviceACallResponse = callService(ServicesConfig.SERVICE_A, clientAB);

	MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
	params.put(FLOW_TYPE_PARAMETER, Collections.singletonList(FLOW_TYPE_CLIENT_CREDENTIALS));
	ServiceCallResponse serviceBCallResponse = callService(ServicesConfig.SERVICE_B, clientAB, params);

	String modelAttr = "flowABCCall_" + FLOW_TYPE_CLIENT_CREDENTIALS;
	model.put(modelAttr, fromUiApp(oauth2Authentication, request, serviceACallResponse, serviceBCallResponse));
	model.put("flowActive", true);

	return "index";
}
 
Example #4
Source File: FlowABCTokenRelayController.java    From oauth2-protocol-patterns with Apache License 2.0 6 votes vote down vote up
@GetMapping
public String flowABC_TokenRelay(@RegisteredOAuth2AuthorizedClient("client-abc") OAuth2AuthorizedClient clientABC,
									OAuth2AuthenticationToken oauth2Authentication,
									HttpServletRequest request,
									Map<String, Object> model) {

	ServiceCallResponse serviceACallResponse = callService(ServicesConfig.SERVICE_A, clientABC);

	MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
	params.put(FLOW_TYPE_PARAMETER, Collections.singletonList(FLOW_TYPE_TOKEN_RELAY));
	ServiceCallResponse serviceBCallResponse = callService(ServicesConfig.SERVICE_B, clientABC, params);

	String modelAttr = "flowABCCall_" + FLOW_TYPE_TOKEN_RELAY;
	model.put(modelAttr, fromUiApp(oauth2Authentication, request, serviceACallResponse, serviceBCallResponse));
	model.put("flowActive", true);

	return "index";
}
 
Example #5
Source File: MainController.java    From tutorials with MIT License 5 votes vote down vote up
@GetMapping("/foos/{id}")
public Mono<Foo> getFooResource(@RegisteredOAuth2AuthorizedClient("custom") OAuth2AuthorizedClient client, @PathVariable final long id){
    return webClient
        .get()
        .uri("http://localhost:8088/spring-security-oauth-resource/foos/{id}", id)
        .attributes(oauth2AuthorizedClient(client))
        .retrieve()
    .bodyToMono(Foo.class); 
}
 
Example #6
Source File: TravelGatewayApplication.java    From spring-security-samples with MIT License 5 votes vote down vote up
@GetMapping("/whoami")
@ResponseBody
public Map<String, Object> index(
		@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient,
		@AuthenticationPrincipal OidcUser oidcUser) {
	Map<String, Object> model = new HashMap<>();
	model.put("clientName", authorizedClient.getClientRegistration().getClientName());
	model.put("userName", oidcUser.getName());
	model.put("userAttributes", oidcUser.getAttributes());
	return model;
}
 
Example #7
Source File: ClientRestController.java    From tutorials with MIT License 5 votes vote down vote up
@GetMapping("/auth-code-annotated")
Mono<String> useOauthWithAuthCodeAndAnnotation(@RegisteredOAuth2AuthorizedClient("bael") OAuth2AuthorizedClient authorizedClient) {
    Mono<String> retrievedResource = webClient.get()
        .uri(RESOURCE_URI)
        .attributes(oauth2AuthorizedClient(authorizedClient))
        .retrieve()
        .bodyToMono(String.class);
    return retrievedResource.map(string -> "We retrieved the following resource using Oauth: " + string + ". Principal associated: " + authorizedClient.getPrincipalName() + ". Token will expire at: " + authorizedClient.getAccessToken()
        .getExpiresAt());
}
 
Example #8
Source File: ClientRestController.java    From tutorials with MIT License 5 votes vote down vote up
@GetMapping("/auth-code-annotated")
Mono<String> useOauthWithAuthCodeAndAnnotation(@RegisteredOAuth2AuthorizedClient("bael") OAuth2AuthorizedClient authorizedClient) {
    Mono<String> retrievedResource = otherWebClient.get()
        .uri(RESOURCE_URI)
        .attributes(oauth2AuthorizedClient(authorizedClient))
        .retrieve()
        .bodyToMono(String.class);
    return retrievedResource.map(string -> "We retrieved the following resource using Oauth: " + string + ". Principal associated: " + authorizedClient.getPrincipalName() + ". Token will expire at: " + authorizedClient.getAccessToken()
        .getExpiresAt());
}
 
Example #9
Source File: GitHubController.java    From blog-tutorials with MIT License 5 votes vote down vote up
@GetMapping
public String index(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient,
                    @AuthenticationPrincipal OAuth2User oauth2User,
                    Model model) {

    model.addAttribute("repositories", fetchAllRepositories(authorizedClient));
    model.addAttribute("username", oauth2User.getAttributes().get("login"));

    return "index";
}
 
Example #10
Source File: GitHubController.java    From blog-tutorials with MIT License 5 votes vote down vote up
@GetMapping
public String index(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient,
                    @AuthenticationPrincipal OAuth2User oauth2User,
                    Model model) {

    model.addAttribute("repositories", fetchAllRepositories(authorizedClient));
    model.addAttribute("username", oauth2User.getAttributes().get("login"));

    return "index";
}
 
Example #11
Source File: FlowAController.java    From oauth2-protocol-patterns with Apache License 2.0 5 votes vote down vote up
@GetMapping
public String flowA(@RegisteredOAuth2AuthorizedClient("client-a") OAuth2AuthorizedClient clientA,
					OAuth2AuthenticationToken oauth2Authentication,
					HttpServletRequest request,
					Map<String, Object> model) {

	ServiceCallResponse serviceACallResponse = callService(ServicesConfig.SERVICE_A, clientA);

	model.put("flowACall", fromUiApp(oauth2Authentication, request, serviceACallResponse));
	model.put("flowActive", true);

	return "index";
}
 
Example #12
Source File: FlowABController.java    From oauth2-protocol-patterns with Apache License 2.0 5 votes vote down vote up
@GetMapping
public String flowAB(@RegisteredOAuth2AuthorizedClient("client-ab") OAuth2AuthorizedClient clientAB,
						OAuth2AuthenticationToken oauth2Authentication,
						HttpServletRequest request,
						Map<String, Object> model) {

	ServiceCallResponse serviceACallResponse = callService(ServicesConfig.SERVICE_A, clientAB);
	ServiceCallResponse serviceBCallResponse = callService(ServicesConfig.SERVICE_B, clientAB);

	model.put("flowABCall", fromUiApp(oauth2Authentication, request, serviceACallResponse, serviceBCallResponse));
	model.put("flowActive", true);

	return "index";
}
 
Example #13
Source File: FrontIndexController.java    From oauth2-client with MIT License 5 votes vote down vote up
@ResponseBody
@GetMapping(value = "/resource")
public Object resource(HttpServletRequest request,
                       OAuth2AuthenticationToken oAuth2AuthenticationToken,
                       @RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient,
                       Model model) {
    String url = "http://localhost:10580/coupon/list";
    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Bearer " + authorizedClient.getAccessToken().getTokenValue());
    ResponseEntity<Object> response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(headers), Object.class);
    return response.getBody();
}
 
Example #14
Source File: FrontIndexController.java    From oauth2-client with MIT License 5 votes vote down vote up
@GetMapping(value = "/user")
public String user(HttpServletRequest request,
                   OAuth2AuthenticationToken oAuth2AuthenticationToken,
                   @RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient,
                   Model model) {
    return "securedPage";
}
 
Example #15
Source File: MessagingController.java    From messaging-app with Apache License 2.0 5 votes vote down vote up
@GetMapping("/{id}")
public String get(@RegisteredOAuth2AuthorizedClient("messaging") OAuth2AuthorizedClient messagingClient,
					Map<String, Object> model,
					@PathVariable Long id) {
	Message message = this.webClient
			.get()
			.uri(this.messagesBaseUri + "/" + id)
			.attributes(oauth2AuthorizedClient(messagingClient))
			.retrieve()
			.bodyToMono(Message.class)
			.block();
	model.put(MESSAGE_ATTRIBUTE_NAME, message);
	return "message-view";
}
 
Example #16
Source File: MessagingController.java    From messaging-app with Apache License 2.0 5 votes vote down vote up
@GetMapping("/compose")
public String compose(@RegisteredOAuth2AuthorizedClient("messaging") OAuth2AuthorizedClient messagingClient,
					  Map<String, Object> model) {
	List<User> users = getUsers(messagingClient);
	model.put(USERS_ATTRIBUTE_NAME, users);
	model.put(MESSAGE_ATTRIBUTE_NAME, new Message());
	return "message-compose";
}
 
Example #17
Source File: MessagingController.java    From messaging-app with Apache License 2.0 5 votes vote down vote up
@GetMapping("/sent")
public String sent(@RegisteredOAuth2AuthorizedClient("messaging") OAuth2AuthorizedClient messagingClient,
					Map<String, Object> model) {
	List<Message> messages = getMessages(messagingClient, this.messagesBaseUri + "/sent");
	model.put(MESSAGES_ATTRIBUTE_NAME, messages);
	model.put(MESSAGE_TYPE_ATTRIBUTE_NAME, MESSAGE_TYPE_SENT);
	return "message-list";
}
 
Example #18
Source File: MessagingController.java    From messaging-app with Apache License 2.0 5 votes vote down vote up
@GetMapping("/inbox")
public String inbox(@RegisteredOAuth2AuthorizedClient("messaging") OAuth2AuthorizedClient messagingClient,
					Map<String, Object> model) {
	List<Message> messages = getMessages(messagingClient, this.messagesBaseUri + "/inbox");
	model.put(MESSAGES_ATTRIBUTE_NAME, messages);
	model.put(MESSAGE_TYPE_ATTRIBUTE_NAME, MESSAGE_TYPE_INBOX);
	return "message-list";
}
 
Example #19
Source File: GatewayApplication.java    From spring-cloud-gateway-demo with Apache License 2.0 5 votes vote down vote up
@GetMapping("/")
public String index(Model model,
					@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient,
					@AuthenticationPrincipal OAuth2User oauth2User) {
	model.addAttribute("userName", oauth2User.getName());
	model.addAttribute("clientName", authorizedClient.getClientRegistration().getClientName());
	model.addAttribute("userAttributes", oauth2User.getAttributes());
	return "index";
}