demo增加统一登出
This commit is contained in:
+25
-1
@@ -7,6 +7,7 @@ import com.central.common.utils.JsonUtil;
|
||||
import com.central.common.utils.RsaUtils;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.security.jwt.Jwt;
|
||||
@@ -22,6 +23,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import sun.misc.BASE64Encoder;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.util.*;
|
||||
@@ -33,6 +35,7 @@ import java.util.*;
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class ApiController {
|
||||
private static final String PUBKEY_START = "-----BEGIN PUBLIC KEY-----";
|
||||
@@ -68,6 +71,8 @@ public class ApiController {
|
||||
*/
|
||||
private final static ThreadLocal<String> NONCE = new ThreadLocal<>();
|
||||
|
||||
private final static Map<String, MyUser> localTokenMap = new HashMap<>();
|
||||
|
||||
@GetMapping("/token/{code}")
|
||||
public Map<String, Object> tokenInfo(@PathVariable String code) throws Exception {
|
||||
//获取token
|
||||
@@ -83,6 +88,8 @@ public class ApiController {
|
||||
if (!userDb.containsKey(user.getId())) {
|
||||
userDb.put(user.getId(), user);
|
||||
}
|
||||
String accessToken = (String)tokenMap.get("access_token");
|
||||
localTokenMap.put(accessToken, user);
|
||||
|
||||
Map<String, Object> result = new HashMap<>(2);
|
||||
result.put("tokenInfo", tokenMap);
|
||||
@@ -90,6 +97,23 @@ public class ApiController {
|
||||
return result;
|
||||
}
|
||||
|
||||
@GetMapping("/logoutNotify")
|
||||
public void logoutNotify(HttpServletRequest request) {
|
||||
String tokens = request.getParameter("tokens");
|
||||
log.info("=====logoutNotify: " + tokens);
|
||||
if (StrUtil.isNotEmpty(tokens)) {
|
||||
for (String accessToken : tokens.split(",")) {
|
||||
localTokenMap.remove(accessToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/user")
|
||||
public MyUser user(HttpServletRequest request) {
|
||||
String token = request.getParameter("access_token");
|
||||
return localTokenMap.get(token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 id_token 的有效性
|
||||
*/
|
||||
@@ -122,7 +146,7 @@ public class ApiController {
|
||||
param.add("code", code);
|
||||
param.add("grant_type", "authorization_code");
|
||||
param.add("redirect_uri", redirectUri);
|
||||
param.add("scope", "app");
|
||||
param.add("scope", "all");
|
||||
param.add("nonce", this.genNonce());
|
||||
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(param, headers);
|
||||
ResponseEntity<Map> response = restTemplate.postForEntity(accessTokenUri, request , Map.class);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
server:
|
||||
port: 8081
|
||||
port: 8082
|
||||
|
||||
spring:
|
||||
application:
|
||||
@@ -7,8 +7,8 @@ spring:
|
||||
|
||||
zlt:
|
||||
sso:
|
||||
client-id: app
|
||||
client-secret: app
|
||||
redirect-uri: http://127.0.0.1:8081/callback.html
|
||||
client-id: webApp
|
||||
client-secret: webApp
|
||||
redirect-uri: http://127.0.0.1:8082/callback.html
|
||||
access-token-uri: http://127.0.0.1:9900/api-uaa/oauth/token
|
||||
jwt-key-uri: http://127.0.0.1:9900/api-uaa/tokens/key
|
||||
@@ -24,12 +24,9 @@
|
||||
let code = getQueryVariable('code');
|
||||
|
||||
//获取token和用户信息
|
||||
$.ajax({url:'http://127.0.0.1:8081/token/'+code, success:function(result) {
|
||||
$.ajax({url:'http://127.0.0.1:'+port+'/token/'+code, success:function(result) {
|
||||
console.log(result);
|
||||
sessionStorage.setItem('access_token', result.tokenInfo.access_token);
|
||||
sessionStorage.setItem('username', result.userInfo.name);
|
||||
sessionStorage.setItem('loginName', result.userInfo.loginName);
|
||||
sessionStorage.setItem('picture', result.userInfo.picture);
|
||||
window.location = sessionStorage.getItem('visitUri');
|
||||
}});
|
||||
};
|
||||
|
||||
@@ -17,31 +17,36 @@
|
||||
<p><input type="button" value="登出" onclick="logout()"/></p>
|
||||
</div>
|
||||
<script>
|
||||
function login() {
|
||||
let state = getState();
|
||||
sessionStorage.setItem("visitUri", window.location.href);
|
||||
sessionStorage.setItem("state", state);
|
||||
window.location = getAuthorizeUri(state);
|
||||
}
|
||||
|
||||
window.onload = function() {
|
||||
let accessToken = sessionStorage.getItem('access_token');
|
||||
if (accessToken) {//已登录
|
||||
let username = sessionStorage.getItem('username');
|
||||
let loginName = sessionStorage.getItem("loginName");
|
||||
let picture = sessionStorage.getItem("picture");
|
||||
$('#accessToken').html(accessToken);
|
||||
$('#userName').html(username);
|
||||
$('#loginName').html(loginName);
|
||||
$('#picture').html(picture);
|
||||
$('#clientId').html(clientId);
|
||||
//获取token和用户信息
|
||||
$.ajax({url:'http://127.0.0.1:'+port+'/user?access_token='+accessToken, success:function(result) {
|
||||
if (result.name) {
|
||||
$('#accessToken').html(accessToken);
|
||||
$('#userName').html(result.name);
|
||||
$('#loginName').html(result.loginName);
|
||||
$('#picture').html(result.picture);
|
||||
$('#clientId').html(clientId);
|
||||
} else {
|
||||
login();
|
||||
}
|
||||
}});
|
||||
} else {//未登录
|
||||
let state = getState();
|
||||
sessionStorage.setItem("visitUri", window.location.href);
|
||||
sessionStorage.setItem("state", state);
|
||||
window.location = getAuthorizeUri(state);
|
||||
login();
|
||||
}
|
||||
};
|
||||
|
||||
function logout() {
|
||||
let accessToken = sessionStorage.getItem('access_token');
|
||||
sessionStorage.removeItem('access_token');
|
||||
sessionStorage.removeItem('username');
|
||||
sessionStorage.removeItem("loginName");
|
||||
sessionStorage.removeItem("picture");
|
||||
window.location = getLogoutUri(accessToken);
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
const FULL_CHARTER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopgrstuvwxyz';
|
||||
//应用id
|
||||
let clientId = 'app';
|
||||
let clientId = 'webApp';
|
||||
//授权中心地址
|
||||
let uaaUri = 'http://127.0.0.1:9900/api-uaa/oauth/';
|
||||
//端口
|
||||
let port = 8082;
|
||||
|
||||
function getAuthorizeUri(state) {
|
||||
return uaaUri+'authorize?client_id='+clientId+'&redirect_uri=http://127.0.0.1:8081/callback.html&response_type=code%20id_token&state='+state;
|
||||
return uaaUri+'authorize?client_id='+clientId+'&redirect_uri=http://127.0.0.1:'+port+'/callback.html&response_type=code%20id_token&state='+state;
|
||||
}
|
||||
|
||||
function getLogoutUri(accessToken) {
|
||||
return uaaUri+'remove/token?redirect_uri=http://127.0.0.1:8081/index.html&access_token='+accessToken;
|
||||
return uaaUri+'remove/token?redirect_uri=http://127.0.0.1:'+port+'/index.html&access_token='+accessToken;
|
||||
}
|
||||
|
||||
function getState() {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.sso.demo.controller;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@@ -14,11 +16,9 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import sun.misc.BASE64Encoder;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
@@ -27,6 +27,7 @@ import java.util.Map;
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class ApiController {
|
||||
@Value("${zlt.sso.client-id:}")
|
||||
@@ -44,8 +45,10 @@ public class ApiController {
|
||||
@Value("${zlt.sso.user-info-uri:}")
|
||||
private String userInfoUri;
|
||||
|
||||
private final static Map<String, Map<String, Object>> localTokenMap = new HashMap<>();
|
||||
|
||||
@GetMapping("/token/{code}")
|
||||
public Map tokenInfo(@PathVariable String code) throws UnsupportedEncodingException {
|
||||
public String tokenInfo(@PathVariable String code) throws UnsupportedEncodingException {
|
||||
//获取token
|
||||
Map tokenMap = getAccessToken(code);
|
||||
String accessToken = (String)tokenMap.get("access_token");
|
||||
@@ -54,10 +57,12 @@ public class ApiController {
|
||||
List<String> roles = getRoles(userMap);
|
||||
|
||||
Map result = new HashMap(2);
|
||||
result.put("tokenInfo", tokenMap);
|
||||
result.put("userInfo", userMap);
|
||||
String username = (String)userMap.get("username");
|
||||
result.put("username", username);
|
||||
result.put("roles", roles);
|
||||
return result;
|
||||
localTokenMap.put(accessToken, result);
|
||||
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,4 +107,21 @@ public class ApiController {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@GetMapping("/user")
|
||||
public Map<String, Object> user(HttpServletRequest request) {
|
||||
String token = request.getParameter("access_token");
|
||||
return localTokenMap.get(token);
|
||||
}
|
||||
|
||||
@GetMapping("/logoutNotify")
|
||||
public void logoutNotify(HttpServletRequest request) {
|
||||
String tokens = request.getParameter("tokens");
|
||||
log.info("=====logoutNotify: " + tokens);
|
||||
if (StrUtil.isNotEmpty(tokens)) {
|
||||
for (String accessToken : tokens.split(",")) {
|
||||
localTokenMap.remove(accessToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,9 +26,7 @@
|
||||
//获取token和用户信息
|
||||
$.ajax({url:'http://127.0.0.1:8081/token/'+code, success:function(result) {
|
||||
console.log(result);
|
||||
sessionStorage.setItem('access_token', result.tokenInfo.access_token);
|
||||
sessionStorage.setItem('username', result.userInfo.username);
|
||||
sessionStorage.setItem('roles', result.roles);
|
||||
sessionStorage.setItem('access_token', result);
|
||||
window.location = sessionStorage.getItem('visitUri');
|
||||
}});
|
||||
};
|
||||
|
||||
@@ -16,28 +16,35 @@
|
||||
<p><input type="button" value="登出" onclick="logout()"/></p>
|
||||
</div>
|
||||
<script>
|
||||
function login() {
|
||||
let state = getState();
|
||||
sessionStorage.setItem("visitUri", window.location.href);
|
||||
sessionStorage.setItem("state", state);
|
||||
window.location = getAuthorizeUri(state);
|
||||
}
|
||||
|
||||
window.onload = function() {
|
||||
let accessToken = sessionStorage.getItem('access_token');
|
||||
if (accessToken) {//已登录
|
||||
let username = sessionStorage.getItem('username');
|
||||
let roles = sessionStorage.getItem("roles");
|
||||
$('#accessToken').html(accessToken);
|
||||
$('#userName').html(username);
|
||||
$('#roles').html(roles);
|
||||
$('#clientId').html(clientId);
|
||||
//获取token和用户信息
|
||||
$.ajax({url:'http://127.0.0.1:8081/user?access_token='+accessToken, success:function(result) {
|
||||
if (result.username) {
|
||||
$('#accessToken').html(accessToken);
|
||||
$('#userName').html(result.username);
|
||||
$('#roles').html(result.roles);
|
||||
$('#clientId').html(clientId);
|
||||
} else {
|
||||
login();
|
||||
}
|
||||
}});
|
||||
} else {//未登录
|
||||
let state = getState();
|
||||
sessionStorage.setItem("visitUri", window.location.href);
|
||||
sessionStorage.setItem("state", state);
|
||||
window.location = getAuthorizeUri(state);
|
||||
login();
|
||||
}
|
||||
};
|
||||
|
||||
function logout() {
|
||||
let accessToken = sessionStorage.getItem('access_token');
|
||||
sessionStorage.removeItem('access_token');
|
||||
sessionStorage.removeItem('username');
|
||||
sessionStorage.removeItem("roles");
|
||||
window.location = getLogoutUri(accessToken);
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -12,6 +12,10 @@ function getLogoutUri(accessToken) {
|
||||
return uaaUri+'remove/token?redirect_uri=http://127.0.0.1:8081/index.html&access_token='+accessToken;
|
||||
}
|
||||
|
||||
function getUserInfo(accessToken) {
|
||||
return 'http://127.0.0.1:8081/user?access_token='+accessToken;
|
||||
}
|
||||
|
||||
function getState() {
|
||||
let state='';
|
||||
for (let i = 0; i < 6; i++) {
|
||||
|
||||
Reference in New Issue
Block a user