Skip to content

Commit 27e0b9b

Browse files
author
Chris Dutra
committed
Redirect back to sample app home page when user logs out
1 parent 23999c6 commit 27e0b9b

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

Diff for: authcode/src/main/java/org/cloudfoundry/identity/samples/authcode/Application.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.cloudfoundry.identity.samples.authcode;
22

33
import java.io.IOException;
4+
import java.net.URL;
45
import java.util.Arrays;
56
import java.util.Map;
67

@@ -55,6 +56,8 @@ public static void main(String[] args) {
5556
// property set by spring-cloud-sso-connector
5657
@Value("${ssoServiceUrl:placeholder}")
5758
private String ssoServiceUrl;
59+
@Value("${security.oauth2.client.clientId}")
60+
private String clientId;
5861

5962
@Autowired(required = false)
6063
private OAuth2RestTemplate oauth2RestTemplate;
@@ -81,16 +84,15 @@ public String index() {
8184
}
8285

8386
@RequestMapping("/authorization_code")
84-
public String authCode(Model model) throws Exception {
87+
public String authCode(Model model, HttpServletRequest request) throws Exception {
8588
if (ssoServiceUrl.equals("placeholder")) {
8689
model.addAttribute("header", "Warning: You need to bind to the SSO service.");
8790
model.addAttribute("warning", "Please bind your app to restore regular functionality");
8891
return "configure_warning";
8992
}
90-
Map<?,?> userInfoResponse = oauth2RestTemplate.getForObject("{ssoServiceUrl}/userinfo", Map.class,
91-
ssoServiceUrl);
93+
Map<?,?> userInfoResponse = oauth2RestTemplate.getForObject("{ssoServiceUrl}/userinfo", Map.class, ssoServiceUrl);
9294
model.addAttribute("ssoServiceUrl",ssoServiceUrl);
93-
model.addAttribute("response",toPrettyJsonString(userInfoResponse));
95+
model.addAttribute("response", toPrettyJsonString(userInfoResponse));
9496

9597
OAuth2AccessToken accessToken = oauth2RestTemplate.getOAuth2ClientContext().getAccessToken();
9698
if (accessToken != null) {
@@ -101,12 +103,14 @@ public String authCode(Model model) throws Exception {
101103
}
102104

103105
@RequestMapping(value="/logout", method = GET)
104-
public String logout(HttpServletRequest request, HttpServletResponse response) {
106+
public String logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
105107
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
106108
if (auth != null){
107109
new SecurityContextLogoutHandler().logout(request, response, auth);
108110
}
109-
return "redirect:" + ssoServiceUrl + "/logout.do";
111+
URL url = new URL(request.getRequestURL().toString());
112+
String urlStr = url.getProtocol() + "://" + url.getAuthority();
113+
return "redirect:" + ssoServiceUrl + "/logout.do?redirect=" + urlStr + "&clientId=" + clientId;
110114
}
111115

112116
private Map<String, ?> parseToken(String base64Token) throws IOException {

Diff for: authcode/src/main/resources/templates/authorization_code.html

-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,5 @@ <h2>What do you want to do?</h2>
2828
</li>
2929
</ul>
3030

31-
32-
3331
</body>
3432
</html>

Diff for: implicit/src/main/resources/public/app.jsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ var ExampleApplication = React.createClass({
2121

2222
render() {
2323
var page = null;
24+
var clientId = document.getElementById("clientId").content;
2425
if (window.location.pathname === "/") {
25-
var clientId = document.getElementById("clientId").content;
2626
if (clientId === "client_id_placeholder") {
2727
page = (
2828
<div>
@@ -52,7 +52,8 @@ var ExampleApplication = React.createClass({
5252
var token = this.prettyToken(this.getFragment("access_token"));
5353
var tokenType = this.getFragment("token_type");
5454
var profileUrl = ssoServiceUrl + '/profile';
55-
var logoutUrl = ssoServiceUrl + '/logout.do';
55+
const urlStr = window.location.protocol + '//' + window.location.host;
56+
var logoutUrl = ssoServiceUrl + '/logout.do' + '?redirect=' + urlStr + '&client_id=' + clientId;
5657
page = (<div>
5758
<h1>Implicit sample</h1>
5859
<p>The server only saw a request for /implicit.html. Everything after the # in the address bar is stuff that only your browser can see.</p>
@@ -76,7 +77,7 @@ var ExampleApplication = React.createClass({
7677
<a id="profile" target="uaa" href={profileUrl}>See your account profile on UAA (so you can de-authorize this client)</a>
7778
</li>
7879
<li>
79-
<a id="logout" target="uaa" href={logoutUrl}>Log out of UAA</a>
80+
<a id="logout" href={logoutUrl}>Log out of UAA</a>
8081
</li>
8182
</ul>
8283
</div>);

0 commit comments

Comments
 (0)