【发布时间】:2019-06-21 13:02:26
【问题描述】:
我正在尝试测试一种休息身份验证方法,但我在 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:
我收到的信息:
{
"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-boot spring-security jwt postman