Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 要创建筛选器以检查cookie,然后保存控制器中的对象和引用吗_Java_Spring_Spring Mvc - Fatal编程技术网

Java 要创建筛选器以检查cookie,然后保存控制器中的对象和引用吗

Java 要创建筛选器以检查cookie,然后保存控制器中的对象和引用吗,java,spring,spring-mvc,Java,Spring,Spring Mvc,我想创建一个过滤器,它将在我的任何SpringMVC控制器操作之前执行 我想检查cookie是否存在,然后只为当前请求存储一个对象 然后,我需要从控制器操作中引用此对象(如果存在) 关于如何做到这一点的建议?要创建过滤器,只需创建一个实现javax.servlet.filter的类,在您的例子中可以是这样的 public class CookieFilter implements Filter {     public void doFilter(ServletRequest req, Serv

我想创建一个过滤器,它将在我的任何SpringMVC控制器操作之前执行

我想检查cookie是否存在,然后只为当前请求存储一个对象

然后,我需要从控制器操作中引用此对象(如果存在)


关于如何做到这一点的建议?

要创建过滤器,只需创建一个实现javax.servlet.filter的类,在您的例子中可以是这样的

public class CookieFilter implements Filter {
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        
        Cookie[] cookies = request.getCookies();
        if (cookies != null){
          for (Cookie ck : cookies) {
            if ("nameOfMyCookie".equals(ck.getName())) {
                // read the cookie etc, etc
                // ....
                // set an object in the current request
                request.setAttribute("myCoolObject", myObject)
            }
        }
        chain.doFilter(request, res);
    }
    public void init(FilterConfig config) throws ServletException {
        // some initialization code called when the filter is loaded
    }
    public void destroy() {
        // executed when the filter is unloaded
    }
}
然后在web.xml中声明过滤器


库克过滤器
    
my.package.CookieFilter
    
库克过滤器
    /*

此时,在控制器中,只需使用request.getAttribute(“myColObject”)检查请求中是否存在attibute即可。Java 8使用stream过滤所需的cookie,然后按照所需逻辑使用该cookie:

public class CookieFilter implements GenericFilterBean {
  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
      throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    Cookie[] cookies = request.getCookies();
    Stream<Cookie> stream = Objects.nonNull(cookies) ? Arrays.stream(cookies) : Stream.empty();
    String cookieValue = stream.filter(cookie -> "nameOfMyCookie".equals(cookie.getName()))
        .findFirst()
        .orElse(new Cookie("nameOfMyCookie", null))
        .getValue();
    if (Objects.nonNull(cookieValue)) {
      request.setAttribute("myCoolObject", myObject);
    } 
    chain.doFilter(request, res);
  }
公共类CookieFilter实现GenericFilterBean{
公共无效doFilter(ServletRequest-req、ServletResponse-res、FilterChain链)
抛出IOException、ServletException{
HttpServletRequest请求=(HttpServletRequest)请求;
Cookie[]cookies=request.getCookies();
Stream=Objects.nonNull(cookies)?Arrays.Stream(cookies):Stream.empty();
字符串cookieValue=stream.filter(cookie->“nameOfMyCookie”.equals(cookie.getName()))
.findFirst()
.orElse(新Cookie(“MyCookie的名称”,空))
.getValue();
if(Objects.nonNull(cookieValue)){
setAttribute(“myColObject”,myObject);
} 
链.doFilter(请求,res);
}

spring可以将依赖项注入我的筛选器吗?只有通过spring应用程序上下文实例化筛选器时,spring才能注入依赖项。您的筛选器由servlet容器实例化。不过,您可以在筛选器中访问应用程序上下文。请看这里:有spring启动的示例吗?这不起作用。
getCookies()
始终为
null
@AdamArold request。getCookies()返回null如果请求中没有Cookie,请确保在应用程序中的其他位置设置它们。要确保Cookie被发送到服务器,请检查请求的头并检查Cookie密钥。