繁体   English   中英

org.springframework.http.converter.HttpMessageNotReadableException:缺少所需的请求正文 Spring 启动项目

[英]org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing Spring boot project

我尝试进行身份验证 JWT,但是当我抛出我的应用程序时,抛出此错误:

WARN 18692 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing

我收到 400 Bad Request 错误,说缺少请求正文

{
"timestamp": "2021-10-06T05:33:49.501+00:00",
"status": 400,
"error": "Bad Request",
"path": "/task-market/api/auth/authenticate"}

这是我的控制器:

@PostMapping("/authenticate")
public ResponseEntity<AuthenticationResponse> createToken(@RequestBody AuthenticationRequest request) {
    try {
        authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(request.getUsername(), request.getPassword()));
        UserDetails userDetails = marketUserDetailsService.loadUserByUsername(request.getUsername());
        String jwt = jwtUtil.generateToken(userDetails);

        return new ResponseEntity<>(new AuthenticationResponse(jwt), HttpStatus.OK);
    } catch (BadCredentialsException e) {
        return new ResponseEntity<>(HttpStatus.FORBIDDEN);
    }
}

这是安全层:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().authorizeRequests().antMatchers("/**/authenticate").permitAll()
            .anyRequest().authenticated();
}

@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}

您必须将用户名和密码传递给Body

喜欢

{
"username":"your_username"
"password":"your_password"
}

并且需要将媒体类型Text更改为JSON

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM