瀏覽代碼

LY 第三方登录

ly 2 年之前
父節點
當前提交
fab56d794d

+ 1 - 1
master/src/main/java/com/ruoyi/framework/config/SecurityConfig.java

@@ -99,7 +99,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
                 // 过滤请求
                 .authorizeRequests()
                 // 对于登录login 验证码captchaImage 允许匿名访问
-                .antMatchers("/login", "/captchaImage").anonymous()
+                .antMatchers("/login", "/captchaImage","/getAccessToken").anonymous()
                 .antMatchers(
                         HttpMethod.GET,
                         "/*.html",

+ 50 - 1
master/src/main/java/com/ruoyi/project/system/controller/SysLoginController.java

@@ -1,8 +1,13 @@
 package com.ruoyi.project.system.controller;
 
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.ParameterizedTypeReference;
+import org.springframework.http.*;
+import org.springframework.util.Assert;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -18,6 +23,7 @@ import com.ruoyi.framework.web.domain.AjaxResult;
 import com.ruoyi.project.system.domain.SysMenu;
 import com.ruoyi.project.system.domain.SysUser;
 import com.ruoyi.project.system.service.ISysMenuService;
+import org.springframework.web.client.RestTemplate;
 
 /**
  * 登录验证
@@ -38,7 +44,8 @@ public class SysLoginController
 
     @Autowired
     private TokenService tokenService;
-
+    //    @Resource // 可优化,注册一个 RestTemplate Bean,然后注入
+    private final RestTemplate restTemplate = new RestTemplate();
     /**
      * 登录方法
      *
@@ -91,4 +98,46 @@ public class SysLoginController
         List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
         return AjaxResult.success(menuService.buildMenus(menus));
     }
+
+    /**
+     * 登录方法
+     *
+     * @param loginBody 登录信息
+     * @return 结果
+     */
+    @PostMapping("/getAccessToken")
+    public AjaxResult getAccessToken(@RequestBody LoginBody loginBody)
+    {
+        AjaxResult ajax = AjaxResult.success();
+        // 生成令牌
+        String code = loginBody.getCode();
+
+// 1.1 构建请求头
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_JSON);
+        headers.add("Authorization", "Bearer " );
+        // 1.2 构建请求参数
+        Map<String,String> body = new HashMap<>();
+        body.put("code" ,code );
+        body.put("grant_type" ,"authorization_code" );
+        body.put("client_secret" ,"12102a6a3290fd2cf3aedf631d771d48ccc474501bea71d47627fe985c34aa8c" );
+        body.put("client_id" ,"e7faeabf239846288ee07e6c40066cbd0dcc46cb1c1dea37c602c29a2368c6b8" );
+        body.put("redirect_uri" ,"http://localhost/cpms/index.html#/socialLogin" );
+        // 2. 执行请求
+        ResponseEntity<AjaxResult> exchange = restTemplate.exchange(
+                 "https://gitee.com/oauth/token",
+                HttpMethod.POST,
+                new HttpEntity<>(body, headers),
+                new ParameterizedTypeReference<AjaxResult>() {}); // 解决 CommonResult 的泛型丢失
+        Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
+        ajax = exchange.getBody();
+        System.out.println(ajax.toString());
+        ajax.get("access_token");
+        //进行jwt解析
+
+        //系统登录 获取系统token
+
+        return ajax;
+    }
+
 }

+ 13 - 1
ui/src/api/login.js

@@ -37,4 +37,16 @@ export function getCodeImg() {
     url: '/captchaImage',
     method: 'get'
   })
-}
+}
+
+// 登录方法
+export function getAccessToken(code) {
+  const data = {
+    code,
+  }
+  return request({
+    url: '/getAccessToken',
+    method: 'post',
+    data: data
+  })
+}

+ 1 - 1
ui/src/permission.js

@@ -7,7 +7,7 @@ import { getToken } from '@/utils/auth'
 
 NProgress.configure({ showSpinner: false })
 
-const whiteList = ['/login', '/auth-redirect', '/bind', '/register']
+const whiteList = ['/login','/socialLogin', '/auth-redirect', '/bind', '/register']
 
 router.beforeEach((to, from, next) => {
   NProgress.start()

+ 5 - 0
ui/src/router/index.js

@@ -46,6 +46,11 @@ export const constantRoutes = [
     component: (resolve) => require(['@/views/login'], resolve),
     hidden: true
   },
+  {
+    path: '/socialLogin',
+    component: (resolve) => require(['@/views/socialLogin'], resolve),
+    hidden: true
+  },
   {
     path: '/404',
     component: (resolve) => require(['@/views/error/404'], resolve),

+ 16 - 1
ui/src/views/login.vue

@@ -48,6 +48,18 @@
           <span v-else>{{ $t('login.loading') }}</span>
         </el-button>
       </el-form-item>
+        <el-form-item style="width:100%;">
+        <el-button
+          :loading="loading"
+          size="medium"
+          type="primary"
+          style="width:100%;"
+          @click.native.prevent="doSocialLogin"
+        >
+          <span v-if="!loading"> 员工卡登录 </span>
+          <span v-else>{{ $t('login.loading') }}</span>
+        </el-button>
+      </el-form-item>
     </el-form>
     <!--  底部  -->
     <div class="el-login-footer">
@@ -147,7 +159,10 @@ export default {
             });
         }
       });
-    }
+    },
+    doSocialLogin() {
+        window.location.href = 'https://gitee.com/oauth/authorize?client_id=e7faeabf239846288ee07e6c40066cbd0dcc46cb1c1dea37c602c29a2368c6b8&redirect_uri=http%3A%2F%2Flocalhost%2Fcpms%2Findex.html%23%2FsocialLogin&response_type=code';
+    },
   }
 };
 </script>

+ 135 - 0
ui/src/views/socialLogin.vue

@@ -0,0 +1,135 @@
+<template>
+  <div class="login">
+  <span>已授权。。正在登录CPMS</span>
+    <!--  底部  -->
+    <div class="el-login-footer">
+      <span>Copyright © 2020-2022 Seashore.ept All Rights Reserved.</span>
+    </div>
+  </div>
+</template>
+
+<script>
+import {getAccessToken, getCodeImg} from "@/api/login";
+import Cookies from "js-cookie";
+import { encrypt, decrypt } from '@/utils/jsencrypt'
+import LangSelect from '@/components/LangSelect'
+import {updatePlantproglist} from "@/api/document/plantproglist";
+
+export default {
+  name: "SocialLogin",
+  components: { LangSelect },
+  data() {
+    return {
+      code: null,
+      loading: false,
+    };
+  },
+
+  created() {
+    let code = this.$route.query.code
+    if (!code) {
+      code = window.location.search.replace("?code=" , '')
+    }
+    this.code = code
+    console.log(this.code)
+    this.getAccessToken()
+  },
+  methods: {
+    getAccessToken(){
+      getAccessToken(this.code).then(response => {
+        this.msgSuccess(this.$t('成功'));
+      });
+    }
+
+  }
+};
+</script>
+
+<style rel="stylesheet/scss" lang="scss">
+.login {
+  display: flex;
+  justify-content: center;
+  align-items: center;
+  height: 100%;
+  background-size: cover;
+}
+.title {
+  margin: 0px auto 15px auto;
+  text-align: center;
+  color: #ffffff;
+}
+
+.login-form {
+  position:absolute;
+  top:50%;
+  left:50%;
+  transform:translate(-50%,-50%);
+  /*实现块元素百分比下居中*/
+  width:450px;
+  padding:50px;
+  background: #2a8db9db;
+  box-sizing:border-box;
+  box-shadow: 0px 15px 25px rgba(0,0,0,.5);
+  border-radius:15px;
+  .el-input {
+    height: 38px;
+    input {
+      height: 38px;
+    }
+  }
+  .input-icon {
+    height: 39px;
+    width: 14px;
+    margin-left: 2px;
+  }
+}
+.login-tip {
+  font-size: 13px;
+  text-align: center;
+  color: #bfbfbf;
+}
+.login-code {
+  width: 33%;
+  height: 38px;
+  float: right;
+  img {
+    cursor: pointer;
+    vertical-align: middle;
+  }
+}
+.el-dropdown {
+  color: #ffffff;
+}
+.el-checkbox {
+  color: #ffffff;
+}
+.el-login-footer {
+  height: 40px;
+  line-height: 40px;
+  position: fixed;
+  bottom: 0;
+  width: 100%;
+  text-align: center;
+  color: #fff;
+  font-family: Arial;
+  font-size: 12px;
+  letter-spacing: 1px;
+}
+.login-code-img {
+  height: 38px;
+}
+
+</style>
+
+<style scoped>
+  .el-button--primary {
+    color: #FFFFFF;
+    background-color: #40a9ff;
+    border-color: #40a9ff;
+  }
+
+  .el-button:hover, .el-button:focus {
+    border-color: #6abfff;
+    background-color: #6abfff;
+  }
+</style>