2014-12-26 4 views
2

저는 스프링 시큐리티를 처음 접했고 그것을 프로젝트에 추가했습니다. 모든 것이 완벽하게 로그인/로그 아웃하고 여러 화면을 탐색하는 것처럼 보입니다. 내가 ExtJS에 그리드를 시도하고 저장소에 기록을 추가 한 후 상점의 동기() 방법이라고 만 할 때, 내가 가지고 -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(); 

    } 
} 

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

감사합니다!

답변

0

CSRF를 사용하려면 Spring에서 할 필요가 없습니다. 오히려 덜 침략적 인 OWASP 방법을 사용하십시오. ExtJS 코드를 포함하는 index.jsp 또는 index.html에 CSRFGuard 3 CRSF injection을 포함하면 CRSF가 AJAX 요청에 삽입됩니다.

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

많은 분들께 감사드립니다. 큰 도움 :) 나는 CSRF 토큰을 서버에 전달하기 위해 프록시에서 extraparams 옵션을 사용했습니다. –

0

당신은 모든 헤더에서 CSRF 토큰을 포함 할 수 있습니다 :

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

또는 귀하의 경우 :

봄에 CSRF의 켜려면 당신은 단지 당신의 봄 구성에서 다음과 같이 설정
Ext.Ajax.defaultHeaders = {ctoken: token}; 

서버 측에서 헤더에서 토큰을 가져 와서 세션 토큰을 일치시킵니다.

관련 문제