-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d7679b
commit bd563b3
Showing
15 changed files
with
292 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>library-api-gateway</artifactId> | ||
<packaging>jar</packaging> | ||
<description>Library API Gateway</description> | ||
|
||
<parent> | ||
<groupId>dev.earlspilner</groupId> | ||
<artifactId>library-api</artifactId> | ||
<version>1.1.0</version> | ||
</parent> | ||
|
||
<dependencies> | ||
<!-- Spring Cloud Gateway --> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-gateway</artifactId> | ||
</dependency> | ||
|
||
<!-- Spring Cloud Netflix Eureka Client --> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId> | ||
<version>2.2.10.RELEASE</version> | ||
</dependency> | ||
|
||
<!-- Spring Web --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-webflux</artifactId> | ||
</dependency> | ||
|
||
<!-- JWT --> | ||
<dependency> | ||
<groupId>io.jsonwebtoken</groupId> | ||
<artifactId>jjwt-api</artifactId> | ||
<version>${jjwt.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.jsonwebtoken</groupId> | ||
<artifactId>jjwt-impl</artifactId> | ||
<version>${jjwt.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.jsonwebtoken</groupId> | ||
<artifactId>jjwt-jackson</artifactId> | ||
<version>${jjwt.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
18 changes: 18 additions & 0 deletions
18
library-api-gateway/src/main/java/dev/earlspilner/gateway/ApiGatewayApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package dev.earlspilner.gateway; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; | ||
|
||
/** | ||
* @author Alexander Dudkin | ||
*/ | ||
@EnableDiscoveryClient | ||
@SpringBootApplication | ||
public class ApiGatewayApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(ApiGatewayApplication.class, args); | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
library-api-gateway/src/main/java/dev/earlspilner/gateway/config/AuthenticationFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package dev.earlspilner.gateway.config; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.cloud.context.config.annotation.RefreshScope; | ||
import org.springframework.cloud.gateway.filter.GatewayFilter; | ||
import org.springframework.cloud.gateway.filter.GatewayFilterChain; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.server.reactive.ServerHttpRequest; | ||
import org.springframework.http.server.reactive.ServerHttpResponse; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* @author Alexander Dudkin | ||
*/ | ||
@Component | ||
@RefreshScope | ||
public class AuthenticationFilter implements GatewayFilter { | ||
|
||
private final RouterValidator routerValidator; | ||
private final JwtUtil jwtUtil; | ||
|
||
@Autowired | ||
public AuthenticationFilter(RouterValidator routerValidator, JwtUtil jwtUtil) { | ||
this.routerValidator = routerValidator; | ||
this.jwtUtil = jwtUtil; | ||
} | ||
|
||
@Override | ||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { | ||
ServerHttpRequest request = exchange.getRequest(); | ||
|
||
if (routerValidator.isSecured.test(request)) { | ||
if (this.isAuthMissing(request)) { | ||
return this.onError(exchange, HttpStatus.UNAUTHORIZED); | ||
} | ||
|
||
final String token = this.getAuthHeader(request); | ||
|
||
if (jwtUtil.isInvalid(token)) { | ||
return this.onError(exchange, HttpStatus.FORBIDDEN); | ||
} | ||
|
||
this.updateRequest(exchange, token); | ||
} | ||
|
||
return chain.filter(exchange); | ||
} | ||
|
||
private Mono<Void> onError(ServerWebExchange exchange, HttpStatus httpStatus) { | ||
ServerHttpResponse response = exchange.getResponse(); | ||
response.setStatusCode(httpStatus); | ||
return response.setComplete(); | ||
} | ||
|
||
private String getAuthHeader(ServerHttpRequest request) { | ||
String header = request.getHeaders().getOrEmpty("Authorization").get(0); | ||
return header.startsWith("Bearer ") ? header.substring(7) : header; | ||
} | ||
|
||
private boolean isAuthMissing(ServerHttpRequest request) { | ||
return !request.getHeaders().containsKey("Authorization"); | ||
} | ||
|
||
private void updateRequest(ServerWebExchange exchange, String token) { | ||
Claims claims = jwtUtil.getAllClaimsFromToken(token); | ||
exchange.getRequest().mutate() | ||
.header("username", String.valueOf(claims.get("username"))) | ||
.build(); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
library-api-gateway/src/main/java/dev/earlspilner/gateway/config/GatewayConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package dev.earlspilner.gateway.config; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.cloud.gateway.route.RouteLocator; | ||
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; | ||
import org.springframework.cloud.netflix.hystrix.EnableHystrix; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @author Alexander Dudkin | ||
*/ | ||
@Configuration | ||
@EnableHystrix | ||
public class GatewayConfig { | ||
|
||
static final String USERS_SERVICE = "http://localhost:9091"; | ||
static final String AUTH_SERVER = "http://localhost:6969"; | ||
static final String BOOKS_SERVICE = "http://localhost:9092"; | ||
static final String LOAN_SERVICE = "http://localhost:9093"; | ||
static final String LIBRARY_SERVICE = "http://localhost:9094"; | ||
|
||
private final AuthenticationFilter filter; | ||
|
||
@Autowired | ||
public GatewayConfig(AuthenticationFilter filter) { | ||
this.filter = filter; | ||
} | ||
|
||
@Bean | ||
public RouteLocator routes(RouteLocatorBuilder builder) { | ||
return builder.routes() | ||
.route("users-service", r -> r.path("/api/users/**") | ||
.filters(f -> f.filter(filter)) | ||
.uri(USERS_SERVICE)) | ||
.route("auth-server", r -> r.path("/api/auth/**") | ||
.filters(f -> f.filter(filter)) | ||
.uri(AUTH_SERVER)) | ||
.route("books-service", r -> r.path("/api/books/**") | ||
.filters(f -> f.filter(filter)) | ||
.uri(BOOKS_SERVICE)) | ||
.route("loan-service", r -> r.path("/api/loans/**") | ||
.filters(f -> f.filter(filter)) | ||
.uri(LOAN_SERVICE)) | ||
.route("library-service", r -> r.path("/api/library/**") | ||
.filters(f -> f.filter(filter)) | ||
.uri(LIBRARY_SERVICE)) | ||
.build(); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
library-api-gateway/src/main/java/dev/earlspilner/gateway/config/JwtUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package dev.earlspilner.gateway.config; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.security.Keys; | ||
import jakarta.annotation.PostConstruct; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.security.Key; | ||
import java.util.Base64; | ||
import java.util.Date; | ||
|
||
/** | ||
* @author Alexander Dudkin | ||
*/ | ||
@Component | ||
public class JwtUtil { | ||
|
||
@Value("${jwt.secret.key}") | ||
private String jwtSecret; | ||
|
||
private Key key; | ||
|
||
@PostConstruct | ||
protected void init() { | ||
byte[] keyBytes = Base64.getDecoder().decode(jwtSecret.getBytes()); | ||
this.key = Keys.hmacShaKeyFor(keyBytes); | ||
} | ||
|
||
public Claims getAllClaimsFromToken(String token) { | ||
return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody(); | ||
} | ||
|
||
private boolean isTokenExpired(String token) { | ||
return this.getAllClaimsFromToken(token).getExpiration().before(new Date()); | ||
} | ||
|
||
public boolean isInvalid(String token) { | ||
return this.isTokenExpired(token); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
library-api-gateway/src/main/java/dev/earlspilner/gateway/config/RouterValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package dev.earlspilner.gateway.config; | ||
|
||
import org.springframework.http.server.reactive.ServerHttpRequest; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* @author Alexander Dudkin | ||
*/ | ||
@Component | ||
public class RouterValidator { | ||
|
||
public static final List<String> openApiEndpoints = List.of( | ||
"/api/auth/login", | ||
"/api/auth/refresh", | ||
"/api/users" | ||
); | ||
|
||
public Predicate<ServerHttpRequest> isSecured = request -> openApiEndpoints.stream() | ||
.noneMatch(uri -> request.getURI().getPath().contains(uri)); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
spring: | ||
application: | ||
name: api-gateway | ||
|
||
server: | ||
port: 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters