6

Could anyone please help me to read the application properties values in Spring Boot interceptor (preHandle method)?

I am trying to write some logic in preHandle. This logic needs to get some values from application.properties file. I use @Value annotation but it is always null.

Thanks

jny
  • 8,007
  • 3
  • 37
  • 56
Peer Mohamed
  • 353
  • 1
  • 5
  • 12

2 Answers2

8

I would suggest the following solution

   @Configuration
public class XYZWebappWebConfig extends WebMvcConfigurerAdapter{



@Autowired
private XYZCustomWebappInterceptor xyzCustomWebappInterceptor;

 @Override
 public void addInterceptors(InterceptorRegistry registry){

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

}

In actual class you would do the following

// 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
}
}
dinesh remje
  • 81
  • 1
  • 3
7

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.

Peer Mohamed
  • 353
  • 1
  • 5
  • 12