java - Spring Cloud Gateway 中的身份验证

标签 java spring spring-boot spring-cloud spring-cloud-gateway

我是 Spring Cloud 的新手。我正在尝试集成 Spring Cloud Gateway(Spring Cloud Finchley.SR1) 只是为了验证和路由我的请求。

My Application stack is as follows

  • 主仪表板应用程序使用 Spring MVC 和 Angular 构建。它有登录页面和仪表板。其中集成了身份验证,可根据数据库对用户凭据进行身份验证。我还在这个应用程序中使用 hazelcast 进行 session 复制。
  • 一组下游服务,一旦用户登录到上述仪表板并在其中导航,就会调用这些服务。
  • 使用 Spring Cloud Gateway 构建的 API 网关:目前它仅执行路由操作。
  cloud:
    gateway:
      routes:
        - id: customers
          uri: http://localhost:8282/customers
          predicates:
          - Path=/customers/**
          - Header=employeeId, \d+
          filters:
          - RewritePath=/customers/(?<segment>.*), /$\{segment}
        - id: rates
          uri: http://localhost:8383/rates
          predicates:
          - Path=/rates/**
          filters:
          - RewritePath=/rates/(?<segment>.*), /$\{segment}

Need to implement following

  • 第一个请求将到达网关,我需要检查用户是否已通过身份验证,如果未通过身份验证,则我需要显示仪表板应用程序(即第一个应用程序)的登录页面,否则允许用户继续。
  • 用户登录并在仪表板内导航后,对下游服务的 API 请求也将通过 API 网关,我还需要对这些请求进行身份验证,如果用户未通过身份验证,则发送 403 状态。

I am not able to figure out how to handle these authentications in spring cloud gateway app. Is it possible to leverage hazelcast session replication that I added in Main Dashboard App, use it validate the user in gateway?

Or is there any better approach using spring cloud suite tools that I can use it here. A sample app will be useful

最佳答案

就我而言,我在 Spring Security 2.0.5 中自定义了 AuthenticationWebFilter 类。 我在AuthenticationWebFilter中定制了ServerAuthenticationConverter类和SuccessHandler、FailureHandler和ReactiveAuthenticationManager类。 如果您使用 Spring security,我建议搜索 WebFilter。 我希望它有帮助。

关于java - Spring Cloud Gateway 中的身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51891450/

相关文章:

java - 更改 JTable 行的颜色

Java 异常处理 - 这是好的做法吗?

java - (关于拦截器)spring @Controller 和 @RestController 的区别

java - SpringBoot、Kafka : java. lang.NoSuchMethodError : org. apache.kafka.clients.producer.Producer.close(Ljava/time/Duration;)V

spring-boot - 如何在使用命令行 Gradle (6.4) build 构建 Spring Boot 应用程序时传递 spring.config.location ="somepath"

java - JDBC 是否在将日期插入 Oracle DB 之前调整日期?我该如何防止这种情况?

java - 如何使用 datastore - google appengine 创建用户注册表单?

java - 创建bean并调用非setter方法

java - 无法在 OncePerRequestFilter 中注入(inject) bean

Java:在REST服务中传递二维数组