我有一个springboot应用程序。端点是安全的,它们需要jwt令牌授权才能被访问。应用程序中有一个端点,该端点接受用户名和密码并返回jwt令牌。现在,可以在授权标头中设置此令牌,并且可以访问其他安全api。
我还有另一个springboot应用程序,它充当zuul api网关。如何通过此zuul应用程序调用我的安全api。我是微服务架构的新手。请指教。
我的建议是您仅应在网关(Zuul)级别进行身份验证。
在微服务间调用期间您不应进行身份验证。
您的微服务将全部通过网关访问,因此将受到保护。
如果仍然要使用JWT令牌机制进行微服务间调用,请使用下面的示例-
假设我有一个名为search-service作为微服务的服务。
创建服务以调用登录端点并获取令牌,然后将该令牌传递到Authorization标头中,例如:“ Bearer” <>
@FeignClient(name = "search-service")
public interface SearchService
{
@RequestMapping(method = RequestMethod.POST , value = "/api-search/user/v2/search" , consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
List<Map<String, List<Map<String, Object>>>> search(@RequestHeader("Authorization")String token, String query);
@RequestMapping(method = RequestMethod.POST , value = "/api-search/auth/login", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public AuthResponseDto login(@RequestBody AuthRequestDto authRequestDto);
}