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() {
|
||||
|
||||
Reference in New Issue
Block a user