我有下面的类,它有CSRF if/else条件。我从属性文件中读取此标志,并且可以使用/refresh端点更改标志值。:
@Order(-1)
@EnableWebSecurity
public class SomeConfigClass extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
String csrfFlag = somePropertyFile.getCsrfFlag();
if(isCsrfEnable){
http.csrf()....some other code
}else {
http.csrf().disable();
}
}
}
问题:当我调试代码时,我发现这个类只在Spring容器启动时调用一次。在此之后将不会调用if。因此,即使将CSRF标志更改为false,这也会关闭CSRF标志,这将不起作用& CSRF检查仍将存在。
为了避免这种情况,我需要重启/重放应用程序,这样容器就会从属性文件中提取新的配置,这可能会导致应用程序停机。有没有人能更好地解决这个问题呢?
目标:应外部化CSRF禁用/启用标志。用户可以切换标志并启用/禁用CSRF,而无需重新启动/重新堆叠应用程序。
发布于 2020-05-04 13:08:16
您需要使用csrf保护匹配器来实现相同的功能。
您需要编写CSRFProtectionMatcher。
http.csrf().requireCsrfProtectionMatcher(new CustomCsrfProtectionMatcher())
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
CustomCsrfProtectionMatcher.java
public class CustomCsrfProtectionMatcher implements RequestMatcher {
@Override
public boolean matches(HttpServletRequest request) {
//based on the logic..
//You can store flag in DB or properties or XML File
//Please note that for every request this flag will be checked.You can choose a
//better way to implement this
return true/false;
}
}
我个人建议保留在app.properties中,而不是按请求检查它。
https://stackoverflow.com/questions/61589674
复制相似问题