[英]Java APIRest - Postman error 400 bad request with GET method and JWT
When I log in to my API via postman, a JWT is generated.当我通过邮递员登录我的 API 时,会生成一个 JWT。 With this jwt I create data for a specific user.
使用此 jwt,我为特定用户创建数据。 But when I am connected with this user and his JWT, when I make a GET request from POSTMAN with the connected user's JWT, I get a 400 error saying that the request is bad.
但是当我与这个用户和他的 JWT 连接时,当我使用连接用户的 JWT 从 POSTMAN 发出 GET 请求时,我收到一个 400 错误,说明请求不正确。 I do not understand why.
我不懂为什么。 My Tomcat server port IS NOT 8080 but 7777 ... Just bellow my controller with GET method :
我的 Tomcat 服务器端口不是 8080 而是 7777 ... 只需使用 GET 方法在我的控制器下方:
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/weights")
public class WeightRecordController {
@Autowired
AppUserRepository appUserRepository;
@Autowired
PersonRepository personRepository;
private final Logger logger = LoggerFactory.getLogger(WeightRecordController.class);
public Long getAppUserConnectedId(Principal principal) {
if (!(principal instanceof UsernamePasswordAuthenticationToken)) {
throw new RuntimeException(("User not found"));
}
logger.info("USER IS PRESENT IN DATABASE FROM FUNCTION 'getAppUserConnectedId()'");
UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) principal;
AppUser appUserFinded = appUserRepository.findByAppUsername(token.getName());
return appUserFinded.getIdUser();
}
@GetMapping("/all")
public ResponseEntity<List<WeightRecord>> getAllWeights(@RequestParam Principal principal) {
logger.info("GET /weights/all");
Long appUserConnectedId = this.getAppUserConnectedId(principal);
Person personToShow = personRepository.findById(appUserConnectedId).orElseThrow();
return new ResponseEntity<List<WeightRecord>>(personToShow.getWeightsList(), HttpStatus.OK);
}
}
I don't know why Tomcat runs on port 8080 and you call API on port 7777, but it returns code 400. it means that you pass authentication of the app.我不知道为什么Tomcat在8080端口上运行,而你在7777端口调用API,但它返回代码400。这意味着你通过了应用程序的身份验证。 your get request @RequestParam principal but it doesn't present on your request
您的获取请求 @RequestParam 主体,但它没有出现在您的请求中
In fact JWT token is transfered by request's header, so i have simply delete the @RequestParam from my endpoint and it work !实际上,JWT 令牌是由请求的标头传输的,所以我只需从端点中删除 @RequestParam 就可以了! Just bellow my new code for the endpoint concerned :
只需下面我为相关端点的新代码:
@GetMapping("/all")
public ResponseEntity<List<WeightRecord>> getAllWeights(Principal principal) {
logger.info("GET /weights/all");
Long appUserConnectedId = this.getAppUserConnectedId(principal);
Person personToShow = personRepository.findById(appUserConnectedId).orElseThrow();
return new ResponseEntity<List<WeightRecord>>(personToShow.getWeightsList(), HttpStatus.OK);
}
If you're using port 8080 on your tomcat, why are you using the port 7777 on Postman ?如果您在 tomcat 上使用端口 8080,为什么在 Postman 上使用端口 7777 ?
Also, try adding the following property in your spring boot configuration logging.level.org.springframework.web=DEBUG
you will get the exact reason for getting 400 Bad requests on console logs.此外,尝试在您的 Spring Boot 配置
logging.level.org.springframework.web=DEBUG
中添加以下属性,您将获得在控制台日志上收到 400 Bad requests 的确切原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.