import org.springframework.util.StringUtils; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * 跨域請求處理 */ public class CrosFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) servletResponse; HttpServletRequest request = (HttpServletRequest) servletRequest; String origin = request.getHeader("Origin"); if(!StringUtils.isEmpty(origin)){ //但請求帶有cookie的時候,origin不能爲*,必須是全匹配名 response.addHeader("Access-Control-Allow-Origin",origin); } response.addHeader("Access-Control-Allow-Methods","*"); String header = request.getHeader("Access-Control-Allow-Headers"); //匹配全部自定義請求頭 if(!StringUtils.isEmpty(header)){ response.addHeader("Access-Control-Allow-Headers",header); } //表明着瀏覽器請求第一次以後,一個小時以內能夠緩存請求信息,不須要在發送預檢命令 response.addHeader("Access-Control-Max-Age","3600"); //當請求帶cookie的時候,須要將Credentials設置爲true response.addHeader("Access-Control-Allow-Credentials","true"); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { } }
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import springfox.documentation.swagger2.annotations.EnableSwagger2; @SpringBootApplication @EnableSwagger2 @MapperScan("com.vcyber.mapper") public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); } /** * 處理跨域 * @return */ @Bean public FilterRegistrationBean registrationBean(){ FilterRegistrationBean bean = new FilterRegistrationBean(); bean.addUrlPatterns("/*"); bean.setFilter(new CrosFilter()); return bean; } }
@RestController @RequestMapping("loginrecord") @CrossOrigin public class LoginRecordController { }
或javascript
@RestController @RequestMapping("loginrecord") public class LoginRecordController { @GetMapping("getMybatisLoginList") @CrossOrigin public ResponseData getMybatisLoginList(){ ResponseData responseData = new ResponseData(); responseData.setCode("200"); responseData.setMessage("成功"); responseData.putDataValue("list",loginRecordMapper.selectList(null)); return responseData; } }
$.ajax({ type:"get", url:"http://localhost:8080/loginrecord/getMybatisLoginList", dataType:"jsonp", jsonp:"callback", success:function(result){ alert(result) }
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice; @ControllerAdvice public class JsonpAdvice extends AbstractJsonpResponseBodyAdvice { public JsonpAdvice(){ super("callback"); } }