spring boot post請求403,get請求成功

項目使用了spring boot  security.使用的spring boot版本是spring boot2.0.

post請求403錯誤,表示資源不可用。服務器理解客戶的請求,但拒絕處理它,通常由於服務器上文件或目錄的權限設置導致的WEB訪問錯誤。

Spring Security CSRF 保護默認是開啓的,那麼在 POST 方式提交表單的時候就必須驗證 Token,如果沒有,那麼自然也就是 403 沒權限了。

需要添加spring security相關配置:

   @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/admin/health")
            .permitAll()
            .anyRequest()
            .authenticated()
            .and()
            .httpBasic()
            .and()
            .formLogin()
            .disable()
            .csrf()
            .disable();

        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
            .withUser(user)
            .password(passwordEncoder().encode(password))
            .roles();
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章