Skip to content

Commit 8b4dcf4

Browse files
YunaiVgitee-org
authored andcommitted
!272 增加“基于授权码模式,实现 SSO 单点登录“示例
Merge pull request !272 from 芋道源码/feature/sso-example
2 parents dc3a17c + 1522c7d commit 8b4dcf4

File tree

4 files changed

+5
-45
lines changed

4 files changed

+5
-45
lines changed

src/permission.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ router.beforeEach((to, from, next) => {
4848
// 在免登录白名单,直接进入
4949
next()
5050
} else {
51-
next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
51+
const redirect = encodeURIComponent(to.fullPath) // 编码 URI,保证参数跳转回去后,可以继续带上
52+
next(`/login?redirect=${redirect}`) // 否则全部重定向到登录页
5253
NProgress.done()
5354
}
5455
}

src/views/login.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export default {
190190
// 验证码开关
191191
this.captchaEnable = getCaptchaEnable();
192192
// 重定向地址
193-
this.redirect = this.$route.query.redirect;
193+
this.redirect = this.$route.query.redirect ? decodeURIComponent(this.$route.query.redirect) : undefined;
194194
this.getCookie();
195195
},
196196
methods: {

src/views/socialLogin.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export default {
117117
// 验证码开关
118118
this.captchaEnable = getCaptchaEnable();
119119
// 重定向地址
120-
this.redirect = this.$route.query.redirect;
120+
this.redirect = this.$route.query.redirect ? decodeURIComponent(this.$route.query.redirect) : undefined;
121121
// 社交登录相关
122122
this.type = this.$route.query.type;
123123
this.code = this.$route.query.code;

src/views/sso.vue

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@
1919
</el-tab-pane>
2020
</el-tabs>
2121
<div>
22-
<el-form ref="loginForm" :model="loginForm" :rules="LoginRules" class="login-form">
23-
<el-form-item prop="tenantName" v-if="tenantEnable">
24-
<el-input v-model="loginForm.tenantName" type="text" auto-complete="off" placeholder='租户'>
25-
<svg-icon slot="prefix" icon-class="tree" class="el-input__icon input-icon"/>
26-
</el-input>
27-
</el-form-item>
22+
<el-form ref="loginForm" :model="loginForm" class="login-form">
2823
<!-- 授权范围的选择 -->
2924
此第三方应用请求获得以下权限:
3025
<el-form-item prop="scopes">
@@ -56,18 +51,14 @@
5651
</template>
5752

5853
<script>
59-
import {getTenantIdByName} from "@/api/system/tenant";
60-
import {getTenantEnable} from "@/utils/ruoyi";
6154
import {authorize, getAuthorize} from "@/api/login";
62-
import {getTenantName, setTenantId} from "@/utils/auth";
6355
6456
export default {
6557
name: "Login",
6658
data() {
6759
return {
6860
tenantEnable: true,
6961
loginForm: {
70-
tenantName: "芋道源码",
7162
scopes: [], // 已选中的 scope 数组
7263
},
7364
params: { // URL 上的 client_id、scope 等参数
@@ -81,35 +72,10 @@ export default {
8172
name: '',
8273
logo: '',
8374
},
84-
LoginRules: {
85-
tenantName: [
86-
{required: true, trigger: "blur", message: "租户不能为空"},
87-
{
88-
validator: (rule, value, callback) => {
89-
// debugger
90-
getTenantIdByName(value).then(res => {
91-
const tenantId = res.data;
92-
if (tenantId && tenantId >= 0) {
93-
// 设置租户
94-
setTenantId(tenantId)
95-
callback();
96-
} else {
97-
callback('租户不存在');
98-
}
99-
});
100-
},
101-
trigger: 'blur'
102-
}
103-
]
104-
},
10575
loading: false
10676
};
10777
},
10878
created() {
109-
// 租户开关
110-
this.tenantEnable = getTenantEnable();
111-
this.getCookie();
112-
11379
// 解析参数
11480
// 例如说【自动授权不通过】:client_id=default&redirect_uri=https%3A%2F%2Fwww.iocoder.cn&response_type=code&scope=user.read%20user.write
11581
// 例如说【自动授权通过】:client_id=default&redirect_uri=https%3A%2F%2Fwww.iocoder.cn&response_type=code&scope=user.read
@@ -162,13 +128,6 @@ export default {
162128
})
163129
},
164130
methods: {
165-
getCookie() {
166-
const tenantName = getTenantName();
167-
this.loginForm = {
168-
...this.loginForm,
169-
tenantName: tenantName ? tenantName : this.loginForm.tenantName,
170-
};
171-
},
172131
handleAuthorize(approved) {
173132
this.$refs.loginForm.validate(valid => {
174133
if (!valid) {

0 commit comments

Comments
 (0)