sso.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. tenantEnable: true,
  58. loginForm: {
  59. scopes: [], // 已选中的 scope 数组
  60. },
  61. params: { // URL 上的 client_id、scope 等参数
  62. responseType: undefined,
  63. clientId: undefined,
  64. redirectUri: undefined,
  65. state: undefined,
  66. scopes: [], // 优先从 query 参数获取;如果未传递,从后端获取
  67. },
  68. client: { // 客户端信息
  69. name: '',
  70. logo: '',
  71. },
  72. loading: false
  73. };
  74. },
  75. created() {
  76. // 解析参数
  77. // 例如说【自动授权不通过】:client_id=default&redirect_uri=https%3A%2F%2Fwww.iocoder.cn&response_type=code&scope=user.read%20user.write
  78. // 例如说【自动授权通过】:client_id=default&redirect_uri=https%3A%2F%2Fwww.iocoder.cn&response_type=code&scope=user.read
  79. this.params.responseType = this.$route.query.response_type
  80. this.params.clientId = this.$route.query.client_id
  81. this.params.redirectUri = this.$route.query.redirect_uri
  82. this.params.state = this.$route.query.state
  83. if (this.$route.query.scope) {
  84. this.params.scopes = this.$route.query.scope.split(' ')
  85. }
  86. // 如果有 scope 参数,先执行一次自动授权,看看是否之前都授权过了。
  87. if (this.params.scopes.length > 0) {
  88. this.doAuthorize(true, this.params.scopes, []).then(res => {
  89. const href = res.data
  90. if (!href) {
  91. console.log('自动授权未通过!')
  92. return;
  93. }
  94. location.href = href
  95. })
  96. }
  97. // 获取授权页的基本信息
  98. getAuthorize(this.params.clientId).then(res => {
  99. this.client = res.data.client
  100. // 解析 scope
  101. let scopes
  102. // 1.1 如果 params.scope 非空,则过滤下返回的 scopes
  103. if (this.params.scopes.length > 0) {
  104. scopes = []
  105. for (const scope of res.data.scopes) {
  106. if (this.params.scopes.indexOf(scope.key) >= 0) {
  107. scopes.push(scope)
  108. }
  109. }
  110. // 1.2 如果 params.scope 为空,则使用返回的 scopes 设置它
  111. } else {
  112. scopes = res.data.scopes
  113. for (const scope of scopes) {
  114. this.params.scopes.push(scope.key)
  115. }
  116. }
  117. // 生成已选中的 checkedScopes
  118. for (const scope of scopes) {
  119. if (scope.value) {
  120. this.loginForm.scopes.push(scope.key)
  121. }
  122. }
  123. })
  124. },
  125. methods: {
  126. handleAuthorize(approved) {
  127. this.$refs.loginForm.validate(valid => {
  128. if (!valid) {
  129. return
  130. }
  131. this.loading = true
  132. // 计算 checkedScopes + uncheckedScopes
  133. let checkedScopes;
  134. let uncheckedScopes;
  135. if (approved) { // 同意授权,按照用户的选择
  136. checkedScopes = this.loginForm.scopes
  137. uncheckedScopes = this.params.scopes.filter(item => checkedScopes.indexOf(item) === -1)
  138. } else { // 拒绝,则都是取消
  139. checkedScopes = []
  140. uncheckedScopes = this.params.scopes
  141. }
  142. // 提交授权的请求
  143. this.doAuthorize(false, checkedScopes, uncheckedScopes).then(res => {
  144. const href = res.data
  145. if (!href) {
  146. return;
  147. }
  148. location.href = href
  149. }).finally(() => {
  150. this.loading = false
  151. })
  152. })
  153. },
  154. doAuthorize(autoApprove, checkedScopes, uncheckedScopes) {
  155. return authorize(this.params.responseType, this.params.clientId, this.params.redirectUri, this.params.state,
  156. autoApprove, checkedScopes, uncheckedScopes)
  157. },
  158. formatScope(scope) {
  159. // 格式化 scope 授权范围,方便用户理解。
  160. // 这里仅仅是一个 demo,可以考虑录入到字典数据中,例如说字典类型 "system_oauth2_scope",它的每个 scope 都是一条字典数据。
  161. switch (scope) {
  162. case 'user.read': return '访问你的个人信息'
  163. case 'user.write': return '修改你的个人信息'
  164. default: return scope
  165. }
  166. }
  167. }
  168. };
  169. </script>
  170. <style lang="scss" scoped>
  171. @import "~@/assets/styles/login.scss";
  172. .oauth-login {
  173. display: flex;
  174. align-items: cen;
  175. cursor:pointer;
  176. }
  177. .oauth-login-item {
  178. display: flex;
  179. align-items: center;
  180. margin-right: 10px;
  181. }
  182. .oauth-login-item img {
  183. height: 25px;
  184. width: 25px;
  185. }
  186. .oauth-login-item span:hover {
  187. text-decoration: underline red;
  188. color: red;
  189. }
  190. </style>