Browse Source

second init

qrs 1 month ago
parent
commit
06cd57b70d

+ 6 - 1
alien-second/pom.xml

@@ -264,7 +264,12 @@
             <groupId>shop.alien</groupId>
             <groupId>shop.alien</groupId>
             <artifactId>alien-entity</artifactId>
             <artifactId>alien-entity</artifactId>
             <version>1.0.0</version>
             <version>1.0.0</version>
-            <scope>compile</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>shop.alien</groupId>
+            <artifactId>alien-config</artifactId>
+            <version>1.0.0</version>
         </dependency>
         </dependency>
 
 
         <dependency>
         <dependency>

+ 8 - 0
alien-second/src/main/java/shop/alien/second/AlienSecondApplication.java

@@ -1,6 +1,7 @@
 package shop.alien.second;
 package shop.alien.second;
 
 
 import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
 import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
+import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.openfeign.EnableFeignClients;
 import org.springframework.cloud.openfeign.EnableFeignClients;
@@ -8,6 +9,13 @@ import org.springframework.context.annotation.ComponentScan;
 
 
 //@ComponentScan("shop.alien.second.*")
 //@ComponentScan("shop.alien.second.*")
 @EnableFeignClients(basePackages = {"shop.alien.second.feign"})
 @EnableFeignClients(basePackages = {"shop.alien.second.feign"})
