JAVAバックエンドクロスドメイン処理
2015 ワード
方法1:
方法2:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* @Description ( nginx , , , )
* @Author lc
* @Date 2019/6/26 0026 3:07
**/
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
/**
* @return void
* @Description
* @Author lc
* @Date 2019/6/26 0026 3:10
* @Param [registry]
**/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
.maxAge(3600)
.allowCredentials(true);
}
}
方法2:
import com.fendan.manager.dto.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import javax.servlet.http.HttpServletResponse;
/**
* @Description //TODO
* @Author lc
* @Date 2019/6/26 0026 3:24
**/
public class TestController {
@ApiOperation(" ")
@PostMapping(value = "/test")
@Validated
public Result test(HttpServletResponse response) {
//
response.setHeader("Access-Control-Allow-Headers", "Origin,X-Requested-With,Content-Type,Accept");
//
response.addHeader("Access-Control-Allow-Origin", "*");
//
response.addHeader("Access-Control-Allow-Methods", "POST, GET, DELETE, OPTIONS, DELETE");
//
response.addHeader("Access-Control-Max-Age", "1000");
return null;
}
}