Prechádzať zdrojové kódy

1、新增通过url获取用户信息接口

fuwb 3 mesiacov pred
rodič
commit
18aa5af850

+ 137 - 0
ais_auth/pom.xml

@@ -0,0 +1,137 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<parent>
+		<groupId>org.springframework.boot</groupId>
+		<artifactId>spring-boot-starter-parent</artifactId>
+		<version>2.7.15</version>
+		<relativePath/> <!-- lookup parent from repository -->
+	</parent>
+	<groupId>com.zjugis.ai</groupId>
+	<artifactId>auth</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+	<name>knowledge</name>
+	<description>用户认证项目</description>
+	<properties>
+		<java.version>1.8</java.version>
+		<spring-cloud.version>2024.0.0</spring-cloud.version>
+		<maven.compiler.source>8</maven.compiler.source>
+		<maven.compiler.target>8</maven.compiler.target>
+	</properties>
+	<dependencies>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-web</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>com.baomidou</groupId>
+			<artifactId>mybatis-plus-boot-starter</artifactId>
+			<version>3.5.3.2</version>
+		</dependency>
+		<!-- Spring Cloud Starter OpenFeign -->
+		<dependency>
+			<groupId>org.springframework.cloud</groupId>
+			<artifactId>spring-cloud-starter-openfeign</artifactId>
+			<version>3.1.5</version>
+		</dependency>
+		<dependency>
+			<groupId>org.postgresql</groupId>
+			<artifactId>postgresql</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>com.alibaba</groupId>
+			<artifactId>druid-spring-boot-starter</artifactId>
+			<version>1.2.19</version>
+		</dependency>
+		<dependency>
+			<groupId>com.baomidou</groupId>
+			<artifactId>dynamic-datasource-spring-boot-starter</artifactId> <!-- 多数据源 -->
+			<version>3.6.1</version>
+		</dependency>
+		<!-- 工具类相关 -->
+		<dependency>
+			<groupId>org.projectlombok</groupId>
+			<artifactId>lombok</artifactId>
+			<version>1.18.28</version>
+		</dependency>
+		<dependency>
+			<groupId>cn.hutool</groupId>
+			<artifactId>hutool-all</artifactId>
+			<version>5.8.20</version>
+		</dependency>
+		<dependency>
+			<groupId>com.google.guava</groupId>
+			<artifactId>guava</artifactId>
+			<version>32.0.1-jre</version>
+		</dependency>
+		<dependency>
+			<groupId>com.github.yulichang</groupId>
+			<artifactId>mybatis-plus-join-boot-starter</artifactId> <!-- MyBatis 联表查询 -->
+			<version>1.4.6</version>
+		</dependency>
+		<dependency>
+			<groupId>com.github.lianjiatech</groupId>
+			<artifactId>retrofit-spring-boot-starter</artifactId>
+			<version>2.3.6</version>
+			<exclusions>
+				<exclusion>
+					<groupId>com.squareup.okio</groupId>
+					<artifactId>okio</artifactId>
+				</exclusion>
+			</exclusions>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.tika</groupId>
+			<artifactId>tika-core</artifactId>
+			<version>2.7.0</version>
+		</dependency>
+		<dependency>
+			<groupId>com.squareup.okhttp3</groupId>
+			<artifactId>okhttp</artifactId>
+			<version>4.9.3</version>
+		</dependency>
+		<dependency>
+			<groupId>com.alibaba</groupId>
+			<artifactId>transmittable-thread-local</artifactId> <!-- 解决 ThreadLocal 父子线程的传值问题 -->
+			<version>2.14.2</version>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-test</artifactId>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>com.alibaba</groupId>
+			<artifactId>fastjson</artifactId>
+			<version>1.2.83</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.httpcomponents</groupId>
+			<artifactId>httpclient</artifactId>
+			<version>4.5</version>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<!-- 设置构建的 jar 包名 -->
+		<finalName>${project.artifactId}</finalName>
+		<plugins>
+			<!-- 打包 -->
+			<plugin>
+				<groupId>org.springframework.boot</groupId>
+				<artifactId>spring-boot-maven-plugin</artifactId>
+				<version>2.7.15</version> <!-- 如果 spring.boot.version 版本修改,则这里也要跟着修改 -->
+				<executions>
+					<execution>
+						<goals>
+							<goal>repackage</goal> <!-- 将引入的 jar 打入其中 -->
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+		</plugins>
+	</build>
+
+</project>