+@ComponentScan({
+        "shop.alien.second.*",
+        "shop.alien.util.*",
+        "shop.alien.config.http",
+        "shop.alien.config.databases",
+        "shop.alien.config.feign"})
+@MapperScan({"shop.alien.mapper"})
 @EnableSwaggerBootstrapUI
 @EnableSwaggerBootstrapUI
 @SpringBootApplication
 @SpringBootApplication
 public class AlienSecondApplication {
 public class AlienSecondApplication {

+ 0 - 14
alien-second/src/main/java/shop/alien/second/config/FeignConfig.java

@@ -1,14 +0,0 @@
-package shop.alien.second.config;
-
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-@Configuration
-public class FeignConfig {
-
-    @Bean
-    public FeignTokenInterceptor feignTokenInterceptor() {
-        return new FeignTokenInterceptor();
-    }
-
-}

+ 0 - 56
alien-second/src/main/java/shop/alien/second/config/FeignOptionConfig.java

@@ -1,56 +0,0 @@
-package shop.alien.second.config;
-
-import feign.Logger;
-import feign.Request;
-import feign.codec.Decoder;
-import org.springframework.beans.factory.ObjectFactory;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
-import org.springframework.cloud.openfeign.support.SpringDecoder;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-/**
- * Openfeign配置
- */
-@Configuration
-public class FeignOptionConfig {
-
-    @Value("${openfeign.timeout.connect}")
-    private int connectTimeout;
-
-    @Value("${openfeign.timeout.read}")
-    private int readTimeout;
-
-    @Bean
-    public Request.Options options() {
-        return new Request.Options(connectTimeout, readTimeout, true);
-    }
-
-    /**
-     * OpenFeign打印日志, 需在配置文件中指定feign类位置
-     * <p>
-     * NONE, 默认,不显示任何日志
-     * BASIC, 仅记录请求方法、url、响应状态码及执行时间
-     * HEADERS, 除记录BASIC信息外,还记录请求头和响应头
-     * FULL,除了HEADERS信息外,还有请求和响应正文以及元数据
-     *
-     * @return 日志等级
-     */
-    @Bean
-    Logger.Level feignLoggerLevel() {
-        return Logger.Level.BASIC;
-//        return Logger.Level.FULL; // 必须设置为 FULL 才能获取响应体
-    }
-
-//    @Bean
-//    public FeignBodyLogger feignLogger() {
-//        return new FeignBodyLogger();
-//    }
-
-    @Bean
-    public Decoder feignDecoder(ObjectFactory<HttpMessageConverters> converters) {
-        return new GzipResponseDecoder(new SpringDecoder(converters));
-    }
-
-}

+ 0 - 28
alien-second/src/main/java/shop/alien/second/config/FeignTokenInterceptor.java

@@ -1,28 +0,0 @@
-package shop.alien.second.config;
-
-import feign.RequestInterceptor;
-import feign.RequestTemplate;
-import org.springframework.web.context.request.RequestContextHolder;
-import org.springframework.web.context.request.ServletRequestAttributes;
-
-public class FeignTokenInterceptor implements RequestInterceptor {
-
-    @Override
-    public void apply(RequestTemplate requestTemplate) {
-        // 从安全上下文中获取 Token(例如 JWT Token)
-        String token = getTokenFromSecurityContext();
-
-        // 将 Token 添加到请求头中
-        if (token != null) {
-            requestTemplate.header("Authorization", token);
-        }
-    }
-
-    private String getTokenFromSecurityContext() {
-        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
-        if (attributes != null) {
-            return attributes.getRequest().getHeader("Authorization");
-        }
-        return null;
-    }
-}

+ 0 - 75
alien-second/src/main/java/shop/alien/second/config/GzipResponseDecoder.java

@@ -1,75 +0,0 @@
-package shop.alien.second.config;
-
-import feign.Response;
-import feign.codec.Decoder;
-import lombok.extern.slf4j.Slf4j;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.lang.reflect.Type;
-import java.util.Collection;
-import java.util.zip.GZIPInputStream;
-
-/**
- * 实现支持 GZip 的解码器
- *
- * @author ssk
- * @version 1.0
- * @date 2025/3/19 9:47
- */
-@Slf4j
-public class GzipResponseDecoder implements Decoder {
-
-    private final Decoder delegate;
-
-    public GzipResponseDecoder(Decoder delegate) {
-        this.delegate = delegate;
-    }
-
-    @Override
-    public Object decode(Response response, Type type) throws IOException {
-        // 判断是否为 GZip 压缩响应
-        if (isGzipResponse(response)) {
-            try (InputStream is = response.body().asInputStream();
-                 GZIPInputStream gzipIs = new GZIPInputStream(is)) {
-                // 读取解压后的数据
-                ByteArrayOutputStream baos = new ByteArrayOutputStream();
-                byte[] buffer = new byte[1024];
-                int len;
-                while ((len = gzipIs.read(buffer)) > 0) {
-                    baos.write(buffer, 0, len);
-                }
-//                // 打印解压后的原始内容
-//                String rawBody = baos.toString("UTF-8");
-//                System.out.println("[GZip 解压内容] " + rawBody);
-                // 构造新的响应对象供后续解析
-                Response modifiedResponse = Response.builder()
-                        .body(baos.toByteArray())
-                        .headers(response.headers())
-                        .status(response.status())
-                        .request(response.request())
-                        .build();
-                return delegate.decode(modifiedResponse, type);
-            }
-        }
-        return delegate.decode(response, type);
-    }
-
-    /**
-     * 判断是否为 GZip 压缩响应
-     *
-     * @param response 请求内容
-     * @return 是否为 GZip 压缩响应
-     */
-    private boolean isGzipResponse(Response response) {
-        Collection<String> strings = response.headers().get("Content-Encoding");
-        if (null == strings) {
-            return false;
-        }
-        return response.headers().get("Content-Encoding")
-                .stream()
-                .anyMatch(enc -> enc.contains("gzip"));
-    }
-
-}

+ 0 - 109
alien-second/src/main/java/shop/alien/second/config/OkHttpConfig.java

@@ -1,109 +0,0 @@
-package shop.alien.second.config;
-
-import okhttp3.ConnectionPool;
-import okhttp3.OkHttpClient;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-import javax.net.ssl.*;
-import java.security.KeyManagementException;
-import java.security.NoSuchAlgorithmException;
-import java.security.SecureRandom;
-import java.security.cert.X509Certificate;
-import java.util.concurrent.TimeUnit;
-
-/**
- * OkHttp配置
- *
- * @author ssk
- * @version 1.0
- * @date 2024/10/27 10:29
- */
-@Configuration
-public class OkHttpConfig {
-
-    /**
-     * OkHttp 客户端配置
-     *
-     * @return OkHttp 客户端配
-     */
-    @Bean
-    public OkHttpClient okHttpClient() {
-        return new OkHttpClient.Builder()
-                .sslSocketFactory(sslSocketFactory(), x509TrustManager())
-                .hostnameVerifier(hostnameVerifier())
-                //是否开启缓存
-                .retryOnConnectionFailure(false)
-                //连接池
-                .connectionPool(pool())
-                //连接超时时间
-                .connectTimeout(15L, TimeUnit.SECONDS)
-                //读取超时时间
-                .readTimeout(15L, TimeUnit.SECONDS)
-                //是否允许重定向
-                .followRedirects(true)
-                .build();
-    }
-
-    /**
-     * 忽略证书校验
-     *
-     * @return 证书信任管理器
-     */
-    @Bean
-    public X509TrustManager x509TrustManager() {
-        return new X509TrustManager() {
-            @Override
-            public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
-            }
-
-            @Override
-            public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
-            }
-
-            @Override
-            public X509Certificate[] getAcceptedIssuers() {
-                return new X509Certificate[0];
-            }
-        };
-    }
-
-    /**
-     * 信任所有 SSL 证书
-     *
-     * @return
-     */
-    @Bean
-    public SSLSocketFactory sslSocketFactory() {
-        try {
-            TrustManager[] trustManagers = new TrustManager[]{x509TrustManager()};
-            SSLContext sslContext = SSLContext.getInstance("SSL");
-            sslContext.init(null, trustManagers, new SecureRandom());
-            return sslContext.getSocketFactory();
-        } catch (NoSuchAlgorithmException | KeyManagementException e) {
-            e.printStackTrace();
-        }
-        return null;
-    }
-
-    /**
-     * 连接池配置
-     *
-     * @return 连接池
-     */
-    @Bean
-    public ConnectionPool pool() {
-        // 最大连接数、连接存活时间、存活时间单位(分钟)
-        return new ConnectionPool(200, 5, TimeUnit.MINUTES);
-    }
-
-    /**
-     * 信任所有主机名
-     *
-     * @return 主机名校验
-     */
-    @Bean
-    public HostnameVerifier hostnameVerifier() {
-        return (s, sslSession) -> true;
-    }
-}

+ 4 - 1
alien-second/src/main/java/shop/alien/second/controller/TestController.java

@@ -18,7 +18,7 @@ import shop.alien.util.common.JwtUtil;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletRequest;
 
 
 @Slf4j
 @Slf4j
-@Api(tags = {"二期-阿里接口"})
+@Api(tags = {"二手平台-测试"})
 @ApiSort(1)
 @ApiSort(1)
 @CrossOrigin
 @CrossOrigin
 @RestController
 @RestController
@@ -31,6 +31,9 @@ public class TestController {
     @GetMapping("/test")
     @GetMapping("/test")
     public String test() {
     public String test() {
         JSONObject data = JwtUtil.getCurrentUserInfo();
         JSONObject data = JwtUtil.getCurrentUserInfo();
+        if (null != data) {
+            int userId = data.getInteger("userId");
+        }
         System.out.println(111);
         System.out.println(111);
         System.out.println(data);
         System.out.println(data);