【问题标题】:ExtJS Store SYNC with Spring Security ONExtJS Store SYNC 与 Spring Security ON
【发布时间】:2015-04-07 15:56:06
【问题描述】:

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

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

我知道我需要将 _csrf 与请求一起传递,但我想从大家那里了解完成这项工作的最佳方法。请帮忙。

当调用 store 上的 sync() 方法时,如何自动通过所有 AJAX(创建/更新/删除/读取)传递这个 _csrf?

安全配置

@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();

    }
}

ExtJS 代码

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();
    }
} ],

谢谢!

【问题讨论】:

    标签: spring security extjs sync


    【解决方案1】:

    如果您想使用 CSRF,则不必在 Spring 中使用。而是使用侵入性较小的 OWASP 方法。在包含 ExtJS 代码的 index.jsp 或 index.html 中,您可以包含 CSRFGuard 3 CRSF injection,这将导致 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();
      }
    

    【讨论】:

    • 非常感谢加布里埃尔。大帮助 :) 我在代理中使用 extraparams 选项将 CSRF 令牌传递给服务器。
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-09
      • 2019-09-25
      • 2011-02-16
      • 1970-01-01
      • 2013-08-29
      • 2016-12-17
      • 2019-04-08
      • 1970-01-01
      相关资源
      最近更新 更多