2016-04-12 12 views
0

私は将来のアンドロイドアプリケーションのバックエンドとして、安らかなAPIを学習し開発しています。私は春のフレームワークを使用します。私はいくつかのリソースへのアクセスを制限するために春のセキュリティを使用します。 私はメソッドでUserControllerを持っています。新しいユーザーを作成するを作成(params ..)してください。 春のセキュリティを持つ新規ユーザーの作成/登録?

@RestController 
public class UserController { 
    private UserService userService; 
    private CreateFormValidator createFormValidator; 
    @Autowired 
    public UserController(UserService userService, CreateFormValidator createFormValidator) { 
     this.userService = userService; 
     this.createFormValidator = createFormValidator; 
    } 
    @InitBinder 
    public void initBinder(WebDataBinder binder) { 
     binder.addValidators(createFormValidator); 
    } 

    @RequestMapping(value = "/user/create", method = RequestMethod.POST) 
    String create(@RequestBody @Valid CreateForm createForm, BindingResult bindingResult) { 

     if (bindingResult.hasErrors()) { 
      return "user_create"; 
     } 
     try { 
      userService.create(createForm); 
     } catch (DataIntegrityViolationException e) { 
      bindingResult.reject("username.exists", "Username already exists"); 
      return "user_create"; 
     } 
     return "redirect:/login"; 
    } 
} 

や構成、セキュリティのための

、今私は私がしたいことだけをここで指定したため、「ユーザー/作成/」すべてのために許可されています。

@Configuration 
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
class SecurityConfig extends WebSecurityConfigurerAdapter { 
    @Autowired 
    private UserDetailsService userDetailsService; 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests() 
       .antMatchers("user/create").permitAll(); 
    } 
    @Override 
    public void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
       .userDetailsService(userDetailsService) 
       .passwordEncoder(new BCryptPasswordEncoder()); 
    } 
} 

しかし、私はそれをテストし、上のPOSTを行う際に、JSON本体

{ 
    "username" : "name", 
    "password" : "1234", 
    "repeatedPassword" : "1234" 
} 

で "ユーザー/作成" 私が取得しています:

を "エラー": "禁断"」メッセージ ":"要求パラメータ '_csrf'またはヘッダ 'X-CSRF-TOKEN'に無効なCSRFトークン 'null'が見つかりました "

このURLのpermitAllアクセスが設定クラスにあるときに禁止されている理由を教えてください。それを修正する方法は?前もって感謝します!あなたは残りのクライアントを使用している場合

+0

あなたはCSRFトークンを追加する必要があります – FreezY

答えて

0

http://www.codesandnotes.be/2015/02/05/spring-securitys-csrf-protection-for-rest-services-the-client-side-and-the-server-side/

に見てみましょう、ヘッダを要求するために、X-CSRF-TOKENが含まれます。いずれかのgetリクエストからサービスにcsrfトークン値を取得します。

または代わりに、次の試してください。

{ 
    "username" : "name", 
    "password" : "1234", 
    "repeatedPassword" : "1234", 
    "_csrf" : "token value" 
} 
関連する問題