首页 > 解决方案 > 我如何允许 http 标头从 JSON 中读取字符串?

问题描述

我正在尝试测试一种休息身份验证方法,但我在 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" 
}

标签: spring-bootspring-securityjwtpostman

解决方案


您在问题中缺少主要数据,这是您对/signinPOST 调用的请求正文。

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

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

您在通话中缺少 required LoginRequest


推荐阅读