出現401可能是框架中添加了
spring-boot-starter-security ,這時候需要進行http請求降級處理 Spring Boot 1.x中的配置 management.security.enabled=false 可以 Spring Boot 2.x中的management.security.enabled=false無效問題,編寫 SecurityConfig 繼承 WebSecurityConfigurerAdapter ,重寫configure(HttpSecurity http) 方法
出現403,Forbidden,這個是因爲你開啓了CSRF保護,關閉即可
{
"timestamp": 1581852880108,
"status": 403,
"error": "Forbidden",
"message": "Forbidden",
"path": "/api/app/userGifts/save"
}
在configure(HttpSecurity http)
方法中追加http.csrf().disable();關閉CSRF保護即可。
直接上代碼
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.authorizeRequests().anyRequest().permitAll().
and().logout().permitAll()
.and().csrf().disable();//關閉CSRF保護即可。
;
}
}
折騰了好久