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 52 */
53 53 @PostMapping("/logout")
54 54 public CommonResult<Boolean> logout(HttpServletRequest request) {
55   - String token = SecurityUtils.obtainAuthorization(request, "Authentication");
  55 + String token = SecurityUtils.obtainAuthorization(request, "Authorization");
56 56 if (StrUtil.isNotBlank(token)) {
57 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 32 protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
33 33 FilterChain filterChain) throws ServletException, IOException {
34 34 // 1. 获得访问令牌
35   - String token = SecurityUtils.obtainAuthorization(request, "Authentication");
  35 + String token = SecurityUtils.obtainAuthorization(request, "Authorization");
36 36 if (StringUtils.hasText(token)) {
37 37 // 2. 基于 token 构建登录用户
38 38 LoginUser loginUser = buildLoginUserByToken(token);
... ...
yudao-example/yudao-sso-demo-by-code/src/main/resources/static/callback.html
... ... @@ -9,8 +9,8 @@
9 9 <script type="application/javascript">
10 10 (function ($) {
11 11 /**
12   - * 获得 URL 的指定参数的值
13   - *
  12 + * 获得 URL 的指定参数的值
  13 + *
14 14 * @param name 参数名
15 15 * @returns 参数值
16 16 */
... ... @@ -25,17 +25,17 @@
25 25 <script type="application/javascript">
26 26 $(function () {
27 27 // 获得 code 授权码
28   - const code = $.getUrlParam('code');
  28 + const code = $.getUrlParam('code');
29 29 if (!code) {
30 30 alert('获取不到 code 参数,请排查!')
31 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 36 $.ajax({
37 37 url: "http://127.0.0.1:18080/auth/login-by-code?code=" + code
38   - + '&redirectUri=' + redirectUri,
  38 + + '&redirectUri=' + redirectUri,
39 39 method: 'POST',
40 40 success: function( result ) {
41 41 if (result.code !== 0) {
... ... @@ -52,7 +52,7 @@
52 52 window.location.href = '/index.html';
53 53 }
54 54 })
55   - })
  55 + })
56 56 </script>
57 57 </head>
58 58 <body>
... ...
yudao-example/yudao-sso-demo-by-code/src/main/resources/static/index.html
... ... @@ -8,20 +8,20 @@
8 8  
9 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 16 const redirectUri = encodeURIComponent('http://127.0.0.1:18080/callback.html'); // 注意,需要使用 encodeURIComponent 编码地址
17 17 const responseType = 'code'; // 1)授权码模式,对应 code;2)简化模式,对应 token
18 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 26 function updateNickname() {
27 27 const nickname = prompt("请输入新的昵称", "");
... ... @@ -34,7 +34,7 @@
34 34 url: "http://127.0.0.1:18080/user/update?nickname=" + nickname,
35 35 method: 'PUT',
36 36 headers: {
37   - 'Authentication': 'Bearer ' + accessToken
  37 + 'Authorization': 'Bearer ' + accessToken
38 38 },
39 39 success: function (result) {
40 40 if (result.code !== 0) {
... ... @@ -45,17 +45,17 @@
45 45 $('#nicknameSpan').html(nickname);
46 46 }
47 47 });
48   - }
  48 + }
49 49  
50 50 /**
51   - * 刷新令牌
  51 + * 刷新令牌
52 52 */
53 53 function refreshToken() {
54 54 const refreshToken = localStorage.getItem('REFRESH-TOKEN');
55 55 if (!refreshToken) {
56 56 alert("获取不到刷新令牌");
57 57 return;
58   - }
  58 + }
59 59 $.ajax({
60 60 url: "http://127.0.0.1:18080/auth/refresh-token?refreshToken=" + refreshToken,
61 61 method: 'POST',
... ... @@ -72,7 +72,7 @@
72 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 87 url: "http://127.0.0.1:18080/auth/logout",
88 88 method: 'POST',
89 89 headers: {
90   - 'Authentication': 'Bearer ' + accessToken
  90 + 'Authorization': 'Bearer ' + accessToken
91 91 },
92 92 success: function (result) {
93 93 if (result.code !== 0) {
... ... @@ -107,10 +107,10 @@
107 107 $(function () {
108 108 const accessToken = localStorage.getItem('ACCESS-TOKEN');
109 109 // 情况一:未登录
110   - if (!accessToken) {
  110 + if (!accessToken) {
111 111 $('#noLoginDiv').css("display", "block");
112 112 return;
113   - }
  113 + }
114 114  
115 115 // 情况二:已登录
116 116 $('#yesLoginDiv').css("display", "block");
... ... @@ -120,7 +120,7 @@
120 120 url: "http://127.0.0.1:18080/user/get",
121 121 method: 'GET',
122 122 headers: {
123   - 'Authentication': 'Bearer ' + accessToken
  123 + 'Authorization': 'Bearer ' + accessToken
124 124 },
125 125 success: function (result) {
126 126 if (result.code !== 0) {
... ... @@ -134,17 +134,17 @@
134 134 </script>
135 135 </head>
136 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 148 </body>
149 149 <style>
150 150 body { /** 页面居中 */
... ...