sso.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <div class="container">
  3. <div class="logo"></div>
  4. <!-- 登录区域 -->
  5. <div class="content">
  6. <!-- 配图 -->
  7. <div class="pic"></div>
  8. <!-- 表单 -->
  9. <div class="field">
  10. <!-- [移动端]标题 -->
  11. <h2 class="mobile-title">
  12. <h3 class="title">芋道后台管理系统</h3>
  13. </h2>
  14. <!-- 表单 -->
  15. <div class="form-cont">
  16. <el-tabs class="form" style=" float:none;" value="uname">
  17. <el-tab-pane :label="'三方授权(' + client.name + ')'" name="uname">
  18. </el-tab-pane>
  19. </el-tabs>
  20. <div>
  21. <el-form ref="loginForm" :model="loginForm" class="login-form">
  22. <!-- 授权范围的选择 -->
  23. 此第三方应用请求获得以下权限:
  24. <el-form-item prop="scopes">
  25. <el-checkbox-group v-model="loginForm.scopes">
  26. <el-checkbox v-for="scope in params.scopes" :label="scope" :key="scope"
  27. style="display: block; margin-bottom: -10px;">{{formatScope(scope)}}</el-checkbox>
  28. </el-checkbox-group>
  29. </el-form-item>
  30. <!-- 下方的登录按钮 -->
  31. <el-form-item style="width:100%;">
  32. <el-button :loading="loading" size="medium" type="primary" style="width:60%;"
  33. @click.native.prevent="handleAuthorize(true)">
  34. <span v-if="!loading">同意授权</span>
  35. <span v-else>授 权 中...</span>
  36. </el-button>
  37. <el-button size="medium" style="width:36%"
  38. @click.native.prevent="handleAuthorize(false)">拒绝</el-button>
  39. </el-form-item>
  40. </el-form>
  41. </div>
  42. </div>
  43. </div>
  44. </div>
  45. <!-- footer -->
  46. <div class="footer">
  47. Copyright © 2020-2022 iocoder.cn All Rights Reserved.
  48. </div>
  49. </div>
  50. </template>
  51. <script>
  52. import {authorize, getAuthorize} from "@/api/login";
  53. export default {
  54. name: "Login",
  55. data() {
  56. return {
  57. loginForm: {
  58. scopes: [], // 已选中的 scope 数组
  59. },
  60. params: { // URL 上的 client_id、scope 等参数
  61. responseType: undefined,
  62. clientId: undefined,
  63. redirectUri: undefined,
  64. state: undefined,
  65. scopes: [], // 优先从 query 参数获取;如果未传递,从后端获取
  66. },
  67. client: { // 客户端信息
  68. name: '',
  69. logo: '',
  70. },
  71. loading: false
  72. };
  73. },
  74. created() {
  75. // 解析参数
  76. // 例如说【自动授权不通过】:client_id=default&redirect_uri=https%3A%2F%2Fwww.iocoder.cn&response_type=code&scope=user.read%20user.write
  77. // 例如说【自动授权通过】:client_id=default&redirect_uri=https%3A%2F%2Fwww.iocoder.cn&response_type=code&scope=user.read
  78. this.params.responseType = this.$route.query.response_type
  79. this.params.clientId = this.$route.query.client_id
  80. this.params.redirectUri = this.$route.query.redirect_uri
  81. this.params.state = this.$route.query.state
  82. if (this.$route.query.scope) {
  83. this.params.scopes = this.$route.query.scope.split(' ')
  84. }
  85. // 如果有 scope 参数,先执行一次自动授权,看看是否之前都授权过了。
  86. if (this.params.scopes.length > 0) {
  87. this.doAuthorize(true, this.params.scopes, []).then(res => {
  88. const href = res.data
  89. if (!href) {
  90. console.log('自动授权未通过!')
  91. return;
  92. }
  93. location.href = href
  94. })
  95. }
  96. // 获取授权页的基本信息
  97. getAuthorize(this.params.clientId).then(res => {
  98. this.client = res.data.client
  99. // 解析 scope
  100. let scopes
  101. // 1.1 如果 params.scope 非空,则过滤下返回的 scopes
  102. if (this.params.scopes.length > 0) {
  103. scopes = []
  104. for (const scope of res.data.scopes) {
  105. if (this.params.scopes.indexOf(scope.key) >= 0) {
  106. scopes.push(scope)
  107. }
  108. }
  109. // 1.2 如果 params.scope 为空,则使用返回的 scopes 设置它
  110. } else {
  111. scopes = res.data.scopes
  112. for (const scope of scopes) {
  113. this.params.scopes.push(scope.key)
  114. }
  115. }
  116. // 生成已选中的 checkedScopes
  117. for (const scope of scopes) {
  118. if (scope.value) {
  119. this.loginForm.scopes.push(scope.key)
  120. }
  121. }
  122. })
  123. },
  124. methods: {
  125. handleAuthorize(approved) {
  126. this.$refs.loginForm.validate(valid => {
  127. if (!valid) {
  128. return
  129. }
  130. this.loading = true
  131. // 计算 checkedScopes + uncheckedScopes
  132. let checkedScopes;
  133. let uncheckedScopes;
  134. if (approved) { // 同意授权,按照用户的选择
  135. checkedScopes = this.loginForm.scopes
  136. uncheckedScopes = this.params.scopes.filter(item => checkedScopes.indexOf(item) === -1)
  137. } else { // 拒绝,则都是取消
  138. checkedScopes = []
  139. uncheckedScopes = this.params.scopes
  140. }
  141. // 提交授权的请求
  142. this.doAuthorize(false, checkedScopes, uncheckedScopes).then(res => {
  143. const href = res.data
  144. if (!href) {
  145. return;
  146. }
  147. location.href = href
  148. }).finally(() => {
  149. this.loading = false
  150. })
  151. })
  152. },
  153. doAuthorize(autoApprove, checkedScopes, uncheckedScopes) {
  154. return authorize(this.params.responseType, this.params.clientId, this.params.redirectUri, this.params.state,
  155. autoApprove, checkedScopes, uncheckedScopes)
  156. },
  157. formatScope(scope) {
  158. // 格式化 scope 授权范围,方便用户理解。
  159. // 这里仅仅是一个 demo,可以考虑录入到字典数据中,例如说字典类型 "system_oauth2_scope",它的每个 scope 都是一条字典数据。
  160. switch (scope) {
  161. case 'user.read': return '访问你的个人信息'
  162. case 'user.write': return '修改你的个人信息'
  163. default: return scope
  164. }
  165. }
  166. }
  167. };
  168. </script>
  169. <style lang="scss" scoped>
  170. @import "~@/assets/styles/login.scss";
  171. .oauth-login {
  172. display: flex;
  173. align-items: center;
  174. cursor:pointer;
  175. }
  176. .oauth-login-item {
  177. display: flex;
  178. align-items: center;
  179. margin-right: 10px;
  180. }
  181. .oauth-login-item img {
  182. height: 25px;
  183. width: 25px;
  184. }
  185. .oauth-login-item span:hover {
  186. text-decoration: underline red;
  187. color: red;
  188. }
  189. </style>