// 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("/**");
}
}
//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
}
}
3条答案
按热度按时间2ledvvac1#
我会建议以下解决方案
在实际的课堂上,你会做以下事情
shyt4zoc2#
我解决了问题。这是细节。
溶液前:
当拦截器(XYZCustomWebappInterceptor)通过使用'new'关键字创建一个类来添加时,Sping Boot 将无法理解注解。这就是为什么当我在XYZCustomWebappInterceptor中读取这些值(来自www.example.com的jwtSecretapplication.properties)时,它是null。
我是如何解决的:
谢谢大家帮助我。
xcitsw883#
对于非webmvc的springboot应用程序。
如果您的要求是从属性文件中读取表名,则使用下面的代码片段。