Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/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 在拦截器中使用req.setAttribute设置的值有时计算为null_Java_Spring Mvc - Fatal编程技术网

Java 在拦截器中使用req.setAttribute设置的值有时计算为null

Java 在拦截器中使用req.setAttribute设置的值有时计算为null,java,spring-mvc,Java,Spring Mvc,我编写了一个拦截器,它根据db检查api会话令牌。然后在请求对象中使用设置相应的userId和userRole req.setAttribute("userId", userId); req.setAttribute("userRole", userRole); 我正在访问控制器中的这些值。这只适用于某些情况。对于相同类型的两个连续请求,使用相同的值。一个计算结果正确,而另一个计算结果为null 我已检查是否未在任何其他地方使用setAttribute。我还检查了从db获得的值和req对象中的

我编写了一个拦截器,它根据db检查
api会话令牌。然后在请求对象中使用设置相应的
userId
userRole

req.setAttribute("userId", userId);
req.setAttribute("userRole", userRole);
我正在访问控制器中的这些值。这只适用于某些情况。对于相同类型的两个连续请求,使用相同的值。一个计算结果正确,而另一个计算结果为
null

我已检查是否未在任何其他地方使用
setAttribute
。我还检查了从db获得的值和req对象中的设置是否为
null

public class ApiSessionInterceptor extends HandlerInterceptorAdapter {

public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        if(request.getMethod().matches(RequestMethod.OPTIONS.name())) {
            return true;
        }
        setRequest(request);
        setResponse(response);
        if (!checkSession()) {
            throw new HttpUnauthorizedException();

        }

        return true;
    }
private boolean checkSession() {

        String sessionKey = getHeaderSessionKey();
        if (sessionKey != null) {

            Map<String, String> result = apiSessionService.getUserIdAndRoleBySessionKey(sessionKey);
            if(result!=null) {
                this.setUserId(result.get("user_id"));
                this.setUserRole(CommonMethods.getRoleNameFromId(result.get("role_id")));
                request.setAttribute("userId", this.getUserId());
                request.setAttribute("role", this.getUserRole());
                return true;
            }
        }
        return false;
    }
}

如果我理解正确的话,您正在尝试扩展HandlerInterceptorAdapter,在其中您将属性设置为request,对吗

你能分享一下你的拦截器和你在控制器中访问请求属性的位置吗

[编辑]

我创建了以下spring引导应用程序,它在100%的情况下都能正常工作。您确定您的属性实际上不是空的并且设置正确吗? 您确定您的请求未在某些筛选器中包装或替换吗

拦截器映射:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Autowired
    HandlerInterceptor userHandlerInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userHandlerInterceptor);

    }
}

拦截器:


包com.example.demo

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class UserHandlerInterceptor extends HandlerInterceptorAdapter {

    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        if(request.getMethod().matches(RequestMethod.OPTIONS.name())) {
            return true;
        }
        request.setAttribute("userId", "USER_ID");
        return true;
    }


}
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class SampleController {

    @RequestMapping(value = "/process", method = RequestMethod.GET)
    @ResponseBody
    public String downloadProcess(Model model, HttpServletRequest req, HttpServletResponse res){
        System.out.println( req.getAttribute("userId"));
        return "";
    }

}

控制员:


包com.example.demo

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class UserHandlerInterceptor extends HandlerInterceptorAdapter {

    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        if(request.getMethod().matches(RequestMethod.OPTIONS.name())) {
            return true;
        }
        request.setAttribute("userId", "USER_ID");
        return true;
    }


}
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class SampleController {

    @RequestMapping(value = "/process", method = RequestMethod.GET)
    @ResponseBody
    public String downloadProcess(Model model, HttpServletRequest req, HttpServletResponse res){
        System.out.println( req.getAttribute("userId"));
        return "";
    }

}

添加了请求的详细信息。感谢您的回复。我唯一的过滤器是cors(跨源请求)过滤器,它不适用于这个特定的API。我做了一些进一步的测试。只有当两个连续的请求从同一个客户机发出时,才会发生这种情况,彼此之间的间隔不到几秒钟。