2
respostas

APPLICATION FAILED TO START

Description:

Field authManager in br.com.alura.forum.controller.AutenticacaoController required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.

The injection point has the following annotations:

- @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.

Classe Token Service

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

import java.util.Date;

import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.Authentication; import org.springframework.stereotype.Service;

import br.com.alura.forum.modelo.Usuario; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm;

@Service public class TokenService {

@Value("${forum.jwt.expiration}")
private String expiration;

@Value("${forum.jwt.secret}")
private String secret;

public String gerarToken(Authentication authentication) {
    Usuario logado = (Usuario) authentication.getPrincipal();
    Date hoje = new Date();
    Date dataExpiracao = new Date(hoje.getTime() + expiration);

    return Jwts.builder()
            .setIssuer("API do Fórum da Alura")
            .setSubject(logado.getId().toString())
            .setIssuedAt(hoje)
            .setExpiration(hoje)
            .signWith(SignatureAlgorithm.HS256, secret)
            .compact();
}

}

Classe AutenticacaoController

package br.com.alura.forum.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;

import br.com.alura.forum.config.security.TokenService; import br.com.alura.forum.controller.form.LoginForm;

@RestController @RequestMapping("/auth") public class AutenticacaoController {

@Autowired
private AuthenticationManager authManager;

@Autowired
private TokenService tokenService;

@PostMapping
public ResponseEntity<?> autenticar(@RequestBody @Valid LoginForm form){
    UsernamePasswordAuthenticationToken dadosLogin = form.converter();

    try {
        Authentication authentication = authManager.authenticate(dadosLogin);
        String token = tokenService.gerarToken(authentication);
        System.out.println(token);


        return ResponseEntity.ok().build();
    } catch (AuthenticationException e) {
        return ResponseEntity.badRequest().build();
    }

}

}

2 respostas

acho que a sua classe está faltando o construtor na sua classe token service e jogar isso la na security configurations ai voce pode injetar com o @autowired exemplo :

public AutenticacaoViaToken(UsuarioRepository usuarioRepository, TokenService tokenService) { this.usuarioRepository = usuarioRepository;

    this.tokenService = tokenService;

}


public class SecutiryConfigurations {
@Autowired
private  UsuarioRepository usuarioRepository;
@Autowired
private TokenService tokenService;

//metodos } ve se funciona

Oi Gabriel,

Confere se na sua classe SecurityConfigurations tem esse método configurando o bean AuthenticationManager:

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