JWT在授权' HTTP请求标头

发布于 2025-02-09 08:47:35 字数 1304 浏览 0 评论 0原文

我创建了一个客户来消耗REST API。一切似乎都还可以,但是由于找不到JWT,我会遇到401个错误。 这是我所做的服务:

@Service
public class RestTemplateService {

    private static final String GET_RELATION_BY_ID = "https://cuco-query-sbg-cuco-dev.apps.scg01.scg.dev.bo2.paas.cloudcenter.corp/getRelationByCucoId/{cucoId}";
    private static final String JWT = "Bearer xxxx"

    RestTemplate restTemplate = new RestTemplate();

    public ResponseEntity<CuCoPerson> getRelationById(Integer cucoId) {

        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        headers.add("Authorization", JWT);
        HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
        Map<String, Integer> param = new HashMap<String, Integer>();
        param.put("cucoId", cucoId);
        return restTemplate.exchange(GET_RELATION_BY_ID, HttpMethod.GET, entity, CuCoPerson.class, param);

    }
}

在这里,控制器:

 @GetMapping("/getRelationById/{cucoId}")
    public ResponseEntity<CuCoPerson> getRelationByCucoId(@PathVariable Integer cucoId, @AuthenticationPrincipal Principal principal) {
        return restTemplateService.getRelationById(cucoId);
    }

那么,我在哪里无法获得该令牌?

提前致谢!!

I've created a Client to consume a Rest API. Everything seems to be OK but I'm getting an 401 error as the JWT was not found.
Here is the service which I've made:

@Service
public class RestTemplateService {

    private static final String GET_RELATION_BY_ID = "https://cuco-query-sbg-cuco-dev.apps.scg01.scg.dev.bo2.paas.cloudcenter.corp/getRelationByCucoId/{cucoId}";
    private static final String JWT = "Bearer xxxx"

    RestTemplate restTemplate = new RestTemplate();

    public ResponseEntity<CuCoPerson> getRelationById(Integer cucoId) {

        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        headers.add("Authorization", JWT);
        HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
        Map<String, Integer> param = new HashMap<String, Integer>();
        param.put("cucoId", cucoId);
        return restTemplate.exchange(GET_RELATION_BY_ID, HttpMethod.GET, entity, CuCoPerson.class, param);

    }
}

And here the controller:

 @GetMapping("/getRelationById/{cucoId}")
    public ResponseEntity<CuCoPerson> getRelationByCucoId(@PathVariable Integer cucoId, @AuthenticationPrincipal Principal principal) {
        return restTemplateService.getRelationById(cucoId);
    }

So, where I'm failing to get that TOKEN?

Thanks in advance!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文