Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

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

Olá, bom dia!

Ao tentar acessar a rota do swagger recebo um HTTP ERROR 403 ja validei o codigo e aparentemente está exatamente como foi passado na aula .

Class SecurityConfigurations

package br.com.alura.forum.config.security; import br.com.alura.forum.config.validacao.security.AutenticacaoService; 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;

import br.com.alura.forum.repository.UsuarioRepository;

@EnableWebSecurity @Configuration public class SecurityConfigurations extends WebSecurityConfigurerAdapter {

@Autowired private AutenticacaoService autenticacaoService;

@Autowired private TokenService tokenService;

@Autowired private UsuarioRepository usuarioRepository;

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

//Configuracoes de autenticacao @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(autenticacaoService).passwordEncoder(new BCryptPasswordEncoder()); }

//Configuracoes de autorizacao @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers(HttpMethod.GET, "/topicos").permitAll() .antMatchers(HttpMethod.GET, "/topicos/").permitAll() .antMatchers(HttpMethod.POST, "/auth").permitAll() .antMatchers(HttpMethod.GET, "/actuator/*").permitAll() .anyRequest().authenticated() .and().csrf().disable() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and().addFilterBefore(new AutenticacaoViaTokenFilter(tokenService, usuarioRepository), UsernamePasswordAuthenticationFilter.class); }

//Configuracoes de recursos estaticos(js, css, imagens, etc.) @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/.html", "/v2/api-docs", "/webjars/", "/configuration/", "/swagger-resources/"); }

}

Class SwaggerConfigurations

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

import java.util.Arrays;

import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;

import br.com.alura.forum.modelo.Usuario; import springfox.documentation.builders.ParameterBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.schema.ModelRef; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket;

@Configuration public class SwaggerConfigurations {

@Bean
public Docket forumApi() {
    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()));
}

}

Resposta 403 Insira aqui a descrição dessa imagem para ajudar na acessibilidade

Alguém pode me ajudar?

2 respostas

Oi Aline,

A principio essa linha do seu metodo configure, da classe SecurityConfigurations esta incorreta:

web.ignoring().antMatchers("/.html", "/v2/api-docs", "/webjars/", "/configuration/", "/swagger-resources/"); }

Altere para:

web.ignoring().antMatchers("/**.html", "/v2/api-docs", "/webjars/**", "/configuration/**", "/swagger-resources/**");
solução!

Obrigada Rodrigo!