如何处理微服务架构中的一些仅限内部的端点?

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

我必须从其他微服务中的身份验证微服务获取一些敏感数据,但我也不想将该特定端点暴露给世界其他地方,它应该只能由我们的一些微服务访问。你能告诉我我该怎么做吗?就像我应该创建一些特定用户(例如 m1、m2),然后检查是否从其中之一调用端点?或者还有其他方法吗?我正在使用 Spring Boot 3 和 Spring Security。

我正在考虑解决方案,例如为数据库中的每个微服务创建一个单独的用户,然后在调用微服务的特定端点时,检查传入的请求是来自这些微服务之一还是来自外部世界。

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

只需在您的应用程序中添加一个拦截器来检查请求中的签名标头,您就可以使拦截器仅适用于特定端点:

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    // Get the signature from the request header
    String signature = request.getHeader("Signature");

    if (signature == null || !isValidSignature(signature, request)) {
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); // Set HTTP 401 Unauthorized
        response.getWriter().write("Invalid Signature");
        return false; // Prevent further handling of the request
    }

    return true; // Proceed with the request
}

您可以通过多种方式生成和验证签名,您只需确保所有微服务共享相同的秘密

© www.soinside.com 2019 - 2024. All rights reserved.