Obtengo el estado 403 Prohibido en Swagger solo para la solicitud del método POST. Probé todos los cfg de seguridad de primavera para resolver esto, pero solo funciona en los métodos GET. Estoy usando Spring Boot, Spring Security y Swagger. Podría alguien ayudarme, por favor ? Aquí está swagger cfg:
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } }Y aquí está el cfg de seguridad de primavera:
@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/**"); } }¡Gracias por leer!
Tuve un problema similar la otra semana, así es como conseguí que el mío funcionara, necesitaba agregar un montón más de emparejadores de lo que pensaba y agregar la desactivación de csrf, pero parece funcionar bien.
@Bean(name="configure") @Conditional(DevConditional.class) public SecurityWebFilterChain configureDev(ServerHttpSecurity http) throws Exception { return http .csrf().disable() .authorizeExchange() .pathMatchers("/v2/api-docs").permitAll() .pathMatchers("/configuration/ui").permitAll() .pathMatchers("/swagger-resources/**").permitAll() .pathMatchers("/configuration/security").permitAll() .pathMatchers("/swagger-ui.html").permitAll() .pathMatchers("/swagger-ui/*").permitAll() .pathMatchers("/webjars/**").permitAll() .pathMatchers("/v2/**").permitAll() .and().cors() .and().oauth2ResourceServer() .jwt().and().and().build(); }Obtuve esta respuesta ".csrf (). Disable ()" de: Spring boot con WebFlux siempre lanza el estado 403 en las pruebas