为什么后端获取的token总是为null?前端该怎么写
package com.zzj.filter;
import com.alibaba.fastjson.JSONObject;
import com.zzj.pojo.Result;
import com.zzj.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Slf4j
@WebFilter(urlPatterns = "/*")
public class DemoFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req =(HttpServletRequest) servletRequest;
HttpServletResponse resp = (HttpServletResponse) servletResponse;
String url = req.getRequestURL().toString();
if(url.contains("login")) {
log.info("登陆操作放行........");
filterChain.doFilter(servletRequest, servletResponse);
return;
}
String jwt = req.getHeader("token");
if(!StringUtils.hasLength(jwt)){
log.info("请求头token为空!!!!!");
Result error = Result.error("NOT_LOGIN");
String notLogin = JSONObject.toJSONString(error);
resp.getWriter().write(notLogin);
return;
}
try {
JwtUtils.parseJWT(jwt);
} catch (Exception e) {//解析失败
e.printStackTrace();
log.info("令牌解析失败!");
Result error = Result.error("NOT_LOGIN");
String notLogin = JSONObject.toJSONString(error);
resp.getWriter().write(notLogin);
}
log.info("令牌合法,放行!!!!!");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
```