简体   繁体   English

请求属性无法在spring微服务中访问

[英]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.

相关问题 内部微服务请求在Spring Cloud应用程序中以禁止状态响应 - inter micro-service request responds with Forbidden status in spring cloud application 在Spring项目中为Micro Service启动Sub SpringBootApplication - Starting Sub SpringBootApplication in Spring project for micro service Spring Boot微服务生产环境? - Spring boot micro service production Environment? Spring 邮件关闭微服务 - Spring Mail shutting down micro service Spring 引导:无法访问 Spring 调度程序中的请求 scope bean - Spring Boot: Unable to access the request scope bean in Spring Scheduler 无法向基于Spring MVC的REST服务发送多部分/混合请求 - Unable to send a multipart/mixed request to spring MVC based REST service Spring Cloud微服务,与其他微服务一起使用密码保护的微服务 - Spring Cloud microservice, consume password protected micro service with other micro service 保护微服务Spring Cloud安全性Oauth2 - Securing micro-service spring cloud security Oauth2 在Spring Boot中管理通用微服务内的属性文件 - Manage property files inside common micro service in Spring Boot Hystrix 断路器在 Spring Micro 服务实现中触发一个事件 - Hystrix circuit breaker trigger an Event in Spring Micro service implementation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM