Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application Properties value in Spring boot interceptor

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

like image 738
Peer Mohamed Avatar asked Jul 15 '26 06:07

Peer Mohamed


2 Answers

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.

like image 94
Peer Mohamed Avatar answered Jul 18 '26 08:07

Peer Mohamed


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
}
}
like image 22
dinesh remje Avatar answered Jul 18 '26 10:07

dinesh remje