1
resposta

swagger-ui.html com erro 403

Segui as instruções do curso, mas obtenho 403 ao tentar acessar a página da swagger-ui.html.

URL:

http://localhost:8080/swagger-ui.html

Pom.xml

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>

Classe principal

@SpringBootApplication
@EnableSpringDataWebSupport
@EnableCaching
@EnableSwagger2
public class StocksApplication {
    public static void main(String[] args) {
        SpringApplication.run(StocksApplication.class, args);
    }
}

Classe de configuração do Swagger:

@Configuration
public class SwaggerConfigurations {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("br.com.stocks"))
                .paths(PathSelectors.any()) //Diferente do curso, mas não fez diferença nos testes.
                .build()
                .ignoredParameterTypes(User.class)
                .apiInfo(this.getApiInfo());
    }

    private ApiInfo getApiInfo() {
       (...)
    }
}

Classe de segurança:

@Configuration
@EnableWebSecurity
public class SecurityConfigurations extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthenticationService authenticationService;

    @Autowired
    private TokenService tokenService;

    @Override
    @Bean //Necessário porque o AuthenticationManager não é injetável.
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(authenticationService)
            .passwordEncoder(new BCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers(HttpMethod.POST, ApiVersions.V1 + AuthenticationRestController.PATH).permitAll()
            .antMatchers(HttpMethod.GET, ApiVersions.V1 + CompanyRestController.PATH + "/*").permitAll()
            .antMatchers(HttpMethod.GET, ApiVersions.V1 + SectorRestController.PATH + "/*").permitAll()
            .antMatchers(HttpMethod.GET, "/actuator/**").permitAll()
            .anyRequest().authenticated()
            .and().csrf().disable()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and().addFilterBefore(new TokenAuthenticationFilter(tokenService, authenticationService), 
                        UsernamePasswordAuthenticationFilter.class);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
            .antMatchers("/**.html", "/v2/api-docs", "/webjars/**", "/configuration/**", "/swagger-resources/**");
    }
}

Rodrigo, onde estaria o erro? Será que a mudança da versão do Swagger para a mais recente (3.0.0) precisa de algo a mais?

Obrigado!

1 resposta

Oi Bruno,

Na versão 3.0.0 do Springfox Swagger teve mudanças em como configurar:

http://springfox.github.io/springfox/docs/current/#migrating-from-existing-2-x-version

Veja se consegue seguir o tutorial para ajustar.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software