跨域请求比较简单,在WebMvcConf中添加配置即可
/**
* web配置类
*
* @author ckun
*
*/
@Configuration
public class WebMvcConf extends WebMvcConfigurationSupport {
/**
* 跨域配置
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/rest/**")
.allowedMethods("PUT", "DELETE", "GET", "POST", "OPTIONS")
.allowedOrigins("*")
.allowedHeaders("*")
.allowCredentials(true).maxAge(3600);
super.addCorsMappings(registry);
}
}
但是,在复杂请求,例如PUT请求和DELETE请求过程中,浏览器端会先发送OPTIONS请求,来测试服务是否支持这些请求类型,然后才会进行正常请求过程。
我的思路是,添加一个拦截器,为所有接口调用前,判断method类型,如果是OPTIONS则单独处理。
/**
* 特殊请求跨域 OPTIONS 处理
*
* @author ckun
*
*/
@Component
public class CorsInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if("OPTIONS".equals(request.getMethod())) {
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
response.addHeader("Access-Control-Allow-Headers", "Accept, Origin, XRequestedWith, Content-Type, LastModified, token");
sendErrMsg(response, 0, "Fuck OPTIONS!");
return false;
}
return HandlerInterceptor.super.preHandle(request, response, handler);
}
/**
* response rest格式的错误信息
*
* @param response
* @param errMsg
* @throws IOException
* @throws JsonProcessingException
*/
private void sendErrMsg(ServletResponse response, Integer errCode, String errMsg) throws IOException, JsonProcessingException {
ResultBean result = new ResultBean(errCode, errMsg);
ObjectMapper mapper = new ObjectMapper();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
response.getWriter().write(mapper.writeValueAsString(result));;
}
}
在请求过程中,如果是OPTIONS请求,则直接返回一个空结果,不是空也行。需要在header中添加允许访问的域名、方法和请求头。
function putTest() {
$.ajax({
url : 'http://192.168.68.204/rest/user/test',
method : 'PUT',
headers : {
token : 'd56c3552293deab5f2d34f04a0db1c4e'
},
data : JSON.stringify({
userName : 'test',
password : '111111'
}),
dataType : 'JSON',
contentType : "application/json;charset-UTF-8",
success : function (data, textStatus, jqXHR) {
console.log(data);
console.log(datextStatusta);
console.log(jqXHR);
}
});
}
以上为请求方法,需要制定访问的数据类型,并将参数格式化成json字符串。
springboot的接口方法
@RestController
@RequestMapping("rest/user")
public class UserRestController {
@PutMapping("test")
public String test(@RequestBody Map<String, String> params) {
System.out.println(params);
return "test";
}
}
留言