Olá meu método gerarToken não compila, avaliei tudo que pude e não sei onde estou errando:
@RestController @RequestMapping("/auth") public class AutenticacaoController {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private TokenService tokenService;
@PostMapping
public ResponseEntity<?> autenticar(@RequestBody @Valid LoginForm form) {
try {
UsernamePasswordAuthenticationToken dadosLogin = form.converter();
Authentication authentication = authenticationManager.authenticate(dadosLogin);
System.out.println("autentication: " + authentication);
String token = tokenService.gerarToken(authentication);
System.out.println("gerarToken: " + token);
return ResponseEntity.ok().build();
} catch (AuthenticationException e) {
return ResponseEntity.badRequest().build();
}
}
}
@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() + Long.parseLong(expiration));
System.out.println("dataExpiracao: " + dataExpiracao);
return Jwts.builder().setIssuer("API do fórum da Alura").setSubject(logado.getId().toString()).setIssuedAt(hoje)
.setExpiration(dataExpiracao).signWith(SignatureAlgorithm.HS256, secret).compact();
}
}
datasource
spring.datasource.driverClassName=org.h2.Driver spring.datasource.url=jdbc:h2:mem:alura-forum spring.datasource.username=sa spring.datasource.password=
jpa
spring.jpa.database-palataform=org.hibernate.dialect.H2Dialect spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.show_sql=true spring.jpa.properties.hibernate.format_sql=true
h2
spring.h2.console.enabled=true spring.h2.console.path=/h2-console
jwt
forum.jwt.secret=rm'!@N=Ke!p8VTA2ZRKnMDQX5Uvm!m'D&]{@Vr?G;2?XhbC:Qa#9#eMLN}x3?JR3.2zrv)gYF^8:8>:XfB:Ww75N/emt9Yj[bQMNCWwW\J?N,nvH.<2.rw]e~vgak)X"v8HMH/7"2E
,^k@n<vE-wD3g9JWPy;CrY.Kd2_D])=><D?YhBaSua5hW%{2]_FVXzb98FH^b[X3jzVER&:jw2<=c38=>L/zBq
}C6tT*cCSVC^c]-L}&/
forum.jwt.expiration=86400000
No console aparece:
APPLICATION FAILED TO START
Description:
Field tokenService in br.com.alura.forum.controller.AutenticacaoController required a bean of type 'org.springframework.security.core.token.TokenService' 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.core.token.TokenService' in your configuration.
No entanto na Classe AutenticacaoController eu tenho o import :
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.security.core.token.TokenService; 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.controller.form.LoginForm;