vue+axios中使用raw+json请求失败的解决办法,application/json请求跨域问题
后端需要开启Content-Type的权限,平时不知道没开启权限,到自己遇到找了好久才知道是这个搞得鬼。o(╥﹏╥)o//Filter过滤器跨域处理String origin = request.getHeader("Origin");response.setHeader("Access-Control-Allow-Origin", origin);response.setHeader("Acce
·
后端需要开启Content-Type的权限,平时不知道没开启权限,到自己遇到找了好久才知道是这个搞得鬼。o(╥﹏╥)o
//Filter过滤器跨域处理
String origin = request.getHeader("Origin");
response.setHeader("Access-Control-Allow-Origin", origin);
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization,token,**Content-Type**");
response.setHeader("Access-Control-Allow-Credentials", "true");
附上过滤器的代码。
@WebFilter(urlPatterns = {"/*"})//过滤所有URL
public class LoginFilter implements Filter {
//ALLOWED_PATHS 存放不需要过滤的url
private static final Set<String> ALLOWED_PATHS = Collections.unmodifiableSet(new HashSet<>(
Arrays.asList("/login", "/logout")));
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("----Filter过滤器初始化----");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//校验用户登录状态
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String path = request.getRequestURI().substring(request.getContextPath().length()).replaceAll("[/]+$", "");
boolean allowedPath = ALLOWED_PATHS.contains(path);
//若该url不需要过滤
if(allowedPath){
//放行
filterChain.doFilter(servletRequest,servletResponse);
} else{
//Filter过滤器跨域处理
String origin = request.getHeader("Origin");
response.setHeader("Access-Control-Allow-Origin", origin);
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization,token,Content-Type");
response.setHeader("Access-Control-Allow-Credentials", "true");
//获取Headers中的参数
String token = request.getHeader("token");
token = token == null ? "" : token;
//查询token在Redis中的剩余时间
Long expire = redisTemplate.getExpire(token);
if(expire > 0){ //是登录状态
//重置token的时间
redisTemplate.expire(token,30L, TimeUnit.MINUTES);
//放行
filterChain.doFilter(servletRequest,servletResponse);
}else{
//未登录,响应数据
JSONObject jsonObject = new JSONObject();
//jsonObject.put("user","null");
jsonObject.put("msg","未登录");
String string = JSONObject.toJSONString(jsonObject);
response.setContentType("json/text;charset=utf-8");
PrintWriter out = response.getWriter();
out.write(string);
}
}
}
@Override
public void destroy() {
System.out.println("----过滤器销毁----");
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)