Simplify oauth2login-webflux sample

Fixes gh-5396
This commit is contained in:
Joe Grandja 2018-05-30 11:53:25 -04:00
parent 6c13e18483
commit f9f74b1bfc
5 changed files with 13 additions and 109 deletions

View File

@ -9,7 +9,6 @@ dependencies {
compile 'org.springframework.boot:spring-boot-starter-thymeleaf' compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
compile 'org.springframework.boot:spring-boot-starter-webflux' compile 'org.springframework.boot:spring-boot-starter-webflux'
compile 'org.thymeleaf.extras:thymeleaf-extras-springsecurity4' compile 'org.thymeleaf.extras:thymeleaf-extras-springsecurity4'
compile 'io.projectreactor.ipc:reactor-netty'
testCompile project(':spring-security-test') testCompile project(':spring-security-test')
testCompile 'net.sourceforge.htmlunit:htmlunit' testCompile 'net.sourceforge.htmlunit:htmlunit'

View File

@ -1,37 +0,0 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* 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.
*/
package sample;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.web.reactive.function.client.OAuth2AuthorizedClientExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient;
/**
* @author Rob Winch
* @since 5.1
*/
@Configuration
public class WebClientConfig {
@Bean
WebClient webClient() {
return WebClient.builder()
.filter(new OAuth2AuthorizedClientExchangeFilterFunction())
.build();
}
}

View File

@ -16,52 +16,27 @@
package sample.web; package sample.web;
import static org.springframework.security.oauth2.client.web.reactive.function.client.OAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient; import org.springframework.security.core.annotation.AuthenticationPrincipal;
import java.util.Map;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.OAuth2Client; import org.springframework.security.oauth2.client.annotation.OAuth2Client;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
/** /**
* @author Rob Winch * @author Rob Winch
*/ */
@Controller @Controller
public class OAuth2LoginController { public class OAuth2LoginController {
private final WebClient webClient;
public OAuth2LoginController(WebClient webClient) {
this.webClient = webClient;
}
@GetMapping("/") @GetMapping("/")
public String index(Model model, @OAuth2Client OAuth2AuthorizedClient authorizedClient) { public String index(Model model,
model.addAttribute("userName", authorizedClient.getPrincipalName()); @OAuth2Client OAuth2AuthorizedClient authorizedClient,
@AuthenticationPrincipal OAuth2User oauth2User) {
model.addAttribute("userName", oauth2User.getName());
model.addAttribute("clientName", authorizedClient.getClientRegistration().getClientName()); model.addAttribute("clientName", authorizedClient.getClientRegistration().getClientName());
model.addAttribute("userAttributes", oauth2User.getAttributes());
return "index"; return "index";
} }
@GetMapping("/userinfo")
public String userinfo(Model model, @OAuth2Client OAuth2AuthorizedClient authorizedClient) {
Mono<Map> userAttributes = Mono.empty();
String userInfoEndpointUri = authorizedClient.getClientRegistration()
.getProviderDetails().getUserInfoEndpoint().getUri();
if (!StringUtils.isEmpty(userInfoEndpointUri)) { // userInfoEndpointUri is optional for OIDC Clients
userAttributes = this.webClient
.get()
.uri(userInfoEndpointUri)
.attributes(oauth2AuthorizedClient(authorizedClient))
.retrieve()
.bodyToMono(Map.class);
}
model.addAttribute("userAttributes", userAttributes);
return "userinfo";
}
} }

View File

@ -40,7 +40,12 @@
</div> </div>
<div>&nbsp;</div> <div>&nbsp;</div>
<div> <div>
<a href="/userinfo" th:href="@{/userinfo}">Display User Info</a> <span style="font-weight:bold">User Attributes:</span>
<ul>
<li th:each="userAttribute : ${userAttributes}">
<span style="font-weight:bold" th:text="${userAttribute.key}"></span>: <span th:text="${userAttribute.value}"></span>
</li>
</ul>
</div> </div>
</body> </body>
</html> </html>

View File

@ -1,38 +0,0 @@
<!DOCTYPE html>
<!--
~ /*
~ * Copyright 2002-2018 the original author or authors.
~ *
~ * 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.
~ */
~
-->
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Spring Security - OAuth 2.0 User Info</title>
<meta charset="utf-8" />
</head>
<body>
<div th:substituteby="index::logout"></div>
<h1>OAuth 2.0 User Info</h1>
<div>
<span style="font-weight:bold">User Attributes:</span>
<ul>
<li th:each="userAttribute : ${userAttributes}">
<span style="font-weight:bold" th:text="${userAttribute.key}"></span>: <span th:text="${userAttribute.value}"></span>
</li>
</ul>
</div>
</body>
</html>