项目初始化

pull/1/head
caolin 2023-08-12 04:04:46 +08:00
parent b8e65ad0d3
commit df866d4d27
5 changed files with 32 additions and 27 deletions

View File

@ -16,7 +16,7 @@
<java.version>1.8</java.version> <java.version>1.8</java.version>
<mybatis-plus-boot-starter.version>3.3.0</mybatis-plus-boot-starter.version> <mybatis-plus-boot-starter.version>3.3.0</mybatis-plus-boot-starter.version>
<hutool.version>5.7.20</hutool.version> <hutool.version>5.7.20</hutool.version>
<knife4j-micro-spring-boot-starter.version>2.0.9</knife4j-micro-spring-boot-starter.version> <knife4j-micro-spring-boot-starter.version>3.0.3</knife4j-micro-spring-boot-starter.version>
</properties> </properties>
<dependencies> <dependencies>

View File

@ -8,16 +8,12 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ComponentScan(basePackages = {"com.jwl.driver.server.*"}) @ComponentScan(basePackages = {"com.jwl.driver.server.*"})
@SpringBootApplication @SpringBootApplication
public class DriverServerApplication implements WebMvcConfigurer { public class DriverServerApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(DriverServerApplication.class, 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/");
}
} }

View File

@ -1,6 +1,7 @@
package com.jwl.driver.server.config; package com.jwl.driver.server.config;
import com.jwl.driver.server.interceptor.AuthInterceptor; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfiguration;
@ -19,17 +20,17 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration @Configuration
public class CorsConfig implements WebMvcConfigurer { public class CorsConfig implements WebMvcConfigurer {
@Bean @Autowired
public AuthInterceptor initAuthInterceptor(){ private AuthInterceptor authInterceptor;
return new AuthInterceptor();
}
@Override @Override
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(initAuthInterceptor()).addPathPatterns("/driver-api/**") registry.addInterceptor(authInterceptor).addPathPatterns("/**").excludePathPatterns("/**/login",
.excludePathPatterns("/login/**"); // "/error",
"/doc.html");
} }
@Override @Override
public void addCorsMappings(CorsRegistry registry) { public void addCorsMappings(CorsRegistry registry) {
// 设置允许跨域的路径 // 设置允许跨域的路径
@ -46,20 +47,22 @@ public class CorsConfig implements WebMvcConfigurer {
.maxAge(3600); .maxAge(3600);
} }
@Bean @Override
public CorsWebFilter corsWebFilter(){ public void addResourceHandlers(ResourceHandlerRegistry registry) {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration(); registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
//1、配置跨域
corsConfiguration.addAllowedHeader("*");//允许哪些头进行跨域
corsConfiguration.addAllowedMethod("*");//允许哪些请求方式进行跨域
corsConfiguration.addAllowedOriginPattern("*");//允许哪些请求来源进行跨域
corsConfiguration.setAllowCredentials(true);//是否允许携带cookie进行跨域否则跨域请求会丢失cookie信息
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/");
} }
} }

View File

@ -2,6 +2,7 @@ package com.jwl.driver.server.interceptor;
import com.jwl.driver.server.redis.RedisCache; import com.jwl.driver.server.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
@ -16,6 +17,7 @@ import java.util.Objects;
* @create 2023/8/11 18:05 * @create 2023/8/11 18:05
*/ */
@Component
public class AuthInterceptor implements HandlerInterceptor { public class AuthInterceptor implements HandlerInterceptor {
@Autowired @Autowired
@ -23,6 +25,10 @@ public class AuthInterceptor implements HandlerInterceptor {
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 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.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8"); response.setContentType("text/html;charset=utf-8");
String token = request.getHeader("token"); String token = request.getHeader("token");

View File

@ -1,7 +1,7 @@
server: server:
port: 8888 port: 8888
servlet: # servlet:
context-path: '/driver-api' # context-path: '/driver-api'
spring: spring:
application: application: