6
respostas

erro ao enviar requisição auth http://localhost:8080/auth

Estou recebendo erro ao enviar o post

{
    "email":"aluno@email.com",
    "senha":"123456"
}

Erro

{
    "timestamp": "2021-04-05T04:30:01.048+0000",
    "status": 400,
    "error": "Bad Request",
    "message": "Required request body is missing: public org.springframework.http.ResponseEntity<br.com.alura.forum.controller.dto.TokenDto> br.com.alura.forum.controller.AutenticacaoController.autenticar(br.com.alura.forum.controller.dto.LoginForm)",
    "trace": "org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public org.springframework.http.ResponseEntity<br.com.alura.forum.controller.dto.TokenDto> br.com.alura.forum.controller.AutenticacaoController.autenticar(br.com.alura.forum.controller.dto.LoginForm)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:161)\n\tat 
6 respostas

Oi Omar,

Parece ser problema de envio incorreto dos dados no postman.

Verifica se você está preenchendo os dados corretamente no postman:

O retorno o forbidden

{
    "timestamp": "2021-04-05T21:46:43.029+0000",
    "status": 403,
    "error": "Forbidden",
    "message": "Access Denied",
    "path": "/auth"
}
```Erro no console do eclipse. 

2021-04-05 18:58:22.220 INFO 13712 --- [nio-8080-exec-2] o.apache.coyote.http11.Http11Processor : Error parsing HTTP request header Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.

java.lang.IllegalArgumentException: Invalid character found in method name [{0x0a]. HTTP method names must be tokens at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:418) ~[tomcat-embed-core-9.0.37.jar:9.0.37] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:260) ~[tomcat-embed-core-9.0.37.jar:9.0.37] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.37.jar:9.0.37] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.37.jar:9.0.37] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589) [tomcat-embed-core-9.0.37.jar:9.0.37] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.37.jar:9.0.37] at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_251] at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_251] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.37.jar:9.0.37] at java.lang.Thread.run(Unknown Source) [na:1.8.0_251] `

Quando tento fazer via post é aquele erro apontado na mensagem inicial .

A requisição é POST mesmo. Na foto acabou indo GET por engano.

Posta aqui o código das suas classes LoginController e LoginForm, por favor.

package br.com.alura.forum.controller.dto;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;

public class LoginForm {

      public String email;
      public String senha;


    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getSenha() {
        return senha;
    }
    public void setSenha(String senha) {
        this.senha = senha;
    }
    public UsernamePasswordAuthenticationToken converter() {
        return  new UsernamePasswordAuthenticationToken(email, senha);
    }

}

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.dto.LoginForm; import br.com.alura.forum.controller.dto.TokenDto;

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

    @Autowired
    private AuthenticationManager authManager;

    @Autowired
    private TokenService tokenService;

    @PostMapping
    public ResponseEntity<TokenDto> autenticar(@RequestBody @Valid LoginForm form){
        UsernamePasswordAuthenticationToken dadosLogin = form.converter();
        System.out.println(form.getEmail());
        System.out.println(form.getSenha());
        try {
        Authentication authentication = authManager.authenticate(dadosLogin);
        String token = tokenService.gerarToken(authentication);
            return ResponseEntity.ok(new TokenDto(token,"Bearer")); 

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


    }

}

alguma resposta sobre o erro ?

Oi Omar,

O código está correto.

O problema então é no envio do json pelo postman, pois pela exception o Spring está acusando que não veio o corpo da requisição.