Browse Source

单点登录

zyl 1 month ago
parent
commit
823cd120f3

+ 58 - 27
liutongyi-admin/src/main/java/com/citygis/web/controller/GetTokenController.java

@@ -6,21 +6,23 @@ import com.citygis.common.core.domain.model.LoginBody;
 import com.citygis.common.enums.BusinessType;
 import com.citygis.web.domain.TabUser;
 import com.citygis.web.service.ITabUserService;
+import com.citygis.web.service.SingleLoginService;
 import com.citygis.web.utils.AesUtil;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.CrossOrigin;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.reactive.function.client.WebClient;
 import reactor.core.publisher.Mono;
 
 import javax.annotation.Resource;
-import java.util.List;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import java.io.IOException;
 
 import static com.citygis.common.utils.SecurityUtils.getUserId;
 import static com.citygis.common.utils.SecurityUtils.getUsername;
@@ -46,39 +48,68 @@ public class GetTokenController {
     @Resource
     ITabUserService tabUserService;
 
+    @Resource
+    SingleLoginService singleLoginService;
+
+
     @Log(title = "获取catalogToken", businessType = BusinessType.OTHER)
     @ApiOperation("获取catalogToken")
     @PostMapping("/getCatalogToken")
     public Mono<ResponseEntity<String>> forwardPostRequest() {
-        LoginBody loginBody = new LoginBody();
+        // 获取当前用户ID和用户名
         Long userId = getUserId();
+        String username = getUsername();
+
+        // 查询用户信息
+        TabUser user = getUserById(userId);
+        if (user == null) {
+            return Mono.just(ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found"));
+        }
+
+        // 解密密码
+        String decryptedPassword;
+        try {
+            decryptedPassword = AesUtil.aesDecrypt(user.getPassword());
+        } catch (Exception e) {
+            return Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to decrypt password"));
+        }
+
+        // 构建请求体
+        LoginBody loginBody = new LoginBody();
+        loginBody.setUsername(username);
+        loginBody.setPassword(decryptedPassword);
 
-        LambdaQueryWrapper<TabUser> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(TabUser::getUserId, userId);
-        List<TabUser> list = tabUserService.list();
-
-        TabUser one = tabUserService.getOne(wrapper);
-
-        String s = AesUtil.aesDecrypt(one.getPassword());
-
-        loginBody.setPassword(s);
-
-        loginBody.setUsername(getUsername());
         // 目标URL
         String targetUrl = url + "/token";  // 替换为实际的目标URL
 
-        // 创建WebClient实例
-        WebClient webClient = webClientBuilder.baseUrl(targetUrl).build();
-
-        // 使用WebClient发送POST请求,并携带请求体
-        return webClient.post()
-                .uri("")  // 目标URL的路径部分
-                .bodyValue(loginBody)  // 将User对象作为请求体
+        // 使用WebClient发送POST请求
+        return webClientBuilder.baseUrl(targetUrl).build()
+                .post()
+                .uri("")
+                .bodyValue(loginBody)
                 .retrieve()
                 .toEntity(String.class)
-                .map(response -> ResponseEntity
-                        .status(response.getStatusCode())
-                        .body(response.getBody()));
+                .map(response -> ResponseEntity.status(response.getStatusCode()).body(response.getBody()));
+    }
+
+    // 提取获取用户信息的方法
+    private TabUser getUserById(Long userId) {
+        LambdaQueryWrapper<TabUser> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(TabUser::getUserId, userId);
+        return tabUserService.getOne(wrapper);
+    }
+
+    /**
+     * 单点登录,页面初始化调用
+     *
+     * @param request
+     * @param response
+     * @throws IOException
+     */
+    @RequestMapping("/singleLogin")
+    public void singleLogin(HttpServletRequest request,
+                            HttpServletResponse response) throws IOException {
+        singleLoginService.singleLogin(request, response);
     }
 
 }

+ 11 - 0
liutongyi-admin/src/main/java/com/citygis/web/service/SingleLoginService.java

@@ -0,0 +1,11 @@
+package com.citygis.web.service;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+public interface SingleLoginService {
+
+    void singleLogin(HttpServletRequest request, HttpServletResponse response) throws IOException;
+
+}

+ 47 - 0
liutongyi-admin/src/main/java/com/citygis/web/service/impl/SingleLoginServiceImpl.java

@@ -0,0 +1,47 @@
+package com.citygis.web.service.impl;
+
+import com.citygis.web.service.SingleLoginService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * @Author: zyl
+ * @CreateTime: 2025-02-05
+ * @Description: 单点登录
+ * @Version: 1.0
+ */
+@Service
+@Slf4j
+public class SingleLoginServiceImpl implements SingleLoginService {
+
+    @Value("${singleLogin.ssoRet}")
+    private String ssoRet;
+
+    @Value("${singleLogin.clientId}")
+    private String clientId;
+
+    @Value("${singleLogin.url}")
+    private String url;
+
+    @Override
+    public void singleLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
+
+        //拼接url,ssoRet为派拉请求登陆认证地址,redirect_uri为跳转应用地址
+        String returnURL = ssoRet +
+                "client_id=" + clientId + "&" +
+                "response_type=code" +
+                "&redirect_uri=" + URLEncoder.encode(url, StandardCharsets.UTF_8.toString()) +
+                "&oauth_timestamp=" + System.currentTimeMillis();
+
+        log.info("成功------>" + returnURL);
+
+        response.sendRedirect(returnURL);
+    }
+}

+ 8 - 0
liutongyi-admin/src/main/resources/application.yml

@@ -154,5 +154,13 @@ jmx:
 
 #周报
 weekReport:
+  #  模板路径
   templateFilePath: E:\Project\2024\2024 liutongyi\2024 liutongyi\liutongyi-admin\src\main\resources\templates\模版.xlsx
+  #  周报生成路径
   filepath: C:\Users\Administrator\Desktop\疾控周报\
+
+#单点登录配置
+singleLogin:
+  clientId:
+  url:
+  ssoRet: https://iam.scdc.sh.cegn.cn/esc-sso/oauth2.0/authorize?