+ 18 - 0
ais_auth/src/main/java/com/zjugis/auth/AuthApplication.java

@@ -0,0 +1,18 @@
+package com.zjugis.auth;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * @author: Fuwb
+ * @date: 2025/3/10
+ * @time: 13:56
+ * @description:
+ */
+@SpringBootApplication
+public class AuthApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(AuthApplication.class, args);
+    }
+}

+ 56 - 0
ais_auth/src/main/java/com/zjugis/auth/controller/AuthController.java

@@ -0,0 +1,56 @@
+package com.zjugis.auth.controller;
+
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.http.HttpUtil;
+import cn.hutool.json.JSONUtil;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.web.bind.annotation.CrossOrigin;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author: Fuwb
+ * @date: 2025/3/10
+ * @time: 10:55
+ * @description: 用户登录接口层
+ */
+@RestController
+@RequestMapping("/auth")
+public class AuthController {
+    private static final int TIME_OUT = 30 * 1000;
+    @Value("${syUrl}")
+    private String syUrl;
+
+    /**
+     * 通过携带ticket的url获取用户信息
+     *
+     * @param url
+     * @return
+     * @throws Exception
+     */
+    @CrossOrigin(origins = "*")
+    @GetMapping("/ticket")
+    public Map getUserBySyToken(String url) throws Exception {
+        if (StrUtil.isBlank(url)) {
+            Map hashMap = new HashMap<>(3);
+            hashMap.put("result", "");
+            hashMap.put("success", false);
+            hashMap.put("message", "url地址无效");
+            return hashMap;
+        }
+
+        int lastIndexOf = url.lastIndexOf("?");
+        String ticketParam = url.substring(lastIndexOf);
+        String[] tickets = ticketParam.split("=");
+        String ticket = tickets[1];
+        String newUrl = syUrl + "?ticket=" + ticket;
+        String json = HttpUtil.get(newUrl, TIME_OUT);
+        Map map = JSONUtil.toBean(json, Map.class);
+        return map;
+    }
+
+}

+ 196 - 0
ais_auth/src/main/java/com/zjugis/auth/utils/RequestUtils.java

