I solved the problem. Here is the detail.
Before solution:
// 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("/**");
}
}
When the interceptor (XYZCustomWebappInterceptor) is added by creating a class using 'new' keyword, then Spring boot will not understand the annotations. Thats why when I read those values (jwtSecret from application.properties) in XYZCustomWebappInterceptor, it is null.
How I solved:
//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
}
}
Thanks all for helping me.