spring boot controller跨域處理
阿新 • • 發佈:2019-01-07
spring boot controller跨域處理
類和方法上添加註解@CrossOrigin
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import com.vividsolutions.jts.io.ParseException; import javax.ws.rs.PathParam; import javax.xml.ws.ResponseWrapper; @RestController @EnableAutoConfiguration @CrossOrigin @RequestMapping("/test") public class geowaveController { //OK //http://localhost:8888/springboot/test/hello @RequestMapping("/hello") @CrossOrigin //@ResponseBody String home() { return "Hello World! /springboot//test/hello"; } }
spring mvc 新增web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <filter> <filter-name>contexFilter</filter-name> <filter-class>com.cwgis.controller.SimpleCORSFilter</filter-class> </filter> <filter-mapping> <filter-name>contexFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
src/com.cwgis.controller.SimpleCORSFilter.java
public class SimpleCORSFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; httpServletResponse.setHeader("Access-Control-Allow-Origin", "*"); httpServletResponse.setHeader("Access-Control-Allow-Headers", "Authentication"); filterChain.doFilter(servletRequest,httpServletResponse); } @Override public void destroy() { } }
src/application.properties
server.context-path=/springboot
#server.address=192.168.10.99
server.port=8888
security.user.name=root
security.user.password=1
security.user.role=ADMIN
#禁用springboot security
management.security.enabled=false
security.basic.enabled=false
# tomcat最大執行緒數,預設為200
server.tomcat.max-threads=800
# tomcat的URI編碼
#server.tomcat.uri-encoding=UTF-8
#關閉actuator的shutdown功能
#endpoints.shutdown.enabled=false
#禁用密碼驗證
#endpoints.shutdown.sensitive=false
—the—end—