@@ -0,0 +1,196 @@
+package com.zjugis.auth.utils;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.serializer.ValueFilter;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+
+import java.io.BufferedReader;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.util.Map;
+
+/**
+ * @author ljy
+ * @version 1.0
+ * @date 2022/5/7 15:36
+ */
+@Slf4j
+public class RequestUtils {
+
+    public static String get(String urlStr,String params) {
+
+        String responseMsg = "";
+
+        try {
+            // 访问地址url对象
+            URL url = new URL(urlStr +"?"+ params);
+            // 创建网络连接对象 http
+            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+            // 设置请求方式
+            connection.setRequestMethod("GET");
+            // 设置超时时间 3秒
+            connection.setConnectTimeout(3000);
+            // 设置是否自行http重定向
+            connection.setInstanceFollowRedirects(true);
+            // 设置是否使用缓存
+            connection.setDefaultUseCaches(true);
+            // 设置是否从HttpURLConnection获取输出
+            connection.setDoOutput(true);
+            // 设置是否从HttpURLConnection输入
+            connection.setDoInput(true);
+            // 创建连接
+            connection.connect();
+            // 获取响应码
+            int responseCode = connection.getResponseCode();
+
+            // 判断是否成功响应
+            if (responseCode == 200) {
+                // 从响应流中读取数据
+                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
+                String line = "";
+                while ((line = reader.readLine()) != null) {
+                    responseMsg += line;
+                }
+                // 关闭数据流
+                reader.close();
+            }
+            // 断开连接
+            connection.disconnect();
+
+            return responseMsg;
+
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return responseMsg;
+
+    }
+
+    public static String httpURLConnectionPOST(String urlStr, Map<String, Object> map) throws Exception {
+        HttpURLConnection connection = getHttpURLConnection(urlStr, map);
+        StringBuilder sb = new StringBuilder();
+        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
+            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
+            String str = null;
+            while ((str = in.readLine()) != null) {
+                sb.append(str);
+            }
+        } else {
+            throw new Exception("请求返回:" + connection.getResponseCode());
+        }
+        return sb.toString();
+    }
+
+    public static Map<String, Object> httpURLConnectionPOSTJson(String urlStr, Object obj) throws Exception {
+        JSONObject jsonObject = new JSONObject();
+        try (final CloseableHttpClient httpClient = HttpClients.createDefault()) {
+            final HttpPost httpPost = new HttpPost(urlStr);
+            //设置请求体参数
+            StringEntity entity = new StringEntity(JSONObject.toJSONString(obj),"UTF-8");
+            entity.setContentEncoding("utf-8");
+            httpPost.setEntity(entity);
+            //设置请求头部
+            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
+            //执行请求,返回请求响应
+            try (final CloseableHttpResponse response = httpClient.execute(httpPost)) {
+                //请求返回状态码
+                int statusCode = response.getStatusLine().getStatusCode();
+                //请求成功
+                if (statusCode == HttpStatus.SC_OK && statusCode <= HttpStatus.SC_TEMPORARY_REDIRECT) {
+                    //取出响应体
+                    final HttpEntity entity2 = response.getEntity();
+                    //从响应体中解析出token
+                    String responseBody = EntityUtils.toString(entity2, "utf-8");
+                    jsonObject = JSONObject.parseObject(responseBody);
+                    //token = jsonObject.getString("access_token");
+                } else {
+                    //请求失败
+                    throw new ClientProtocolException("请求失败,响应码为:" + statusCode);
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return jsonObject;
+    }
+
+    private static HttpURLConnection getHttpURLConnectionJson(String urlStr, Map<String, Object> map) throws IOException {
+        URL url = new URL(urlStr);
+
+        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+        connection.setDoOutput(true);
+        connection.setDoInput(true);
+        connection.setRequestMethod("POST");
+        connection.setUseCaches(false);
+        connection.setInstanceFollowRedirects(true);
+        connection.setRequestProperty("Content-Type", "application/json");
+        connection.connect();
+        try (DataOutputStream dataout = new DataOutputStream(connection.getOutputStream())) {
+            StringBuilder params = new StringBuilder();
+            if (null != map) {
+                for (String keymap : map.keySet()) {
+                    if (null != map.get(keymap)) {
+                        params.append(keymap).append("=");
+                        if (map.get(keymap) instanceof String) {
+                            params.append(URLEncoder.encode(String.valueOf(map.get(keymap)), "utf-8")).append("&");
+                        } else {
+                            params.append(URLEncoder.encode(JSON.toJSONString(map.get(keymap), (ValueFilter) (o, s, value) -> value == null ? "" : value), "utf-8")).append("&");
+                        }
+                    }
+                }
+            }
+            if (params.length() > 0) {
+                dataout.writeBytes(params.toString());
+            }
+        }
+        return connection;
+    }
+
+
+    private static HttpURLConnection getHttpURLConnection(String urlStr, Map<String, Object> map) throws IOException {
+        URL url = new URL(urlStr);
+
+        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+        connection.setDoOutput(true);
+        connection.setDoInput(true);
+        connection.setRequestMethod("POST");
+        connection.setUseCaches(false);
+        connection.setInstanceFollowRedirects(true);
+        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
+        connection.connect();
+        try (DataOutputStream dataout = new DataOutputStream(connection.getOutputStream())) {
+            StringBuilder params = new StringBuilder();
+            if (null != map) {
+                for (String keymap : map.keySet()) {
+                    if (null != map.get(keymap)) {
+                        params.append(keymap).append("=");
+                        if (map.get(keymap) instanceof String) {
+                            params.append(URLEncoder.encode(String.valueOf(map.get(keymap)), "utf-8")).append("&");
+                        } else {
+                            params.append(URLEncoder.encode(JSON.toJSONString(map.get(keymap), (ValueFilter) (o, s, value) -> value == null ? "" : value), "utf-8")).append("&");
+                        }
+                    }
+                }
+            }
+            if (params.length() > 0) {
+                dataout.writeBytes(params.toString());
+            }
+        }
+        return connection;
+    }
+}

+ 14 - 0
ais_auth/src/main/resources/application.yml

@@ -0,0 +1,14 @@
+server:
+  port: 10020
+
+spring:
+  main:
+    allow-bean-definition-overriding: true
+
+syUrl: https://kjzl.zrzyt.zj.gov.cn/bev2/api/Ticket/ValidateTicket
+
+
+
+
+
+