http訪問springboot接口出現401 、403、 Forbidden 錯誤解決方法

出現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保護即可。
        ;
    }
}

折騰了好久

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