SpringBoot中配置了SSL之後,發現除了小程序訪問後臺的GET方法, 其餘POST,DELETE都被拒絕,並且報錯403.
找了好久原來是因爲在Security的默認攔截器裏,默認會開啓CSRF處理,判斷請求是否攜帶了token。
如果沒有就拒絕訪問。並且,在請求爲
(GET|HEAD|TRACE|OPTIONS)
時,則不會開啓。
解決
既然是因爲默認開啓了CSRF,那關掉即可。可以加入以下代碼關閉~
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfigSEVEN extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//super.configure(http);
http.csrf().disable(); //關閉CSRF
}
}