Commit 6be30c0887117f67cb1cc577c65d7711df1b35a7

Authored by YunaiV
1 parent f634ecf7

优化 yudao-sso-demo-by-code 的代码

yudao-example/yudao-sso-demo-by-code/src/main/java/cn/iocoder/yudao/ssodemo/controller/AuthController.java
@@ -52,7 +52,7 @@ public class AuthController { @@ -52,7 +52,7 @@ public class AuthController {
52 */ 52 */
53 @PostMapping("/logout") 53 @PostMapping("/logout")
54 public CommonResult<Boolean> logout(HttpServletRequest request) { 54 public CommonResult<Boolean> logout(HttpServletRequest request) {
55 - String token = SecurityUtils.obtainAuthorization(request, "Authentication"); 55 + String token = SecurityUtils.obtainAuthorization(request, "Authorization");
56 if (StrUtil.isNotBlank(token)) { 56 if (StrUtil.isNotBlank(token)) {
57 return oauth2Client.revokeToken(token); 57 return oauth2Client.revokeToken(token);
58 } 58 }
yudao-example/yudao-sso-demo-by-code/src/main/java/cn/iocoder/yudao/ssodemo/framework/core/filter/TokenAuthenticationFilter.java
@@ -32,7 +32,7 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter { @@ -32,7 +32,7 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
32 protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, 32 protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
33 FilterChain filterChain) throws ServletException, IOException { 33 FilterChain filterChain) throws ServletException, IOException {
34 // 1. 获得访问令牌 34 // 1. 获得访问令牌
35 - String token = SecurityUtils.obtainAuthorization(request, "Authentication"); 35 + String token = SecurityUtils.obtainAuthorization(request, "Authorization");
36 if (StringUtils.hasText(token)) { 36 if (StringUtils.hasText(token)) {
37 // 2. 基于 token 构建登录用户 37 // 2. 基于 token 构建登录用户
38 LoginUser loginUser = buildLoginUserByToken(token); 38 LoginUser loginUser = buildLoginUserByToken(token);
yudao-example/yudao-sso-demo-by-code/src/main/resources/static/callback.html
@@ -9,8 +9,8 @@ @@ -9,8 +9,8 @@
9 <script type="application/javascript"> 9 <script type="application/javascript">
10 (function ($) { 10 (function ($) {
11 /** 11 /**
12 - * 获得 URL 的指定参数的值  
13 - * 12 + * 获得 URL 的指定参数的值
  13 + *
14 * @param name 参数名 14 * @param name 参数名
15 * @returns 参数值 15 * @returns 参数值
16 */ 16 */
@@ -25,17 +25,17 @@ @@ -25,17 +25,17 @@
25 <script type="application/javascript"> 25 <script type="application/javascript">
26 $(function () { 26 $(function () {
27 // 获得 code 授权码 27 // 获得 code 授权码
28 - const code = $.getUrlParam('code'); 28 + const code = $.getUrlParam('code');
29 if (!code) { 29 if (!code) {
30 alert('获取不到 code 参数,请排查!') 30 alert('获取不到 code 参数,请排查!')
31 return; 31 return;
32 - } 32 + }
33 33
34 // 提交 34 // 提交
35 - const redirectUri = 'http://127.0.0.1:18080/callback.html'; // 需要修改成,你回调的地址,就是在 index.html 拼接的 redirectUri 35 + const redirectUri = 'http://127.0.0.1:18080/callback.html'; // 需要修改成,你回调的地址,就是在 index.html 拼接的 redirectUri
36 $.ajax({ 36 $.ajax({
37 url: "http://127.0.0.1:18080/auth/login-by-code?code=" + code 37 url: "http://127.0.0.1:18080/auth/login-by-code?code=" + code
38 - + '&redirectUri=' + redirectUri, 38 + + '&redirectUri=' + redirectUri,
39 method: 'POST', 39 method: 'POST',
40 success: function( result ) { 40 success: function( result ) {
41 if (result.code !== 0) { 41 if (result.code !== 0) {
@@ -52,7 +52,7 @@ @@ -52,7 +52,7 @@
52 window.location.href = '/index.html'; 52 window.location.href = '/index.html';
53 } 53 }
54 }) 54 })
55 - }) 55 + })
56 </script> 56 </script>
57 </head> 57 </head>
58 <body> 58 <body>
yudao-example/yudao-sso-demo-by-code/src/main/resources/static/index.html
@@ -8,20 +8,20 @@ @@ -8,20 +8,20 @@
8 8
9 <script type="application/javascript"> 9 <script type="application/javascript">
10 10
11 - /**  
12 - * 跳转单点登录 11 + /**
  12 + * 跳转单点登录
13 */ 13 */
14 - function ssoLogin() {  
15 - const clientId = 'yudao-sso-demo-by-code'; // 可以改写成,你的 clientId 14 + function ssoLogin() {
  15 + const clientId = 'yudao-sso-demo-by-code'; // 可以改写成,你的 clientId
16 const redirectUri = encodeURIComponent('http://127.0.0.1:18080/callback.html'); // 注意,需要使用 encodeURIComponent 编码地址 16 const redirectUri = encodeURIComponent('http://127.0.0.1:18080/callback.html'); // 注意,需要使用 encodeURIComponent 编码地址
17 const responseType = 'code'; // 1)授权码模式,对应 code;2)简化模式,对应 token 17 const responseType = 'code'; // 1)授权码模式,对应 code;2)简化模式,对应 token
18 window.location.href = 'http://127.0.0.1:1024/sso?client_id=' + clientId 18 window.location.href = 'http://127.0.0.1:1024/sso?client_id=' + clientId
19 - + '&redirect_uri=' + redirectUri  
20 - + '&response_type=' + responseType; 19 + + '&redirect_uri=' + redirectUri
  20 + + '&response_type=' + responseType;
21 } 21 }
22 22
23 /** 23 /**
24 - * 修改昵称 24 + * 修改昵称
25 */ 25 */
26 function updateNickname() { 26 function updateNickname() {
27 const nickname = prompt("请输入新的昵称", ""); 27 const nickname = prompt("请输入新的昵称", "");
@@ -34,7 +34,7 @@ @@ -34,7 +34,7 @@
34 url: "http://127.0.0.1:18080/user/update?nickname=" + nickname, 34 url: "http://127.0.0.1:18080/user/update?nickname=" + nickname,
35 method: 'PUT', 35 method: 'PUT',
36 headers: { 36 headers: {
37 - 'Authentication': 'Bearer ' + accessToken 37 + 'Authorization': 'Bearer ' + accessToken
38 }, 38 },
39 success: function (result) { 39 success: function (result) {
40 if (result.code !== 0) { 40 if (result.code !== 0) {
@@ -45,17 +45,17 @@ @@ -45,17 +45,17 @@
45 $('#nicknameSpan').html(nickname); 45 $('#nicknameSpan').html(nickname);
46 } 46 }
47 }); 47 });
48 - } 48 + }
49 49
50 /** 50 /**
51 - * 刷新令牌 51 + * 刷新令牌
52 */ 52 */
53 function refreshToken() { 53 function refreshToken() {
54 const refreshToken = localStorage.getItem('REFRESH-TOKEN'); 54 const refreshToken = localStorage.getItem('REFRESH-TOKEN');
55 if (!refreshToken) { 55 if (!refreshToken) {
56 alert("获取不到刷新令牌"); 56 alert("获取不到刷新令牌");
57 return; 57 return;
58 - } 58 + }
59 $.ajax({ 59 $.ajax({
60 url: "http://127.0.0.1:18080/auth/refresh-token?refreshToken=" + refreshToken, 60 url: "http://127.0.0.1:18080/auth/refresh-token?refreshToken=" + refreshToken,
61 method: 'POST', 61 method: 'POST',
@@ -72,7 +72,7 @@ @@ -72,7 +72,7 @@
72 localStorage.setItem('REFRESH-TOKEN', result.data.refresh_token); 72 localStorage.setItem('REFRESH-TOKEN', result.data.refresh_token);
73 } 73 }
74 }); 74 });
75 - } 75 + }
76 76
77 /** 77 /**
78 * 刷新令牌 78 * 刷新令牌
@@ -87,7 +87,7 @@ @@ -87,7 +87,7 @@
87 url: "http://127.0.0.1:18080/auth/logout", 87 url: "http://127.0.0.1:18080/auth/logout",
88 method: 'POST', 88 method: 'POST',
89 headers: { 89 headers: {
90 - 'Authentication': 'Bearer ' + accessToken 90 + 'Authorization': 'Bearer ' + accessToken
91 }, 91 },
92 success: function (result) { 92 success: function (result) {
93 if (result.code !== 0) { 93 if (result.code !== 0) {
@@ -107,10 +107,10 @@ @@ -107,10 +107,10 @@
107 $(function () { 107 $(function () {
108 const accessToken = localStorage.getItem('ACCESS-TOKEN'); 108 const accessToken = localStorage.getItem('ACCESS-TOKEN');
109 // 情况一:未登录 109 // 情况一:未登录
110 - if (!accessToken) { 110 + if (!accessToken) {
111 $('#noLoginDiv').css("display", "block"); 111 $('#noLoginDiv').css("display", "block");
112 return; 112 return;
113 - } 113 + }
114 114
115 // 情况二:已登录 115 // 情况二:已登录
116 $('#yesLoginDiv').css("display", "block"); 116 $('#yesLoginDiv').css("display", "block");
@@ -120,7 +120,7 @@ @@ -120,7 +120,7 @@
120 url: "http://127.0.0.1:18080/user/get", 120 url: "http://127.0.0.1:18080/user/get",
121 method: 'GET', 121 method: 'GET',
122 headers: { 122 headers: {
123 - 'Authentication': 'Bearer ' + accessToken 123 + 'Authorization': 'Bearer ' + accessToken
124 }, 124 },
125 success: function (result) { 125 success: function (result) {
126 if (result.code !== 0) { 126 if (result.code !== 0) {
@@ -134,17 +134,17 @@ @@ -134,17 +134,17 @@
134 </script> 134 </script>
135 </head> 135 </head>
136 <body> 136 <body>
137 - <!-- 情况一:未登录:1)跳转 ruoyi-vue-pro 的 SSO 登录页 -->  
138 - <div id="noLoginDiv" style="display: none">  
139 - 您未登录,点击 <a href="#" onclick="ssoLogin()">跳转 </a> SSO 单点登录  
140 - </div> 137 +<!-- 情况一:未登录:1)跳转 ruoyi-vue-pro 的 SSO 登录页 -->
  138 +<div id="noLoginDiv" style="display: none">
  139 + 您未登录,点击 <a href="#" onclick="ssoLogin()">跳转 </a> SSO 单点登录
  140 +</div>
141 141
142 - <!-- 情况二:已登录:1)展示用户信息;2)刷新访问令牌;3)退出登录 -->  
143 - <div id="yesLoginDiv" style="display: none">  
144 - 您已登录!<button onclick="logout()">退出登录</button> <br />  
145 - 昵称:<span id="nicknameSpan"> 加载中... </span> <button onclick="updateNickname()">修改昵称</button> <br />  
146 - 访问令牌:<span id="accessTokenSpan"> 加载中... </span> <button onclick="refreshToken()">刷新令牌</button> <br />  
147 - </div> 142 +<!-- 情况二:已登录:1)展示用户信息;2)刷新访问令牌;3)退出登录 -->
  143 +<div id="yesLoginDiv" style="display: none">
  144 + 您已登录!<button onclick="logout()">退出登录</button> <br />
  145 + 昵称:<span id="nicknameSpan"> 加载中... </span> <button onclick="updateNickname()">修改昵称</button> <br />
  146 + 访问令牌:<span id="accessTokenSpan"> 加载中... </span> <button onclick="refreshToken()">刷新令牌</button> <br />
  147 +</div>
148 </body> 148 </body>
149 <style> 149 <style>
150 body { /** 页面居中 */ 150 body { /** 页面居中 */