我的应用程序具有多个微服务,包括:Auth,Game和Group。 当我按下前端(播放)上的按钮时,我正在调用Group rest api方法,我们说foo()。 为了在Group中实现foo(),我需要GET调用Game rest api,但这是安全的。
当我在React应用程序上登录时,我从/ login(Auth)获取JWT令牌并将其存储在localstorage中。然后,我成功地使用它从Group调用了foo(),但在foo()的实现中,我还需要使用jwt令牌,以便能够从Game中获取信息。
@Configuration
//+component scans...
public class GroupConfiguration {
@Bean
@LoadBalanced
public WebClient.Builder buildWebClientBuilder() {
return WebClient.builder();
}
}
@RestController
@RequestMapping("/groups")
public class Controller {
private final Logger logger = LogManager.getLogger();
@Autowired
private WebClient.Builder webClientBuilder;
private int getMinimumNumberOfPlayers(int gameId) {
try {
return webClientBuilder.build()
.get()
.uri("http://game-service/games/minimumNumberOfPlayers/2")
.retrieve()
.bodyToMono(Integer.class)
.block();
} catch (NullPointerException|WebClientResponseException e) {
e.printStackTrace();
return 0;
}
}
...
// foo() frontend calls foo(). foo it's using getMinimumNumberOfPlayers
getMinimumNumberOfPlayers()用于Group的foo()方法中。可以按ID检索游戏的最少玩家数量,但是游戏微服务受jwt保护,并且出现未经授权的错误。
所以我的问题是我如何才能使Group微服务能够调用Game微服务。
谢谢。
编辑:RestTemplate Interceptor 这就是我解决的方法。
答案 0 :(得分:2)
即使我有一些安全问题,我也会回答这个问题:
您可以做的是:
request context
中。request context
中,并将其添加到请求标头中。这里重要的是request context
。