当前位置: 首页 > 知识库问答 >
问题:

Spring Boot拦截器中的应用程序属性值

王泓
2023-03-14

谁能帮我读取 Spring 引导拦截器(preHandle 方法)中的应用程序属性值?

我试图在preHandle中编写一些逻辑。这个逻辑需要从application.properties文件中获取一些值。我使用@Value注释,但它总是空的。

谢谢。

共有2个答案

司马彦
2023-03-14

我建议采用以下解决方案

   @Configuration
public class XYZWebappWebConfig extends WebMvcConfigurerAdapter{



@Autowired
private XYZCustomWebappInterceptor xyzCustomWebappInterceptor;

 @Override
 public void addInterceptors(InterceptorRegistry registry){

    registry.addInterceptor(xyzCustomWebappInterceptor).addPathPatterns("/**");
   }

}

在实际课堂上,您将执行以下操作

// Custom interceptor class
@Component
public class XYZCustomInterceptor implements HandlerInterceptor{
@Value("${JWT.secret}")
private String jwtSecret;

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse 
                           response, Object arg2) throws Exception {
//use the secret key
}
}
阎丰羽
2023-03-14

我解决了这个问题。这是细节。

解决方案之前:

// Custom interceptor class
public class XYZCustomInterceptor implements HandlerInterceptor{
@Value("${JWT.secret}")
private String jwtSecret;

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse 
                               response, Object arg2) throws Exception {

 // I am using the jwtSecret in this method for parsing the JWT
 // jwtSecret is NULL

  }
}


 //To Register (another class)

 @Configuration
 public class XYZWebappWebConfig extends WebMvcConfigurerAdapter{

 @Override
 public void addInterceptors(InterceptorRegistry registry){

    registry.addInterceptor(new                         
                 XYZCustomWebappInterceptor()).addPathPatterns("/**");
   }

}

当拦截器(XYZCustomWebappInterceptor)通过使用“new”关键字创建一个类来添加时,Spring boot将无法理解注释。这就是为什么当我在XYZCustomWebappInterceptor中读取这些值(来自application.properties的jwtSecret)时,它是null。

我是如何解决的:

//Read those properties values in this class and pass it to interceptor's 
//constructor method. 

 @Configuration
 public class XYZWebappWebConfig extends WebMvcConfigurerAdapter{

 @Value("${JWT.secret}")
 private String jwtSecret;

 @Override
 public void addInterceptors(InterceptorRegistry registry){

    registry.addInterceptor(new                         

    XYZCustomWebappInterceptor(jwtSecret)).addPathPatterns("/**");
   }

}


// Custom interceptor class
public class XYZCustomInterceptor implements HandlerInterceptor{

private String jwtSecret;

RbsCustomWebappInterceptor(String secret){
    this.jwtSecret = secret;
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse 
                               response, Object arg2) throws Exception {


 // I am using the jwtSecret in this method for parsing the JWT
 // Now, jwtSecret is NOT NULL

  }
}

谢谢大家对我的帮助。

 类似资料:
  • 本文向大家介绍SpringBoot拦截器的使用小结,包括了SpringBoot拦截器的使用小结的使用技巧和注意事项,需要的朋友参考一下 总结一下SpringBoot下拦截器的使用,步骤很简单: 1.自定义自己的拦截类,拦截类需要继承HandlerInterceptor接口并实现这个接口的方法。 2.配置类需要继承WebMvcConfigurerAdapter类 3.启动SpringBoot应用即可

  • 我在src/main/resources下创建了2个文件: 应用程序。属性 第一个具有从env变量中获取值的属性,而后者具有固定值。 根据这里的具体情况,我以这样的方式推出了Spring靴: 然而,不会产生任何影响,并且应用程序是局部的。属性似乎被忽略。 有什么提示吗?

  • 本文向大家介绍SpringBoot拦截器实现登录拦截的方法示例,包括了SpringBoot拦截器实现登录拦截的方法示例的使用技巧和注意事项,需要的朋友参考一下 源码 GitHub:https://github.com/291685399/springboot-learning/tree/master/springboot-interceptor01 SpringBoot拦截器可以做什么 可以对UR

  • 我正在将一个非常基本的web应用程序部署到Google应用程序引擎。我使用的是Springboot,我可以在本地很好地运行应用程序,但当我部署到Google时,应用程序引擎不会启动实例。我在启动时配置了一个云SQL数据源。 我有云sql配置属性配置src/main/Resources/application.properties.App Engine似乎找不到这些属性,所以它无法正确设置Cloud

  • 主要内容:1.maven仓库,2.过滤器,3.拦截器,4.监听器,5.实例化,6.测试,7.拦截器与过滤器的区别1.maven仓库 2.过滤器 过滤器的英文名称为 Filter, 是 Servlet 技术中最实用的技术。 如同它的名字一样,过滤器是处于客户端和服务器资源文件之间的一道过滤网,帮助我们过滤掉一些不符合要求的请求,通常用作 Session 校验,判断用户权限,如果不符合设定条件,则会被拦截到特殊的地址或者基于特殊的响应。 3.拦截器 Java中的拦截器是动态拦截 action 调用的

  • 我能以某种方式截获来自应用程序的绝对所有流量吗?我应该做什么来做到这一点?提前谢谢你)