callback.html 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>SSO 授权后的回调页</title>
  6. <!-- jQuery:操作 dom、发起请求等 -->
  7. <script src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/2.1.2/jquery.min.js" type="application/javascript"></script>
  8. <!-- 工具类 -->
  9. <script type="application/javascript">
  10. (function ($) {
  11. /**
  12. * 获得 URL 的指定参数的值
  13. *
  14. * @param name 参数名
  15. * @returns 参数值
  16. */
  17. $.getUrlParam = function (name) {
  18. const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  19. const r = window.location.search.substr(1).match(reg);
  20. if (r != null) return unescape(r[2]); return null;
  21. }
  22. })(jQuery);
  23. </script>
  24. <script type="application/javascript">
  25. $(function () {
  26. // 获得 code 授权码
  27. const code = $.getUrlParam('code');
  28. if (!code) {
  29. alert('获取不到 code 参数,请排查!')
  30. return;
  31. }
  32. // 提交
  33. const redirectUri = 'http://127.0.0.1:18080/callback.html'; // 需要修改成,你回调的地址,就是在 index.html 拼接的 redirectUri
  34. $.ajax({
  35. url: "http://127.0.0.1:18080/auth/login-by-code?code=" + code
  36. + '&redirectUri=' + redirectUri,
  37. method: 'POST',
  38. success: function( result ) {
  39. if (result.code !== 0) {
  40. alert('获得访问令牌失败,原因:' + result.msg)
  41. return;
  42. }
  43. alert('获得访问令牌成功!点击确认,跳转回首页')
  44. // 设置到 localStorage 中
  45. localStorage.setItem('ACCESS-TOKEN', result.data.access_token);
  46. localStorage.setItem('REFRESH-TOKEN', result.data.refresh_token);
  47. // 跳转回首页
  48. window.location.href = '/index.html';
  49. }
  50. })
  51. })
  52. </script>
  53. </head>
  54. <body>
  55. 正在使用 code 授权码,进行 accessToken 访问令牌的获取
  56. </body>
  57. </html>