5
respostas

Erro ao testar no docker - Ao acessar o projeto na localhost:8080/topicos encaminha para tela login

Boa noite poderiam ajudar? Ao rodar a aplicação via Docker sou direcionado para a tela de login e mesmo tentando logar com user e senhas de banco não consigo acessar segue a classe Security:

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

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; 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 @Profile(value = {"prod","test"}) public class SecurityConfigurations extends WebSecurityConfigurerAdapter {

@Autowired
private AutenticacaoService autenticacaoService;
@Autowired
private TokenService tokenService;
@Autowired
private UsuarioRepository usuarioRepository;

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

// Configurações de autenticação ( controle de acesso login )
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(autenticacaoService).passwordEncoder(new BCryptPasswordEncoder());

}

// Configurações de autorização (url ,quem pode acessar cada url, perfil de
// acesso )
@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().antMatchers(HttpMethod.DELETE, "/topicos/*")
            .hasRole("MODERADOR").anyRequest().authenticated().and().csrf().disable().sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
            .addFilterBefore(new AutenticacaoViaTokenFilter(tokenService, usuarioRepository),
                    UsernamePasswordAuthenticationFilter.class);

}

// configurações de recursos estáticos ( requisições para arquivos, Js, CSS,
// imagens, etc )
@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/**.html", "/v2/api-docs", "/webjars/**", "/configuration/**",
            "/swagger-resources/**");

}

}

5 respostas

Oi Odair,

Pode postar o comando que voce utilizou para rodar o container docker?

Segue comando:

C:\eclipse-novo\forum>docker run -p 8080:8080 -e SPRING_PROFILES_ACTIVE='prod' -e FORUM_DATABASE_URL='jdbc:h2:mem:alura-forum' -e FORUM_DATABASE_USERNAME='sa' -e FORUM_DATABASE_PASSWORD='' -e FORUM_JWT_SECRET='123456' alura/forum

Oi Odair,

Parece ser um problema comum esse, pois outros alunos ja o reportaram aqui no forum.

Vou verificar se teve alguma mudanca nas novas versao do Spring Boot em relacao a isso e te retorno.

Muito obrigado Rodrigo Ferreira.

Boa noite,

Cheguei agora nessa aula e tive o mesmo problema, conseguiu resolver de alguma forma Odair?

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