ExtJS Store SYNC with Spring Security ON



我是Spring Security的新手,我已将其添加到我的项目中。一切似乎都完美运行 登录/注销 甚至跨屏幕导航。只有当我尝试拥有一个 ExtJS 网格并在存储中添加一条记录然后调用存储的 sync() 方法时,我才得到 -

Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'.

我知道我需要通过请求_csrf,但我想从你们所有人那里了解完成这项工作的最佳方法。请帮忙。

调用存储上的 sync() 方法时,如何自动传递此_csrf的所有 AJAX(创建/更新/删除/读取)?

安全配置

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserService userService;
    @Autowired
    private BCryptPasswordEncoder encoder;
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(encoder);
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/**").access("hasRole('ROLE_ADMIN')").and().formLogin().and().csrf();
    }
}

扩展代码

tbar : [ '->', {
    text : 'Add',
    handler : function(btn) {
        var grid = btn.up('grid');
        var editor = grid.findPlugin('rowediting');
        grid.getStore().insert(0, {});
        editor.startEdit(0, 0);
    }
} ],
bbar : [ '->', {
    text : 'Save',
    handler : function(btn) {
        btn.up('grid').getStore().sync();
    }
} ],

谢谢!

如果你想

使用CSRF,你不必在Spring中这样做。而是使用侵入性较小的OWASP方法。在你的索引.jsp或索引中.html如果你包含你的ExtJS代码,你可以包括CSRFGuard 3 CRSF注入,这将导致CRSF被注入到任何AJAX请求中。要在 Spring 中打开 CSRF,您只需在 Spring 配置中设置如下内容:

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
  }

或者在您的情况下:

  @Override
  protected void configure(HttpSecurity http) throws Exception 
  {
     http.authorizeRequests().antMatchers("/**").access("hasRole('ROLE_ADMIN')")
       .and().formLogin()
       .and().csrf().disable();
  }

您可以在所有标头中包含 CSRF 令牌:

Ext.Ajax.defaultHeaders = {ctoken: token};

在服务器端,从标头获取令牌并匹配会话令牌。

最新更新