【发布时间】:2021-02-26 10:15:05
【问题描述】:
我得到 403 状态 Forbidden 仅适用于 POST 方法请求。 我尝试了所有 spring security cfg 来解决这个问题,但只适用于 GET 方法。 我正在使用弹簧靴、弹簧安全和招摇。 ¿ 有人可以帮助我吗? 这是招摇的cfg:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
这里是 spring security cfg:
@Configuration
@EnableWebSecurity
public class SecurityCFG extends WebSecurityConfigurerAdapter{
@Bean
public PasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
PasswordEncoder encoder = encoder();
auth
.inMemoryAuthentication()
.withUser("carlos")
.password(encoder.encode("admin123"))
.roles("USER")
.and()
.withUser("carlos2")
.password(encoder.encode("admin123"))
.roles("USER", "ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(
"/v2/api-docs",
"/swagger-resources/**",
"/swagger-ui.html",
"/webjars/**" ,
/*Probably not needed*/ "/swagger.json")
.permitAll()
.anyRequest()
.authenticated()
.and()
.httpBasic();
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v2/api-docs/**");
web.ignoring().antMatchers("/swagger.json");
web.ignoring().antMatchers("/swagger-ui.html");
web.ignoring().antMatchers("/swagger-resources/**");
web.ignoring().antMatchers("/webjars/**");
}
}
感谢阅读!
【问题讨论】:
-
我没有看到任何事情发生在我身上。我相信这是因为您使用的是http basic。尝试使用表单登录,看看是否允许招摇。
标签: java spring spring-boot spring-security swagger