From df866d4d278eb5ecbb5290be7cca3cf6b9b1a328 Mon Sep 17 00:00:00 2001 From: caolin <1149034574@qq.com> Date: Sat, 12 Aug 2023 04:04:46 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../server/DriverServerApplication.java | 8 +--- .../jwl/driver/server/config/CorsConfig.java | 39 ++++++++++--------- .../server/interceptor/AuthInterceptor.java | 6 +++ src/main/resources/application.yml | 4 +- 5 files changed, 32 insertions(+), 27 deletions(-) diff --git a/pom.xml b/pom.xml index cadcb4c..7def1df 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ 1.8 3.3.0 5.7.20 - 2.0.9 + 3.0.3 diff --git a/src/main/java/com/jwl/driver/server/DriverServerApplication.java b/src/main/java/com/jwl/driver/server/DriverServerApplication.java index 4b0d9d2..d47d91d 100644 --- a/src/main/java/com/jwl/driver/server/DriverServerApplication.java +++ b/src/main/java/com/jwl/driver/server/DriverServerApplication.java @@ -8,16 +8,12 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @ComponentScan(basePackages = {"com.jwl.driver.server.*"}) @SpringBootApplication -public class DriverServerApplication implements WebMvcConfigurer { +public class DriverServerApplication { public static void main(String[] args) { SpringApplication.run(DriverServerApplication.class, args); } - @Override - public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); - registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); - } + } diff --git a/src/main/java/com/jwl/driver/server/config/CorsConfig.java b/src/main/java/com/jwl/driver/server/config/CorsConfig.java index 3bed5b9..173928b 100644 --- a/src/main/java/com/jwl/driver/server/config/CorsConfig.java +++ b/src/main/java/com/jwl/driver/server/config/CorsConfig.java @@ -1,6 +1,7 @@ package com.jwl.driver.server.config; import com.jwl.driver.server.interceptor.AuthInterceptor; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; @@ -19,17 +20,17 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class CorsConfig implements WebMvcConfigurer { - @Bean - public AuthInterceptor initAuthInterceptor(){ - return new AuthInterceptor(); - } + @Autowired + private AuthInterceptor authInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(initAuthInterceptor()).addPathPatterns("/driver-api/**") - .excludePathPatterns("/login/**"); + registry.addInterceptor(authInterceptor).addPathPatterns("/**").excludePathPatterns("/**/login", +// "/error", + "/doc.html"); } + @Override public void addCorsMappings(CorsRegistry registry) { // 设置允许跨域的路径 @@ -46,20 +47,22 @@ public class CorsConfig implements WebMvcConfigurer { .maxAge(3600); } - @Bean - public CorsWebFilter corsWebFilter(){ - UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { - CorsConfiguration corsConfiguration = new CorsConfiguration(); - //1、配置跨域 - corsConfiguration.addAllowedHeader("*");//允许哪些头进行跨域 - corsConfiguration.addAllowedMethod("*");//允许哪些请求方式进行跨域 - corsConfiguration.addAllowedOriginPattern("*");//允许哪些请求来源进行跨域 - corsConfiguration.setAllowCredentials(true);//是否允许携带cookie进行跨域,否则跨域请求会丢失cookie信息 + registry.addResourceHandler("/**").addResourceLocations("classpath:/static/"); - source.registerCorsConfiguration("/**",corsConfiguration); + /** 配置knife4j 显示文档 */ + registry.addResourceHandler("doc.html") + .addResourceLocations("classpath:/META-INF/resources/"); - return new CorsWebFilter(source); + /** + * 配置swagger-ui显示文档 + */ + registry.addResourceHandler("swagger-ui.html") + .addResourceLocations("classpath:/META-INF/resources/"); + /** 公共部分内容 */ + registry.addResourceHandler("/webjars/**") + .addResourceLocations("classpath:/META-INF/resources/webjars/"); } - } diff --git a/src/main/java/com/jwl/driver/server/interceptor/AuthInterceptor.java b/src/main/java/com/jwl/driver/server/interceptor/AuthInterceptor.java index 87316cd..a092248 100644 --- a/src/main/java/com/jwl/driver/server/interceptor/AuthInterceptor.java +++ b/src/main/java/com/jwl/driver/server/interceptor/AuthInterceptor.java @@ -2,6 +2,7 @@ package com.jwl.driver.server.interceptor; import com.jwl.driver.server.redis.RedisCache; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; @@ -16,6 +17,7 @@ import java.util.Objects; * @create 2023/8/11 18:05 */ +@Component public class AuthInterceptor implements HandlerInterceptor { @Autowired @@ -23,6 +25,10 @@ public class AuthInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + String requestURI = request.getRequestURI(); + if (requestURI.contains("doc.html")){ + return true; + } response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); String token = request.getHeader("token"); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 8810fb9..d84c9b4 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,7 +1,7 @@ server: port: 8888 - servlet: - context-path: '/driver-api' +# servlet: +# context-path: '/driver-api' spring: application: