2014-07-14 5 views
1

스프링 보안에서 AJAX 요청을 처리하는 데 어려움이 있습니다.요청 방법 'POST'가 스프링 보안에서 지원되지 않습니다.

스프링 보안 설정은 (자바 설정)입니다 :

@Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/themes/**").permitAll() 
       .anyRequest().access("hasRole('ROLE_USER')") 
       .antMatchers("/offerings/**").access("hasRole('ROLE_ADMIN')") 
       .antMatchers("/users").access("hasRole('ROLE_ADMIN')") 
       .and() 
      .formLogin() 
       .loginPage("/login") 
       .permitAll() 
       .and() 
      .logout() 
       .logoutUrl("/logout") 
       .logoutSuccessUrl("/login?logout") 
       .deleteCookies("JSESSIONID") 
       .invalidateHttpSession(true) 
       .permitAll() 
       .and() 
      .csrf(); 
    } 

AJAX 핸들이

@RequestMapping(value = "/user/edit", method = RequestMethod.POST) 
public ModelAndView edit(@RequestParam Map<String, String> allRequestParams) { 
    ... 
} 

그리고 AJAX JS 코드는 내가하지 않으면

$.ajax({ 
    url: '/user/edit', 
    type: 'POST', 
    ... 
}); 

모든 것이 잘 작동합니다입니다 csrf은 다음과 같이 구성됩니다. csrf().disable(). 따라서 csrf이 활성화되어있을 때 AJAX 요청을 처리하는 방법.

답변

1

답변을 찾았습니다. AJAX 요청을 보낼 때 "_csrf"토큰 키만 있으면 데이터에 하나 이상의 매개 변수를 추가하십시오.

+1

+1, 아약스 요청에서 csrf 토큰을 설명하는 것이 좋습니다 .... 여기에서 찾았습니다 http://docs.spring.io/spring-security/site/docs/3.2.0.CI-SNAPSHOT/reference/html /csrf.html –

관련 문제