Commit 384b4a5cf13e4de3f7f8655b2ae54e0f11085e54

Authored by weir
1 parent 133cb49c

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

yudao-admin-ui/src/api/login.js
... ... @@ -40,9 +40,9 @@ export function getCodeImg() {
40 40 }
41 41  
42 42 // 接入第三方登录
43   -export function giteeLogin() {
  43 +export function oAuthLogin(provider) {
44 44 return request({
45   - url: '/auth2/authorization/gitee',
  45 + url: '/auth2/authorization/' + provider,
46 46 method: 'get'
47 47 })
48 48 }
... ... @@ -50,7 +50,7 @@ export function giteeLogin() {
50 50 export function getToken(path) {
51 51 console.log({path});
52 52 return request({
53   - url: '/auth2/login/gitee' + path,
  53 + url: '/auth2/login/github' + path,
54 54 method: 'get'
55 55 })
56 56 }
... ...
yudao-admin-ui/src/views/login.vue
... ... @@ -45,10 +45,14 @@
45 45 <span v-else>登 录 中...</span>
46 46 </el-button>
47 47 </el-form-item>
  48 +
48 49 <el-form-item style="width:100%;">
49   - <el-button @click="doAuth2Login" title="使用 Gitee 帐号登录">
50   - <span class="gitee-login-title">使用 Gitee 帐号登录</span>
51   - </el-button>
  50 + <div class="oauth-login" style="display:flex">
  51 + <div class="oauth-login-item" v-for="item in oauthProviders" :key="item.code" @click="doAuth2Login(item)">
  52 + <img :src=item.img height="25px" width="25px" alt="登录" >
  53 + <span>{{item.title}}</span>
  54 + </div>
  55 + </div>
52 56 </el-form-item>
53 57 </el-form>
54 58 <!-- 底部 -->
... ... @@ -59,7 +63,7 @@
59 63 </template>
60 64  
61 65 <script>
62   -import { getCodeImg,giteeLogin } from "@/api/login";
  66 +import { getCodeImg,oAuthLogin } from "@/api/login";
63 67 import Cookies from "js-cookie";
64 68 import { encrypt, decrypt } from '@/utils/jsencrypt'
65 69  
... ... @@ -76,6 +80,23 @@ export default {
76 80 code: "",
77 81 uuid: ""
78 82 },
  83 + oauthProviders: [
  84 + {
  85 + img: "https://cdn.jsdelivr.net/gh/justauth/justauth-oauth-logo@1.2/gitee.png",
  86 + title: "码云",
  87 + code: "gitee"
  88 + },
  89 + {
  90 + img: "https://cdn.jsdelivr.net/gh/justauth/justauth-oauth-logo@1.2/wechat.png",
  91 + title: "微信",
  92 + code: "weixin"
  93 + },
  94 + {
  95 + img: "https://cdn.jsdelivr.net/gh/justauth/justauth-oauth-logo@1.2/qq.png",
  96 + title: "QQ",
  97 + code: "qq"
  98 + }
  99 + ],
79 100 loginRules: {
80 101 username: [
81 102 { required: true, trigger: "blur", message: "用户名不能为空" }
... ... @@ -141,10 +162,10 @@ export default {
141 162 }
142 163 });
143 164 },
144   - doAuth2Login() {
145   - console.log("开始Oauth登录...");
  165 + doAuth2Login(provider) {
  166 + console.log("开始Oauth登录...%o", provider.code);
146 167 this.loading = true;
147   - giteeLogin().then((res) => {
  168 + oAuthLogin(provider.code).then((res) => {
148 169 console.log(res.url);
149 170 window.location.href = res.url;
150 171 });
... ... @@ -214,4 +235,21 @@ export default {
214 235 .login-code-img {
215 236 height: 38px;
216 237 }
  238 +.oauth-login {
  239 + display: flex;
  240 + cursor:pointer;
  241 +}
  242 +.oauth-login-item {
  243 + display: flex;
  244 + align-items: center;
  245 + margin-right: 10px;
  246 +}
  247 +.oauth-login-item img {
  248 + height: 25px;
  249 + width: 25px;
  250 +}
  251 +.oauth-login-item span:hover {
  252 + text-decoration: underline red;
  253 + color: red;
  254 +}
217 255 </style>
... ...
yudao-admin-ui/src/views/oauthLogin.vue
1 1 <template>
2   - <div>登录成功!</div>
3 2 </template>
4 3  
5 4 <script>
6   -import { getToken, setToken, removeToken } from '@/utils/auth'
  5 +import { setToken } from '@/utils/auth'
7 6  
8 7 export default {
9 8 created() {
10   - var token = this.getOauthToken();
  9 + const {token} = this.$route.query;
11 10 setToken(token);
12 11 this.$router.push({ path: this.redirect || "/" }).catch(() => {});
13 12 },
14   - methods: {
15   - getOauthToken() {
16   - console.log(this);
17   - var h = window.location.href;
18   - var token = "token=";
19   - return h.substring(h.indexOf(token)+token.length);
20   - }
21   - },
22 13 }
23 14 </script>
24 15  
... ...
yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/LoginUser.java
... ... @@ -8,6 +8,7 @@ import org.springframework.security.core.userdetails.UserDetails;
8 8  
9 9 import java.util.Collection;
10 10 import java.util.Date;
  11 +import java.util.HashSet;
11 12 import java.util.Set;
12 13  
13 14 /**
... ... @@ -69,7 +70,7 @@ public class LoginUser implements UserDetails {
69 70 @Override
70 71 @JsonIgnore// 避免序列化
71 72 public Collection<? extends GrantedAuthority> getAuthorities() {
72   - return null;
  73 + return new HashSet<>();
73 74 }
74 75  
75 76 @Override
... ...