有人可以帮我读取 Spring Boot 拦截器中的应用程序属性值(
preHandle
方法)吗?
我正在尝试在
preHandle
中写一些逻辑。该逻辑需要从 application.properties
文件中获取一些值。我使用 @Value
注释,但它始终为空。
谢谢
我建议以下解决方案
@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
}
}
我解决了这个问题。这是详细信息。
解决前:
// 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("/**");
}
}
当通过使用“new”关键字创建类来添加拦截器(XYZCustomWebappInterceptor)时,Spring boot将无法理解这些注释。这就是为什么当我在 XYZCustomWebappInterceptor 中读取这些值(来自 application.properties 的 jwtSecret)时,它为空。
我是如何解决的:
//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
}
}
谢谢大家对我的帮助。
对于非webmvc springboot应用。
如果您的要求是从属性文件中读取表名称,请使用下面的代码片段。
@Slf4j
@Component
public class HibernateInterceptor extends EmptyInterceptor {
private static final long serialVersionUID = 1L;
@Value(value = "${user.documents.table.name}")
private String targetTableName;
private static String FINAL_TABLE_NAME;
@PostConstruct
public void init() {
FINAL_TABLE_NAME = targetTableName;
}
@Override
public String onPrepareStatement(String sql) {
log.info("tagetTableName::"+FINAL_TABLE_NAME);
//String prepedStatement = super.onPrepareStatement(sql);
sql = sql.replaceAll("filenet_tfa_documents", FINAL_TABLE_NAME);
//log.info("new table name:"+sql);
return sql;
}
}