[英]request attribute unable to access in spring micro service
I am setting up request attribute in overridden method doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain)
of class OncePerRequestFilter
but i am unable to get request attribute in my service. 我在类OncePerRequestFilter
重写方法doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain)
中设置请求属性,但我无法在我的服务中获取请求属性。
public class AuthenticationFilter extends OncePerRequestFilter {
protected void doFilterInternal(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, FilterChain filterChain)
throws ServletException, IOException {
....
.....
httpServletRequest.setAttribute("testing","testing");
filterChain.doFilter(httpServletRequest, httpServletResponse);
}
}
@RequestMapping(
value = "/index/{index:.+}",
method = RequestMethod.GET,
produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
public ResponseEntity<RestApiModelResponse> getIndex(
@PathVariable String index,
HttpServletRequest httpServletRequest)
throws Exception {
var test = httpServletRequest.getAttribute("testing");
}
Here, I am getting null in test variable.. 在这里,我在测试变量中得到null。
You need to define filter as @Component 您需要将过滤器定义为@Component
@Component
public class AuthenticationFilter extends OncePerRequestFilter {
In order for Spring to be able to recognize a filter, we needed to define it as a bean with the @Component annotation 为了使Spring能够识别过滤器,我们需要使用@Component注释将其定义为bean
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.