项目初始化
parent
b8e65ad0d3
commit
df866d4d27
2
pom.xml
2
pom.xml
|
@ -16,7 +16,7 @@
|
|||
<java.version>1.8</java.version>
|
||||
<mybatis-plus-boot-starter.version>3.3.0</mybatis-plus-boot-starter.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>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -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/");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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/");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
server:
|
||||
port: 8888
|
||||
servlet:
|
||||
context-path: '/driver-api'
|
||||
# servlet:
|
||||
# context-path: '/driver-api'
|
||||
|
||||
spring:
|
||||
application:
|
||||
|
|
Loading…
Reference in New Issue