🎉 新增Oauth2单点登录功能

dev
smallchill 4 years ago
parent e296787589
commit 65c7f0c175
  1. 15
      src/api/user.js
  2. 4
      src/config/website.js
  3. 1
      src/lang/en.js
  4. 1
      src/lang/zh.js
  5. 17
      src/page/login/index.vue
  6. 25
      src/store/modules/user.js

@ -37,6 +37,21 @@ export const loginBySocial = (tenantId, source, code, state) => request({
}
})
export const loginBySso = (state, code) => request({
url: '/api/blade-auth/oauth/token',
method: 'post',
headers: {
'Tenant-Id': state
},
params: {
tenantId: state,
code,
grant_type: "authorization_code",
scope: "all",
redirect_uri: website.redirectUri,
}
})
export const refreshToken = (refresh_token, tenantId, deptId, roleId) => request({
url: '/api/blade-auth/oauth/token',
method: 'post',

@ -44,4 +44,8 @@ export default {
authUrl: 'http://localhost/blade-auth/oauth/render',
// 报表设计器地址(cloud端口为8108,boot端口为80)
reportUrl: 'http://localhost:8108/ureport',
// 单点登录系统认证(blade-auth服务的地)
ssoUrl: 'http://localhost:8100/oauth/authorize?client_id=saber&response_type=code&redirect_uri=',
// 单点登录回调地址(Saber服务的地址)
redirectUri: 'http://localhost:1888',
}

@ -81,6 +81,7 @@ export default {
userLogin: 'userLogin',
phoneLogin: 'phoneLogin',
thirdLogin: 'thirdLogin',
ssoLogin: 'ssoLogin',
msgText: 'send code',
msgSuccess: 'reissued code',
},

@ -81,6 +81,7 @@ export default {
userLogin: '账号密码登录',
phoneLogin: '手机号登录',
thirdLogin: '第三方系统登录',
ssoLogin: '单点系统登录',
msgText: '发送验证码',
msgSuccess: '秒后重发',
},

@ -45,6 +45,7 @@
<a href="#" @click.stop="activeName='user'">{{ $t('login.userLogin') }}</a>
<!--<a href="#" @click.stop="activeName='code'">{{ $t('login.phoneLogin') }}</a>-->
<a href="#" @click.stop="activeName='third'">{{ $t('login.thirdLogin') }}</a>
<a :href="website.ssoUrl + website.redirectUri">{{ $t('login.ssoLogin') }}</a>
</div>
</div>
@ -108,6 +109,7 @@
handleLogin() {
const topUrl = getTopUrl();
const redirectUrl = "/oauth/redirect/";
const ssoCode = "?code=";
this.socialForm.source = getQueryString("source");
this.socialForm.code = getQueryString("code");
this.socialForm.state = getQueryString("state");
@ -116,7 +118,7 @@
source = source.split(redirectUrl)[1];
this.socialForm.source = source;
}
if (!validatenull(this.socialForm.source) && !validatenull(this.socialForm.code) && !validatenull(this.socialForm.state)) {
if (topUrl.includes(redirectUrl) && !validatenull(this.socialForm.source) && !validatenull(this.socialForm.code) && !validatenull(this.socialForm.state)) {
const loading = this.$loading({
lock: true,
text: '第三方系统登录中,请稍后。。。',
@ -129,6 +131,19 @@
}).catch(() => {
loading.close();
});
} else if (!topUrl.includes(redirectUrl) && !validatenull(this.socialForm.code) && !validatenull(this.socialForm.state)) {
const loading = this.$loading({
lock: true,
text: '单点系统登录中,请稍后。。。',
spinner: "el-icon-loading"
});
this.$store.dispatch("LoginBySso", this.socialForm).then(() => {
window.location.href = topUrl.split(ssoCode)[0];
this.$router.push({path: this.tagWel.value});
loading.close();
}).catch(() => {
loading.close();
});
}
}
}

@ -4,7 +4,7 @@ import {setStore, getStore} from '@/util/store'
import {isURL, validatenull} from '@/util/validate'
import {deepClone} from '@/util/util'
import website from '@/config/website'
import {loginByUsername, loginBySocial, getUserInfo, logout, refreshToken, getButtons} from '@/api/user'
import {loginByUsername, loginBySocial, loginBySso, getUserInfo, logout, refreshToken, getButtons} from '@/api/user'
import {getTopMenu, getRoutes} from '@/api/system/menu'
import md5 from 'js-md5'
@ -95,6 +95,29 @@ const user = {
commit('SET_TOKEN', data.access_token);
commit('SET_REFRESH_TOKEN', data.refresh_token);
commit('SET_USER_INFO', data);
commit('SET_TENANT_ID', data.tenant_id);
commit('DEL_ALL_TAG');
commit('CLEAR_LOCK');
}
resolve();
})
})
},
//根据单点信息登录
LoginBySso({commit}, userInfo) {
return new Promise((resolve) => {
loginBySso(userInfo.state, userInfo.code).then(res => {
const data = res.data;
if (data.error_description) {
Message({
message: data.error_description,
type: 'error'
})
} else {
commit('SET_TOKEN', data.access_token);
commit('SET_REFRESH_TOKEN', data.refresh_token);
commit('SET_USER_INFO', data);
commit('SET_TENANT_ID', data.tenant_id);
commit('DEL_ALL_TAG');
commit('CLEAR_LOCK');
}

Loading…
Cancel
Save