401错误:
出现这个错误之后,我们需要建一个SecurityConfig类,如果你有的话,直接在该类中加以下代码段
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().permitAll().and().logout().permitAll();//配置不需要登录验证
}
如果没有改类,则需要建一个类文件,里面代码如下
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().permitAll().and().logout().permitAll();//配置不需要登录验证
}
}
重新编译项目并启动之后调用接口如果报403错误的话,如图
则需要修改SecurityConfig中的代码,修改后的代码段为
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().permitAll().and().logout().permitAll()//配置不需要登录验证
.and().csrf().disable();//关闭CSRF保护即可。
}
到此403问题解决