feat: sso-demo 适配 Spring Authorization Server
This commit is contained in:
@@ -1,23 +1,10 @@
|
||||
server:
|
||||
port: 8080
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: sso-demo
|
||||
|
||||
zlt:
|
||||
api-uaa:
|
||||
url: http://127.0.0.1:9900/api-uaa/oauth
|
||||
logout-uri: ${zlt.api-uaa.url}/remove/token?redirect_uri=http://127.0.0.1:8080&access_token=
|
||||
|
||||
security:
|
||||
oauth2:
|
||||
sso:
|
||||
login-path: /singleLogin
|
||||
client:
|
||||
client-id: zlt
|
||||
client-secret: zlt
|
||||
access-token-uri: ${zlt.api-uaa.url}/token
|
||||
user-authorization-uri: ${zlt.api-uaa.url}/authorize
|
||||
resource:
|
||||
token-info-uri: ${zlt.api-uaa.url}/check_token
|
||||
sso:
|
||||
client-id: zlt
|
||||
client-secret: zlt
|
||||
redirect-uri: http://127.0.0.1:8080/callback.html
|
||||
access-token-uri: http://127.0.0.1:9900/api-uaa/oauth/token
|
||||
user-info-uri: http://127.0.0.1:9900/api-user/users/current
|
||||
@@ -0,0 +1,35 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
|
||||
<meta charset="utf-8"/>
|
||||
<title>zlt</title>
|
||||
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
|
||||
<script type="text/javascript" src="js/sso.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script>
|
||||
window.onload = function() {
|
||||
//url获取state
|
||||
let state = getQueryVariable('state');
|
||||
let localState = sessionStorage.getItem("state");
|
||||
//判断state防止CSRF攻击
|
||||
if (localState !== state) {
|
||||
alert('state参数无效!');
|
||||
let state = getState();
|
||||
sessionStorage.setItem("state", state);
|
||||
window.location = getAuthorizeUri(state);
|
||||
}
|
||||
//url获取code
|
||||
let code = getQueryVariable('code');
|
||||
|
||||
//获取token和用户信息
|
||||
$.ajax({url:'http://127.0.0.1:8080/token/'+code, success:function(result) {
|
||||
console.log(result);
|
||||
sessionStorage.setItem('access_token', result);
|
||||
window.location = sessionStorage.getItem('visitUri');
|
||||
}});
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,52 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
|
||||
<meta charset="utf-8"/>
|
||||
<title>zlt</title>
|
||||
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
|
||||
<script type="text/javascript" src="js/sso.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<p>用户名:<span id="userName"></span></p>
|
||||
<p>权限:[<span id="roles"></span>]</p>
|
||||
<p>应用id:<span id="clientId"></span></p>
|
||||
<p>token:<span id="accessToken"></span></p>
|
||||
<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) {//已登录
|
||||
//获取token和用户信息
|
||||
$.ajax({url:'http://127.0.0.1:8080/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 {//未登录
|
||||
login();
|
||||
}
|
||||
};
|
||||
|
||||
function logout() {
|
||||
let accessToken = sessionStorage.getItem('access_token');
|
||||
sessionStorage.removeItem('access_token');
|
||||
window.location = getLogoutUri(accessToken);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,38 @@
|
||||
const FULL_CHARTER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopgrstuvwxyz';
|
||||
//应用id
|
||||
let clientId = 'zlt';
|
||||
//授权中心地址
|
||||
let uaaUri = 'http://127.0.0.1:9900/api-uaa/oauth/';
|
||||
|
||||
function getAuthorizeUri(state) {
|
||||
return uaaUri+'authorize?client_id='+clientId+'&redirect_uri=http://127.0.0.1:8080/callback.html&response_type=code&state='+state;
|
||||
}
|
||||
|
||||
function getLogoutUri(accessToken) {
|
||||
return uaaUri+'remove/token?redirect_uri=http://127.0.0.1:8080/index.html&access_token='+accessToken;
|
||||
}
|
||||
|
||||
function getUserInfo(accessToken) {
|
||||
return 'http://127.0.0.1:8080/user?access_token='+accessToken;
|
||||
}
|
||||
|
||||
function getState() {
|
||||
let state='';
|
||||
for (let i = 0; i < 6; i++) {
|
||||
state += FULL_CHARTER[Math.floor(Math.random() * 52)];
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取url参数
|
||||
*/
|
||||
function getQueryVariable(variable) {
|
||||
var query = window.location.search.substring(1);
|
||||
var vars = query.split("&");
|
||||
for (var i=0;i<vars.length;i++) {
|
||||
var pair = vars[i].split("=");
|
||||
if(pair[0] == variable){return pair[1];}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta content="text/html;charset=UTF-8">
|
||||
<title>SSO单点登录demo</title>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<p>登录者: <span th:text="${username}"></span></p>
|
||||
<p>权限: <span th:text="${authorities}"></span></p>
|
||||
<p>应用id: <span th:text="${clientId}"></span></p>
|
||||
<p>token: <span th:text="${token}"></span></p>
|
||||
<p><input type="button" value="登出" onclick="location='/logout'"/></p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user