spring Sping Boot 拦截器中的Application Properties值

omqzjyyz  于 9个月前  发布在  Spring
关注(0)|答案(3)|浏览(104)

有没有人可以帮助我读取Sping Boot 拦截器中的应用程序属性值(preHandle方法)?
我正在尝试用preHandle写一些逻辑。这个逻辑需要从application.properties文件中获取一些值。我使用@Value注解,但它总是空的。
谢谢

2ledvvac

2ledvvac1#

我会建议以下解决方案

@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
}
}
shyt4zoc

shyt4zoc2#

我解决了问题。这是细节。
溶液前:

// 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("/**");
   }

}

当拦截器(XYZCustomWebappInterceptor)通过使用'new'关键字创建一个类来添加时,Sping Boot 将无法理解注解。这就是为什么当我在XYZCustomWebappInterceptor中读取这些值(来自www.example.com的jwtSecretapplication.properties)时,它是null。
我是如何解决的:

//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

  }
}

谢谢大家帮助我。

xcitsw88

xcitsw883#

对于非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;
    }
    
}

相关问题