Using the auth filter in an api gateway Spring boot - spring-boot

I made 2 microservices running on a cluster in Kubernetes , I was able to make an API gateway using spring cloud to call those microservices , and I made a JWT Authorization and Authentication filters in another project .
My question is, how can I integrate these filters in my API gateway to authorize the access to my microservices
spring:
cloud:
gateway:
routes:
- id: r1
uri: http://192.168.49.2:30288/
predicates:
- Path= /api1/**
- id: r2
uri: http://192.168.49.2:30289/
predicates:
- Path= /api2/**
discovery:
enabled: false
server:
port: 8888

Related

Is there a way to route sub calls as well from spring cloud gateway routes?

I am building a spring cloud gateway to act as gateway with multiple backends. Some backends are internally calling websocket call ws://x.x.x.x:0000
user -> http://localhost:8085/ -> http://x.x.x.x:0000 -> ws://x.x.x.x:0000/websockify
if i don't explicitly setup a route for websocket call as well, it is taking localhost:8085 as host and port from origin server but not from http://x.x.x.x:0000.
Below is my application.yaml of spring cloud gateway and it is working. But i dont want to mention websocket_route which is being made internally from first route uri (http://x.x.x.x:0000) and i have other apps also internally making similar calls so can't setup individual routes for every app. please help
spring:
cloud:
gateway:
routes:
- id: inst-user1-target-77
uri: http://x.x.x.x:0000
predicates:
- Path=/inst-user1-target-77/**
filters:
- RewritePath=/inst-user1-target-77/(?<segment>.*), /$\{segment}
- id: websocket_route
uri: ws://x.x.x.x:0000
predicates:
- Path=/websockify

Spring boot application ,accessing routs with gateway service gives white label error?

Hey Guys I was developing a simple project with spring boot, which has 2 services user and blogs and I am using eureka server and I tried to implement a gateway service and it also got registered on the eureka server also but I cannot access the route paths of either user or blog service can anyone please help me with this. This the application.yml file for the gateway service.
server:
port: 3030
spring:
application:
name: gateway-service
cloud:
gateway:
routes:
- id: userModule
uri: http://localhost:3001/
predicates:
- Path=/users/**
- id: blogModule
uri: http://localhost:3002/
predicates:
- Path=/blogs/**
eureka:
client:
serviceUrl:
defaultZone: http://localhost:3000/eureka
registerWithEureka:
- true
fetchRegistry:
- true

How to use SetPath with query strings in Spring Cloud Gateway

I am using Spring Cloud Gateway and I am trying to remap a url to another one (using the SetPath filter) that uses a query string (involves a ?).
Consider the Spring Cloud Gateway config YAML below:
spring:
cloud:
gateway:
routes:
- id: demoEndpoint
uri: lb://demo-service
predicates:
- Path=/api/v1/user/{userId}/items
filters:
- SetPath=/items?searchUser={userId}
When I do a GET request on the endpoint (/api/v1/user/testUserId/items), Spring Cloud Gateway maps the request to: lb://demo-service/items but what I expected to get is lb://demo-service/items?searchUser=testUserId
It appears that Gateway has some issue when a question mark is involved in the path.
Question
How do I use query string in SetPath? Do I need to escape the ? character? Or is this a bug in Spring Cloud Gateway?
Other Info
I have tried this other config:
spring:
cloud:
gateway:
routes:
- id: demoEndpoint
uri: lb://demo-service
predicates:
- Path=/api/v1/user/{userId}/items
filters:
- SetPath=/items/searchUser={userId}
When I do a GET request on the endpoint (/api/v1/user/testUserId/items), Spring Cloud Gateway maps the request to: lb://demo-service/items/searchUser=testUserId
This helps me confirm that setPath is finding the uri segments properly (userId) and that only usage of the question mark might be causing issues.
Spring Cloud version: 2021.0.1
Spring Cloud Gateway version: 3.1.1

Path Definition For Each Route in Cloud Gateway

I would like to add a path to each route that I define in spring.cloud.gateway.routes without defining context path, server.servlet.context-path, of each microservice.
E.g.: My gateway runs at port 9090, and my microservice runs at port 8071. The configuration for the microservice in the gateway is like this:
spring:
cloud:
gateway:
routes:
- id: auth
uri: 'http://localhost:8071'
If I hit http://localhost:9090/lorem/ipsum, I can ping the microservice, and see the response. But, I would like to ping the microservice from the gateway as http://localhost:9090/auth/lorem/ipsum.
If I define Path key in predicates, it pings http://localhost:8071/auth/lorem/ipsum, which fails, because the URL in the microservice is not /auth/lorem/ipsum but /lorem/ipsum.
spring:
cloud:
gateway:
routes:
- id: auth
uri: 'http://localhost:8071'
predicates:
- Path=/auth/**
Is there a way to define a path for each microservice or do I have to set server.servlet.context-path for each microservice to achieve what I would like to do?
Spring boot version: 2.6.1 & Spring cloud version: 2021.0.0

How Eureka Server and Spring Cloud API Gateway communicates with each other?

In Spring Boot Microservices architecture, we generally register our each microservice (its many instances) into Eureka server by doing eureka.client.register-with-eureka=true, eureka.client.fetch-registry=true and eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka. So Eureka acts as service registry for these services (service-name, hostname and its IP).
Spring Cloud API Gateway acts as a single point of entry for any microservice call. It can work as a proxy service to route a request to the concerned microservice, abstracting the producer details. It has route information only, then how Spring Cloud API gateway comes to know which microservice instance to call to? How API Gateway and Eureka communicates and load balance?
spring:
application:
name: api-gateway
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: user-service
uri: lb://user-service
predicates:
- Path=/users/**
- id: order-service
uri: lb://department-service
predicates:
- Path=/departments/**
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
With the Discovery Locator enabled in API Gateway, you do not have to manually configure the routes unless absolutely needed.
The way API Gateway knows which Eureka Service to route the incoming request to is as follows:
Assuming,
Orders Service runs on http://localhost:8080
API Gateway runs on http://localhost:8082
Eureka Service name for Orders Service - order-service
Then if order-service getOrders endpoint: http://localhost:8080/orders, then with Discovery Locator enabled the request needs to be routed through the API gateway with the following URL: https://localhost:8082/order-service/orders, i.e., {ApiGatewayHost}/{EurekaServiceId}/{ActualEndpoint}.
Register Gatway to Eureka Server

Resources