Skip to content

Commit 2138612

Browse files
committed
新增 sso 页面
1 parent 2c367c0 commit 2138612

File tree

3 files changed

+107
-44
lines changed

3 files changed

+107
-44
lines changed

src/api/login.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,37 @@ export function refreshToken() {
111111
}
112112

113113
// ========== OAUTH 2.0 相关 ==========
114-
export function authorize() {
114+
115+
export function getAuthorize(clientId) {
116+
return request({
117+
url: '/system/oauth2/authorize?clientId=' + clientId,
118+
method: 'get'
119+
})
120+
}
121+
122+
export function authorize(responseType, clientId, redirectUri, state,
123+
autoApprove, checkedScopes, uncheckedScopes) {
124+
// 构建 scopes
125+
const scopes = {};
126+
for (const scope of checkedScopes) {
127+
scopes[scope] = true;
128+
}
129+
for (const scope of uncheckedScopes) {
130+
scopes[scope] = false;
131+
}
132+
// 发起请求
115133
return service({
116134
url: '/system/oauth2/authorize',
117135
headers:{
118136
'Content-type': 'application/x-www-form-urlencoded',
119-
"Access-Control-Allow-Origin": "*"
120137
},
121138
params: {
122-
response_type: 'code',
123-
client_id: 'test',
124-
redirect_uri: 'https://www.iocoder.cn',
125-
// scopes: {
126-
// read: true,
127-
// write: false
128-
// }
129-
scope: {
130-
read: true,
131-
write: false
132-
}
139+
response_type: responseType,
140+
client_id: clientId,
141+
redirect_uri: redirectUri,
142+
state: state,
143+
auto_approve: autoApprove,
144+
scope: JSON.stringify(scopes)
133145
},
134146
method: 'post'
135147
})

src/router/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ export const constantRoutes = [
4343
hidden: true
4444
},
4545
{
46-
path: '/authorize',
47-
component: (resolve) => require(['@/views/authorize'], resolve),
46+
path: '/sso',
47+
component: (resolve) => require(['@/views/sso'], resolve),
4848
hidden: true
4949
},
5050
{

src/views/authorize.vue renamed to src/views/sso.vue

Lines changed: 80 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<!-- 表单 -->
1616
<div class="form-cont">
17-
<el-tabs class="form" style=" float:none;">
17+
<el-tabs class="form" style=" float:none;" value="uname">
1818
<el-tab-pane label="三方授权" name="uname">
1919
</el-tab-pane>
2020
</el-tabs>
@@ -53,7 +53,7 @@
5353
<span v-if="!loading">同意授权</span>
5454
<span v-else>登 录 中...</span>
5555
</el-button>
56-
<el-button size="medium" style="width:37%">拒绝</el-button>
56+
<el-button size="medium" style="width:36%">拒绝</el-button>
5757
</el-form-item>
5858
</el-form>
5959
</div>
@@ -69,10 +69,9 @@
6969

7070
<script>
7171
import {getTenantIdByName} from "@/api/system/tenant";
72-
import Cookies from "js-cookie";
73-
import {SystemUserSocialTypeEnum} from "@/utils/constants";
7472
import {getTenantEnable} from "@/utils/ruoyi";
75-
import {authorize} from "@/api/login";
73+
import {authorize, getAuthorize} from "@/api/login";
74+
import {getTenantName, setTenantId} from "@/utils/auth";
7675
7776
export default {
7877
name: "Login",
@@ -82,6 +81,18 @@ export default {
8281
loginForm: {
8382
tenantName: "芋道源码",
8483
},
84+
params: { // URL 上的 client_id、scope 等参数
85+
responseType: undefined,
86+
clientId: undefined,
87+
redirectUri: undefined,
88+
state: undefined,
89+
scopes: [], // 优先从 query 参数获取;如果未传递,从后端获取
90+
},
91+
client: { // 客户端信息
92+
name: '',
93+
logo: '',
94+
},
95+
checkedScopes: [], // 已选中的 scope 数组
8596
LoginRules: {
8697
tenantName: [
8798
{required: true, trigger: "blur", message: "租户不能为空"},
@@ -92,7 +103,7 @@ export default {
92103
const tenantId = res.data;
93104
if (tenantId && tenantId >= 0) {
94105
// 设置租户
95-
Cookies.set("tenantId", tenantId);
106+
setTenantId(tenantId)
96107
callback();
97108
} else {
98109
callback('租户不存在');
@@ -104,44 +115,84 @@ export default {
104115
]
105116
},
106117
loading: false,
107-
redirect: undefined,
108-
// 枚举
109-
SysUserSocialTypeEnum: SystemUserSocialTypeEnum,
118+
//
110119
};
111120
},
112121
created() {
113122
// 租户开关
114123
this.tenantEnable = getTenantEnable();
115-
// 重定向地址
116-
this.redirect = this.$route.query.redirect;
117124
this.getCookie();
125+
126+
// 解析参数
127+
// 例如说【自动授权不通过】:client_id=default&redirect_uri=https%3A%2F%2Fwww.iocoder.cn&response_type=code&scope=user.read%20user.write
128+
// 例如说【自动授权通过】:client_id=default&redirect_uri=https%3A%2F%2Fwww.iocoder.cn&response_type=code&scope=user.read
129+
this.params.responseType = this.$route.query.response_type
130+
this.params.clientId = this.$route.query.client_id
131+
this.params.redirectUri = this.$route.query.redirect_uri
132+
this.params.state = this.$route.query.state
133+
if (this.$route.query.scope) {
134+
this.params.scopes = this.$route.query.scope.split(' ')
135+
}
136+
137+
// 如果有 scope 参数,先执行一次自动授权,看看是否之前都授权过了。
138+
if (this.params.scopes.length > 0) {
139+
this.doAuthorize(true, this.params.scopes, []).then(res => {
140+
const href = res.data
141+
if (!href) {
142+
console.log('自动授权未通过!')
143+
return;
144+
}
145+
location.href = href
146+
})
147+
}
148+
149+
// 获取授权页的基本信息
150+
getAuthorize(this.params.clientId).then(res => {
151+
this.client = res.data.client
152+
// 解析 scope
153+
let scopes
154+
// 1.1 如果 params.scope 非空,则过滤下返回的 scopes
155+
if (this.params.scopes.length > 0) {
156+
scopes = []
157+
for (const scope of res.data.scopes) {
158+
if (this.params.scopes.indexOf(scope.key) >= 0) {
159+
scopes.push(scope)
160+
}
161+
}
162+
// 1.2 如果 params.scope 为空,则使用返回的 scopes 设置它
163+
} else {
164+
scopes = res.data.scopes
165+
for (const scope of scopes) {
166+
this.params.scopes.push(scope.key)
167+
}
168+
}
169+
// 生成已选中的 checkedScopes
170+
for (const scope of scopes) {
171+
if (scope.value) {
172+
this.checkedScopes.push(scope.key)
173+
}
174+
}
175+
})
118176
},
119177
methods: {
120178
getCookie() {
121-
const tenantName = Cookies.get('tenantName');
179+
const tenantName = getTenantName();
122180
this.loginForm = {
123-
tenantName: tenantName === undefined ? this.loginForm.tenantName : tenantName
181+
tenantName: tenantName ? tenantName : this.loginForm.tenantName,
124182
};
125183
},
126184
handleLogin() {
127-
if (true) {
128-
authorize()
129-
return;
130-
}
131185
this.$refs.loginForm.validate(valid => {
132-
if (valid) {
133-
this.loading = true;
134-
// 发起登陆
135-
console.log("发起登录", this.loginForm);
136-
this.$store.dispatch(this.loginForm.loginType === "sms" ? "SmsLogin" : "Login", this.loginForm).then(() => {
137-
this.$router.push({path: this.redirect || "/"}).catch(() => {
138-
});
139-
}).catch(() => {
140-
this.loading = false;
141-
this.getCode();
142-
});
186+
if (!valid) {
187+
return
143188
}
144-
});
189+
190+
191+
})
192+
},
193+
doAuthorize(autoApprove, checkedScopes, uncheckedScopes) {
194+
return authorize(this.params.responseType, this.params.clientId, this.params.redirectUri, this.params.state,
195+
autoApprove, checkedScopes, uncheckedScopes)
145196
}
146197
}
147198
};

0 commit comments

Comments
 (0)