增加前后端分离的单点登录样例工程web-sso
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package com.sso.demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2020/3/10
|
||||
* <p>
|
||||
* Blog: https://blog.csdn.net/zlt2000
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class WebSSOApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(WebSSOApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.sso.demo.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import sun.misc.BASE64Encoder;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2020/3/10
|
||||
* <p>
|
||||
* Blog: https://blog.csdn.net/zlt2000
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@RestController
|
||||
public class ApiController {
|
||||
@Value("${zlt.sso.client-id:}")
|
||||
private String clientId;
|
||||
|
||||
@Value("${zlt.sso.client-secret:}")
|
||||
private String clientSecret;
|
||||
|
||||
@Value("${zlt.sso.redirect-uri:}")
|
||||
private String redirectUri;
|
||||
|
||||
@Value("${zlt.sso.access-token-uri:}")
|
||||
private String accessTokenUri;
|
||||
|
||||
@Value("${zlt.sso.user-info-uri:}")
|
||||
private String userInfoUri;
|
||||
|
||||
@GetMapping("/token/{code}")
|
||||
public Map tokenInfo(@PathVariable String code) throws UnsupportedEncodingException {
|
||||
//获取token
|
||||
Map tokenMap = getAccessToken(code);
|
||||
String accessToken = (String)tokenMap.get("access_token");
|
||||
//获取用户信息
|
||||
Map userMap = getUserInfo(accessToken);
|
||||
|
||||
Map result = new HashMap(2);
|
||||
result.put("tokenInfo", tokenMap);
|
||||
result.put("userInfo", userMap);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取token
|
||||
*/
|
||||
public Map getAccessToken(String code) throws UnsupportedEncodingException {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||
byte[] authorization = (clientId + ":" + clientSecret).getBytes("UTF-8");
|
||||
BASE64Encoder encoder = new BASE64Encoder();
|
||||
String base64Auth = encoder.encode(authorization);
|
||||
headers.add("Authorization", "Basic " + base64Auth);
|
||||
|
||||
MultiValueMap<String, String> param = new LinkedMultiValueMap<>();
|
||||
param.add("code", code);
|
||||
param.add("grant_type", "authorization_code");
|
||||
param.add("redirect_uri", redirectUri);
|
||||
param.add("scope", "app");
|
||||
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(param, headers);
|
||||
ResponseEntity<Map> response = restTemplate.postForEntity(accessTokenUri, request , Map.class);
|
||||
Map result = response.getBody();
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
*/
|
||||
public Map getUserInfo(String accessToken) {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
Map result = restTemplate.getForObject(userInfoUri+"?access_token="+accessToken, Map.class);
|
||||
return (Map)result.get("datas");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
server:
|
||||
port: 8081
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: sepSso-demo
|
||||
|
||||
zlt:
|
||||
sso:
|
||||
client-id: app
|
||||
client-secret: app
|
||||
redirect-uri: http://127.0.0.1:8081/login.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,43 @@
|
||||
<!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>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<p>用户名:<span id="userName"></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>
|
||||
//应用id
|
||||
let clientId = 'app';
|
||||
//授权中心地址
|
||||
let uaaUri = 'http://127.0.0.1:9900/api-uaa/oauth/';
|
||||
|
||||
window.onload = function() {
|
||||
let accessToken = sessionStorage.getItem('access_token');
|
||||
if (accessToken) {//已登录
|
||||
let username = sessionStorage.getItem('username');
|
||||
$('#accessToken').html(accessToken);
|
||||
$('#userName').html(username);
|
||||
$('#clientId').html(clientId);
|
||||
} else {//未登录
|
||||
sessionStorage.setItem("visitUri", window.location.href);
|
||||
window.location = uaaUri+'authorize?client_id='+clientId+'&redirect_uri=http://127.0.0.1:8081/login.html&response_type=code';
|
||||
}
|
||||
};
|
||||
|
||||
function logout() {
|
||||
let accessToken = sessionStorage.getItem('access_token');
|
||||
sessionStorage.removeItem('access_token');
|
||||
sessionStorage.removeItem('username');
|
||||
window.location = uaaUri+'remove/token?redirect_uri=http://127.0.0.1:8081/index.html&access_token='+accessToken;
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
File diff suppressed because one or more lines are too long
@@ -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>
|
||||
</head>
|
||||
<body>
|
||||
<script>
|
||||
window.onload = function() {
|
||||
//获取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 '';
|
||||
}
|
||||
|
||||
//url获取code
|
||||
let code = getQueryVariable('code');
|
||||
//获取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);
|
||||
window.location = sessionStorage.getItem('visitUri');
|
||||
}});
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user