1

我試圖插入數據使用POST請求,但我得到一個403錯誤。當我使用GET時,基本認證起作用。爲了測試我使用Fiddler。春季安全 - 基本身份驗證

有什麼問題?

安全配置:

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/**").hasRole("USER").and() 
       .httpBasic(); 
    } 

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

請求 - POST:

User-Agent: Fiddler 
Host: localhost:8080 
Content-Length: 200 
Content-Type: application/json 
Authorization: Basic dXNlcjpwYXNzd29yZA== 

請求正文:

{"name" : "name1", 
"description" : "desc1"} 

回答

1

試試這個:

@Configuration 
@EnableWebSecurity 
public class HelloWebSecurityConfiguration 
    extends WebSecurityConfigurerAdapter { 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) { 
    auth 
     .inMemoryAuthentication() 
     .withUser("user").password("password").roles("USER"); 
    } 
} 

來源:http://spring.io/blog/2013/07/03/spring-security-java-config-preview-web-security/

+0

THX禁用CSRF,但它不工作也。 – Bakus123

+0

不太清楚:你是否正在嘗試使用表單和POST登錄/插入數據等,而不是使用SpringSecurity,但GET呢? – Asura