我怎么能允许HTTP标头读取JSON字符串?

时间:2019-01-28 13:53:21

标签: spring-boot spring-security jwt postman

我正在尝试测试其余的身份验证方法,但我在邮递员内陷入了一些错误。该错误与HHTP标头有关,它无法从JSON读取消息,异常消息为: HttpMessageNotReadableException

这是RestController类:

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/auth")
public class AuthController {

    @Autowired
    AuthenticationManager authenticationManager;
    @Autowired
    EmployeRepository employeRepository;




    @Autowired
    PasswordEncoder passwordEncoder;

    @Autowired
    JwtTokenProvider tokenProvider;

    /**
     * METHODE D'AUTHENTIFICATION
     * 
     * @param loginRequest
     * @return
     */
    @PostMapping("/signin")
    @ApiImplicitParams(@ApiImplicitParam(name = "Authorization", value = "Bearer token", required = true, dataType = "String", paramType = "header"))
    public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest)  {
        Optional<Employe> ListEmployees = employeRepository.findByMail(loginRequest.getEmail());
        List<Employe> listEmpl = new ArrayList<>();
        ListEmployees.ifPresent(listEmpl::add);
        if (listEmpl.size() == 1) {
            final Employe empl = listEmpl.stream().findFirst().get();
            Boolean matches = passwordEncoder.matches(loginRequest.getPassword(),
                    listEmpl.stream().findFirst().get().getMp());
            if (matches.equals(true)) {
                if ( empl.getMail()!= null) {
                    Authentication authentication = authenticationManager.authenticate(
                    new UsernamePasswordAuthenticationToken(
                    loginRequest.getEmail(), loginRequest.getPassword()));
                    SecurityContextHolder.getContext().setAuthentication(authentication);
                    String jwt = tokenProvider.generateToken(authentication);
                    HttpHeaders headers = new HttpHeaders();
                    headers.add("Content-Type", "application/json");
                    headers.add("Authorization", new JwtAuthenticationResponse(jwt).getAccessToken());
                    return ResponseEntity.ok(new JwtAuthenticationResponse(jwt));
                } else if (empl.getMp()!= null) {
                    Authentication authentication = authenticationManager.authenticate(
                    new UsernamePasswordAuthenticationToken(
                    empl.getMail(), loginRequest.getPassword()));
                    SecurityContextHolder.getContext().setAuthentication(authentication);
                    String jwt = tokenProvider.generateToken(authentication);
                    return ResponseEntity.ok(new JwtAuthenticationResponse(jwt));
                }

            }
        }
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }}

邮递员URI:

  

http://localhost:8082/api/auth/signin

我收到的消息:

{
        "timestamp": 1548681879270,
        "status": 400,
        "error": "Bad Request",
        "exception": "org.springframework.http.converter.HttpMessageNotReadableException",
        "message": "Required request body is missing: public org.springframework.http.ResponseEntity<?> com.Cynapsys.Pointage.Controller.AuthController.authenticateUser(com.Cynapsys.Pointage.Model.LoginRequest)",
        "path": "/api/auth/signin" 
}

1 个答案:

答案 0 :(得分:1)

您缺少问题中的主要数据,而这是/signin POST呼叫的请求正文。

但是您的错误消息给出了解决问题的必要提示。

  

“ message”:“缺少所需的请求正文:公开   org.springframework.http.ResponseEntity   com.Cynapsys.Pointage.Controller.AuthController.authenticateUser(com.Cynapsys.Pointage.Model.LoginRequest)“

您在通话中缺少必填LoginRequest