Commit 082de629 by 段启岩

解决跨域问题

parent c080bd96
......@@ -123,6 +123,11 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
......
......@@ -6,10 +6,7 @@ import cn.meteor.beyondclouds.core.resolver.CurrentSubjectResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.*;
import java.util.List;
......@@ -56,4 +53,13 @@ public class WebMvcConfig implements WebMvcConfigurer {
resolvers.add(new CurrentSubjectResolver());
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("*")
.allowCredentials(true)
.maxAge(3600)
.allowedHeaders("*");
}
}
package cn.meteor.beyondclouds.core.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.cors.CorsUtils;
import javax.servlet.http.HttpServletRequest;
/**
* @author meteor
*/
@Aspect
@Component
@Slf4j
public class CorsPreFlightRequestDetector {
@Around(value = "execution(boolean *..*.preHandle(..))")
public Object processTx(ProceedingJoinPoint jp) throws Throwable {
HttpServletRequest request = (HttpServletRequest) jp.getArgs()[0];
if (request != null && CorsUtils.isPreFlightRequest(request)) {
log.info("detected pre-flight-request:{}.", request.getRequestURL().toString());
return true;
} else {
return jp.proceed();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment