从其他外部spring boot应用程序调用api

问题描述 投票:0回答:1

我有两个不同的springboot应用程序。每个都通过UsernameAndPasswordAuthentication机制和JWT启用了Spring安全性。我需要从另一个调用应用程序api之一,但是我不需要用户验证机密用户名和密码,因为这两个应用程序将具有不同的用户,如果每个请求的授权标头包含有效的JWT令牌,都将被授权。我需要的是,如果从外部应用程序调用的api应该检查Authorization标头具有“ Basic someAccessToken”。甚至有可能执行这种实现。

java spring spring-boot spring-security
1个回答
0
投票

[有可能:调用会在http authorization标头上设置JWT令牌(使用Bearer模式,这通常被接受为惯例)

HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Bearer " + jwtToken);

MyDto dto = new MyDto();
HttpEntity<MyDto> requestEntity = new HttpEntity<>(dto, headers);

ResponseEntity resp =
            new RestTemplate().exchange("http://localhost:8080/rest/service",
                    HttpMethod.POST, requestEntity, String.class);

在服务端获取令牌并验证访问权限:

public ResponseEntity<String> create(@RequestBody MyDto dto, @RequestHeader("authorization") String authorization) throws Exception {

    System.out.println("authorization: " + authorization);
© www.soinside.com 2019 - 2024. All rights reserved.