2017-03-20 16:18:08 -04:00
|
|
|
/*
|
2017-11-11 20:54:04 -05:00
|
|
|
* Copyright 2002-2017 the original author or authors.
|
2017-03-20 16:18:08 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2017-05-01 12:01:41 -04:00
|
|
|
package sample.web;
|
2017-03-20 16:18:08 -04:00
|
|
|
|
2017-10-28 17:10:39 -04:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2017-06-01 11:46:22 -04:00
|
|
|
import org.springframework.http.HttpHeaders;
|
2017-10-28 17:10:39 -04:00
|
|
|
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
|
|
|
|
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
|
2017-10-25 16:17:49 -04:00
|
|
|
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
|
2017-03-20 16:18:08 -04:00
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.ui.Model;
|
2017-09-29 10:51:02 -04:00
|
|
|
import org.springframework.util.StringUtils;
|
2017-03-20 16:18:08 -04:00
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
2017-06-01 11:46:22 -04:00
|
|
|
import org.springframework.web.reactive.function.client.ClientRequest;
|
|
|
|
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
|
|
|
|
import org.springframework.web.reactive.function.client.WebClient;
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
2017-09-29 10:51:02 -04:00
|
|
|
import java.util.Collections;
|
2017-06-01 11:46:22 -04:00
|
|
|
import java.util.Map;
|
2017-03-20 16:18:08 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Joe Grandja
|
|
|
|
*/
|
|
|
|
@Controller
|
|
|
|
public class MainController {
|
|
|
|
|
2017-10-28 17:10:39 -04:00
|
|
|
@Autowired
|
|
|
|
private OAuth2AuthorizedClientService authorizedClientService;
|
|
|
|
|
2017-03-20 16:18:08 -04:00
|
|
|
@RequestMapping("/")
|
2017-10-28 17:10:39 -04:00
|
|
|
public String index(Model model, OAuth2AuthenticationToken authentication) {
|
|
|
|
OAuth2AuthorizedClient authorizedClient = this.getAuthorizedClient(authentication);
|
|
|
|
model.addAttribute("userName", authentication.getName());
|
|
|
|
model.addAttribute("clientName", authorizedClient.getClientRegistration().getClientName());
|
2017-03-20 16:18:08 -04:00
|
|
|
return "index";
|
|
|
|
}
|
2017-06-01 11:46:22 -04:00
|
|
|
|
|
|
|
@RequestMapping("/userinfo")
|
2017-10-24 15:11:35 -04:00
|
|
|
public String userinfo(Model model, OAuth2AuthenticationToken authentication) {
|
2017-10-28 17:10:39 -04:00
|
|
|
OAuth2AuthorizedClient authorizedClient = this.getAuthorizedClient(authentication);
|
2017-09-29 10:51:02 -04:00
|
|
|
Map userAttributes = Collections.emptyMap();
|
2017-10-28 17:10:39 -04:00
|
|
|
String userInfoEndpointUri = authorizedClient.getClientRegistration()
|
2017-09-29 10:51:02 -04:00
|
|
|
.getProviderDetails().getUserInfoEndpoint().getUri();
|
|
|
|
if (!StringUtils.isEmpty(userInfoEndpointUri)) { // userInfoEndpointUri is optional for OIDC Clients
|
2017-10-10 14:40:11 -04:00
|
|
|
userAttributes = WebClient.builder()
|
2017-10-28 17:10:39 -04:00
|
|
|
.filter(oauth2Credentials(authorizedClient))
|
2017-10-09 08:17:21 -05:00
|
|
|
.build()
|
2017-09-29 10:51:02 -04:00
|
|
|
.get()
|
|
|
|
.uri(userInfoEndpointUri)
|
|
|
|
.retrieve()
|
|
|
|
.bodyToMono(Map.class)
|
|
|
|
.block();
|
|
|
|
}
|
2017-06-01 11:46:22 -04:00
|
|
|
model.addAttribute("userAttributes", userAttributes);
|
|
|
|
return "userinfo";
|
|
|
|
}
|
|
|
|
|
2017-10-28 17:10:39 -04:00
|
|
|
private OAuth2AuthorizedClient getAuthorizedClient(OAuth2AuthenticationToken authentication) {
|
|
|
|
return this.authorizedClientService.loadAuthorizedClient(
|
2017-10-29 20:25:03 -04:00
|
|
|
authentication.getAuthorizedClientRegistrationId(), authentication.getName());
|
2017-10-28 17:10:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private ExchangeFilterFunction oauth2Credentials(OAuth2AuthorizedClient authorizedClient) {
|
2017-06-01 11:46:22 -04:00
|
|
|
return ExchangeFilterFunction.ofRequestProcessor(
|
|
|
|
clientRequest -> {
|
|
|
|
ClientRequest authorizedRequest = ClientRequest.from(clientRequest)
|
2017-10-28 17:10:39 -04:00
|
|
|
.header(HttpHeaders.AUTHORIZATION, "Bearer " + authorizedClient.getAccessToken().getTokenValue())
|
2017-06-01 11:46:22 -04:00
|
|
|
.build();
|
|
|
|
return Mono.just(authorizedRequest);
|
|
|
|
});
|
|
|
|
}
|
2017-03-20 16:18:08 -04:00
|
|
|
}
|