【问题标题】:Java APIRest - Postman error 400 bad request with GET method and JWTJava API Rest - 使用 GET 方法和 JWT 的 Postman 错误 400 错误请求
【发布时间】:2022-07-17 23:13:31
【问题描述】:

当我通过邮递员登录我的 API 时,会生成一个 JWT。使用此 jwt,我为特定用户创建数据。但是当我与这个用户和他的 JWT 连接时,当我使用连接用户的 JWT 从 POSTMAN 发出 GET 请求时,我收到一个 400 错误,说明请求不正确。我不懂为什么。我的 Tomcat 服务器端口不是 8080... 只需使用 GET 方法向我的控制器吼叫:

    @RestController
    @RequestMapping("/weights")
    public class WeightRecordController {
    
        @Autowired
        WeightRecordServiceImpl weightRecordServiceImpl;
    
        @Autowired
        WeightRecordRepository weightRecordRepository;
    
        @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);
        }
}

【问题讨论】:

    标签: java spring rest jwt postman


    【解决方案1】:

    如果您在 tomcat 上使用端口 8080,为什么在 Postman 上使用端口 7777?

    【讨论】:

      猜你喜欢
      • 2022-11-10
      • 1970-01-01
      • 2019-06-21
      • 2016-11-06
      • 2016-04-13
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多