浏览代码

王子文 Azure授权登录

wangggziwen 2 年之前
父节点
当前提交
df24773e32

+ 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","/getAccessToken").anonymous()
+                .antMatchers("/login", "/captchaImage","/getAzureAccessToken", "/getAccessToken").anonymous()
                 .antMatchers(
                         HttpMethod.GET,
                         "/*.html",

+ 41 - 0
master/src/main/java/com/ruoyi/project/system/controller/SysLoginController.java

@@ -138,4 +138,45 @@ public class SysLoginController {
         return ajax;
     }
 
+    /**
+     * 登录方法
+     *
+     * @param loginBody 登录信息
+     * @return 结果
+     */
+    @PostMapping("/getAzureAccessToken")
+    public AjaxResult getAzureAccessToken(@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", "FdR8Q~hmMJsJtJzPhDntTMwRv2WKD6dEhpSKraqk");
+        body.put("client_id", "3db6f125-db4d-456b-a76e-a6d03182e845");
+        body.put("redirect_uri", "http://localhost/cpms/index.html#/azureLogin");
+        // 2. 执行请求
+        ResponseEntity<AjaxResult> exchange = restTemplate.exchange(
+                "https://login.microsoftonline.com/7503e40a-97ec-4eb9-bf6d-2836e57e882d/oauth2/v2.0/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;
+    }
+
 }

+ 12 - 0
ui/src/api/login.js

@@ -50,3 +50,15 @@ export function getAccessToken(code) {
     data: data
   })
 }
+
+// Azure登录
+export function getAzureAccessToken(code) {
+  const data = {
+    code,
+  }
+  return request({
+    url: '/getAzureAccessToken',
+    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','/socialLogin', '/auth-redirect', '/bind', '/register']
+const whiteList = ['/login','/socialLogin', '/azureLogin', '/auth-redirect', '/bind', '/register']
 
 router.beforeEach((to, from, next) => {
   NProgress.start()

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

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

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

@@ -0,0 +1,135 @@
+<template>
+  <div class="login">
+  <span>Azure已授权。。正在登录CPMS</span>
+    <!--  底部  -->
+    <div class="el-login-footer">
+      <span>Copyright © 2020-2022 Seashore.ept All Rights Reserved.</span>
+    </div>
+  </div>
+</template>
+
+<script>
+import {getAzureAccessToken, 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: "AzureLogin",
+  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.getAzureAccessToken()
+  },
+  methods: {
+    getAzureAccessToken(){
+      getAzureAccessToken(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>

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

@@ -48,7 +48,7 @@
           <span v-else>{{ $t('login.loading') }}</span>
         </el-button>
       </el-form-item>
-        <el-form-item style="width:100%;">
+      <el-form-item style="width:100%;">
         <el-button
           :loading="loading"
           size="medium"
@@ -60,6 +60,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="doAzureLogin"
+        >
+          <span v-if="!loading"> Azure登录 </span>
+          <span v-else>{{ $t('login.loading') }}</span>
+        </el-button>
+      </el-form-item>
     </el-form>
     <!--  底部  -->
     <div class="el-login-footer">
@@ -163,6 +175,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';
     },
+    /** Azure登录 */
+    doAzureLogin() {
+        window.location.href = 'https://login.microsoftonline.com/7503e40a-97ec-4eb9-bf6d-2836e57e882d/oauth2/v2.0/authorize?client_id=3db6f125-db4d-456b-a76e-a6d03182e845&redirect_uri=http%3A%2F%2Flocalhost%2Fcpms%2Findex.html%23%2FazureLogin&scope=api://3db6f125-db4d-456b-a76e-a6d03182e845/User.Read&response_type=code';
+    },
   }
 };
 </script>