4
respostas

Erro ao acessar swagger-ui

Estou com o mesmo erro citado aqui: https://cursos.alura.com.br/forum/topico-http-localhost-8080-swagger-ui-html-167458 Mas a linha mencionada para substituição já está como recomendado.

package br.com.alura.forum.config.security;

import br.com.alura.forum.repository.UsuarioRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@EnableWebSecurity @Configuration public class SecurityConfigurations extends WebSecurityConfigurerAdapter {

@Autowired
private AutenticacaoServices autenticacaoServices;

@Autowired
private TokenService tokenService;

@Autowired
private UsuarioRepository usuarioRepository;

@Override
@Bean
protected AuthenticationManager authenticationManager() throws Exception {
    return super.authenticationManager();
}

//Que controla acesso com autenticações.
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {

    auth.userDetailsService(autenticacaoServices).passwordEncoder(new BCryptPasswordEncoder());
}

//Determina os perfis de acesso.
@Override
protected void configure(HttpSecurity http) throws Exception {

    http.authorizeRequests()
            .antMatchers(HttpMethod.GET, "/topicos/listarTodos").permitAll()
            .antMatchers(HttpMethod.GET, "/topicos/listarPorCurso").permitAll()
            .antMatchers(HttpMethod.GET, "/topicos/listarPorCursoComQuery").permitAll()
            .antMatchers(HttpMethod.POST, "/auth").permitAll()
            .antMatchers(HttpMethod.POST, "/actuator**").permitAll()//Em produção não deve ser acesso público
            .anyRequest().authenticated()
            .and().csrf().disable()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and().addFilterBefore(new AutenticacaoViaTokenFilter(tokenService, usuarioRepository), UsernamePasswordAuthenticationFilter.class);

}

//Configura acesso a recursos de front-end (Arquivos js, css, imagens, etc).
@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/**.html", "/v2/api-docs", "/webjars/**", "/configuration/**", "/swagger-resources/**");
}

/* public static void main(String[] args) { System.out.println(new BCryptPasswordEncoder().encode("123456")); }*/ }

package br.com.alura.forum.config.swagger;

import java.util.Arrays;

@Configuration public class SwaggerConfigurations {

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("br.com.alura.forum"))
            .paths(PathSelectors.ant("/**"))
            .build()
            .ignoredParameterTypes(Usuario.class)
            .globalOperationParameters(
                    Arrays.asList(
                            new ParameterBuilder()
                                    .name("Authorization")
                                    .description("Header para Token JWT")
                                    .modelRef(new ModelRef("string"))
                                    .parameterType("header")
                                    .required(false)
                                    .build()));
}

}

package br.com.alura.forum;

@SpringBootApplication @EnableSpringDataWebSupport @EnableCaching @EnableSwagger2

public class ForumApplication {

public static void main(String[] args) {
    SpringApplication.run(ForumApplication.class, args);
}

}

4 respostas

Oi Amber,

O código está certinho.

Você entrou no https://localhost:8080/swagger-ui.html e deu o erro 403?

Sim, deu esse erro. Só não consegui mandar o print pq na minha empresa não consigo fazer upload, mas é o mesmo erro do topico que citei no início desse.

Consegue compartilhar o seu projeto?

Eu alterei a linha .apis(RequestHandlerSelectors.basePackage("br.com.alura.forum")) de acordo com o que está no do XML e deu certo