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

Error creating bean with name 'springSecurityFilterChain'

Boa noite.

Esta dando erro quando executo o projeto, erro abaixo:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.RuntimeException: Could not postProcess org.springframework.security.authentication.dao.DaoAuthenticationProvider@4eb4415a of type class org.springframework.security.authentication.dao.DaoAuthenticationProvider

4 respostas

package br.com.alura.config.security;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service;

import br.com.alura.modelo.Usuario; import br.com.alura.repository.UsuarioRepository;

@Service public class AutenticacaoService implements UserDetailsService {

@Autowired
private UsuarioRepository repository;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    Optional<Usuario> usuario = repository.findByEmail(username);
    if (usuario.isPresent()) {
        return usuario.get();
    }

    throw new UsernameNotFoundException("Dados inválidos!");
}

}

package br.com.alura.config.security;

import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; 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.crypto.bcrypt.BCryptPasswordEncoder;

import ch.qos.logback.core.pattern.color.BoldCyanCompositeConverter;

@EnableWebSecurity @Configuration public class SecurityConfigurations extends WebSecurityConfigurerAdapter {

private AutenticacaoService autenticacaoService;

//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()
    .anyRequest().authenticated()
    .and().formLogin();

}

//Configuracoes de recursos estaticos
@Override
public void configure(WebSecurity web) throws Exception {

}


}
solução!

Oi Airlon,

Olhando o codigo vi que na sua classe SecurityConfigurations esta faltando a anotacao @Autowired no atributo AutenticacaoService autenticacaoService. Veja se o problema seria esse.

Show professor! funcionou. Obrigado