简体   繁体   English

Java APIRest - 使用 GET 方法和 JWT 的 Postman 错误 400 错误请求

[英]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.

相关问题 Java服务400错误的请求错误 - java service 400 bad request error 错误400错误的请求 - Error 400 Bad Request 用于获取访问令牌的Google oauth java客户端失败,并显示“400 Bad Request {”error“:”invalid_request“}” - Google oauth java client to get an access token fails with “400 Bad Request { ”error“ : ”invalid_request“ }” 用于获取访问令牌的Google oauth java客户端失败,并显示“400 Bad Request {”error“:”invalid_request“}” - Google oauth java client to get an access token fails with “400 Bad Request { ”error“ : ”invalid_request“ }” "status": 400, "error": "Bad Request",方法不行 Java REST - "status": 400, "error": "Bad Request", the method does not work out Java REST Rest API 可以从 Postman 调用,显示来自 ZD52387880E1EA22831817A27 的 400 个错误请求 - Rest API can be called from Postman, shows 400 bad request from Java Spring Security JWT - 400 错误请求:“无效授权”“错误凭据” - Spring Security JWT - 400 Bad Request : “invalid grant” “Bad credentials” DocuSign Java JWT 身份验证失败,400 错误请求,需要同意 - DocuSign Java JWT authentication fails, 400 bad request, consent_required 发布方法 400 错误请求错误角度弹簧引导 - Post method 400 bad request error angular spring boot 使用纯Java 400搜索推文得到错误请求 - Search Tweets with pure Java 400 get Bad Request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM