接入第三方登录--前端页面兼容多个第三方登录

pull/2/head
weir 2021-08-14 21:16:20 +08:00
parent 133cb49c42
commit 384b4a5cf1
4 changed files with 52 additions and 22 deletions

View File

@ -40,9 +40,9 @@ export function getCodeImg() {
} }
// 接入第三方登录 // 接入第三方登录
export function giteeLogin() { export function oAuthLogin(provider) {
return request({ return request({
url: '/auth2/authorization/gitee', url: '/auth2/authorization/' + provider,
method: 'get' method: 'get'
}) })
} }
@ -50,7 +50,7 @@ export function giteeLogin() {
export function getToken(path) { export function getToken(path) {
console.log({path}); console.log({path});
return request({ return request({
url: '/auth2/login/gitee' + path, url: '/auth2/login/github' + path,
method: 'get' method: 'get'
}) })
} }

View File

@ -45,10 +45,14 @@
<span v-else> ...</span> <span v-else> ...</span>
</el-button> </el-button>
</el-form-item> </el-form-item>
<el-form-item style="width:100%;"> <el-form-item style="width:100%;">
<el-button @click="doAuth2Login" title="使用 Gitee 帐号登录"> <div class="oauth-login" style="display:flex">
<span class="gitee-login-title">使用 Gitee 帐号登录</span> <div class="oauth-login-item" v-for="item in oauthProviders" :key="item.code" @click="doAuth2Login(item)">
</el-button> <img :src=item.img height="25px" width="25px" alt="登录" >
<span>{{item.title}}</span>
</div>
</div>
</el-form-item> </el-form-item>
</el-form> </el-form>
<!-- 底部 --> <!-- 底部 -->
@ -59,7 +63,7 @@
</template> </template>
<script> <script>
import { getCodeImg,giteeLogin } from "@/api/login"; import { getCodeImg,oAuthLogin } from "@/api/login";
import Cookies from "js-cookie"; import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt' import { encrypt, decrypt } from '@/utils/jsencrypt'
@ -76,6 +80,23 @@ export default {
code: "", code: "",
uuid: "" uuid: ""
}, },
oauthProviders: [
{
img: "https://cdn.jsdelivr.net/gh/justauth/justauth-oauth-logo@1.2/gitee.png",
title: "码云",
code: "gitee"
},
{
img: "https://cdn.jsdelivr.net/gh/justauth/justauth-oauth-logo@1.2/wechat.png",
title: "微信",
code: "weixin"
},
{
img: "https://cdn.jsdelivr.net/gh/justauth/justauth-oauth-logo@1.2/qq.png",
title: "QQ",
code: "qq"
}
],
loginRules: { loginRules: {
username: [ username: [
{ required: true, trigger: "blur", message: "用户名不能为空" } { required: true, trigger: "blur", message: "用户名不能为空" }
@ -141,10 +162,10 @@ export default {
} }
}); });
}, },
doAuth2Login() { doAuth2Login(provider) {
console.log("开始Oauth登录..."); console.log("开始Oauth登录...%o", provider.code);
this.loading = true; this.loading = true;
giteeLogin().then((res) => { oAuthLogin(provider.code).then((res) => {
console.log(res.url); console.log(res.url);
window.location.href = res.url; window.location.href = res.url;
}); });
@ -214,4 +235,21 @@ export default {
.login-code-img { .login-code-img {
height: 38px; height: 38px;
} }
.oauth-login {
display: flex;
cursor:pointer;
}
.oauth-login-item {
display: flex;
align-items: center;
margin-right: 10px;
}
.oauth-login-item img {
height: 25px;
width: 25px;
}
.oauth-login-item span:hover {
text-decoration: underline red;
color: red;
}
</style> </style>

View File

@ -1,24 +1,15 @@
<template> <template>
<div>登录成功</div>
</template> </template>
<script> <script>
import { getToken, setToken, removeToken } from '@/utils/auth' import { setToken } from '@/utils/auth'
export default { export default {
created() { created() {
var token = this.getOauthToken(); const {token} = this.$route.query;
setToken(token); setToken(token);
this.$router.push({ path: this.redirect || "/" }).catch(() => {}); this.$router.push({ path: this.redirect || "/" }).catch(() => {});
}, },
methods: {
getOauthToken() {
console.log(this);
var h = window.location.href;
var token = "token=";
return h.substring(h.indexOf(token)+token.length);
}
},
} }
</script> </script>

View File

@ -8,6 +8,7 @@ import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
/** /**
@ -69,7 +70,7 @@ public class LoginUser implements UserDetails {
@Override @Override
@JsonIgnore// 避免序列化 @JsonIgnore// 避免序列化
public Collection<? extends GrantedAuthority> getAuthorities() { public Collection<? extends GrantedAuthority> getAuthorities() {
return null; return new HashSet<>();
} }
@Override @Override