spring cloud : e-commerce(user-service) - login filter
This commit is contained in:
@@ -25,6 +25,10 @@ ext {
|
||||
dependencies {
|
||||
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
|
||||
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
|
||||
|
||||
implementation group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
|
||||
implementation group: 'javax.xml.bind', name: 'jaxb-api'
|
||||
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
annotationProcessor 'org.projectlombok:lombok'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.example.apigatewayservice.filter;
|
||||
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
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;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AuthorizationHeaderFilter extends AbstractGatewayFilterFactory<AuthorizationHeaderFilter.Config> {
|
||||
|
||||
private final Environment env;
|
||||
|
||||
public AuthorizationHeaderFilter(Environment env) {
|
||||
super(Config.class);
|
||||
this.env = env;
|
||||
}
|
||||
|
||||
public static class Config {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public GatewayFilter apply(Config config) {
|
||||
return (exchange, chain) -> {
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
|
||||
if (!request.getHeaders().containsKey(HttpHeaders.AUTHORIZATION)) {
|
||||
return onError(exchange, "No authorization header", HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
String authorizationHeader = request.getHeaders().get(HttpHeaders.AUTHORIZATION).get(0);
|
||||
String jwt = authorizationHeader.replace("Bearer ", "");
|
||||
|
||||
if (!isJwtValid(jwt)) {
|
||||
return onError(exchange, "JWT token is not vaild", HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
return chain.filter(exchange);
|
||||
};
|
||||
}
|
||||
|
||||
private boolean isJwtValid(String jwt) {
|
||||
boolean returnValue = true;
|
||||
|
||||
String subject = null;
|
||||
|
||||
try {
|
||||
subject = Jwts.parser().setSigningKey(env.getProperty("token.secret"))
|
||||
.parseClaimsJws(jwt).getBody()
|
||||
.getSubject();
|
||||
} catch (Exception e) {
|
||||
returnValue = false;
|
||||
}
|
||||
|
||||
if (subject == null || subject.isEmpty()) {
|
||||
returnValue = false;
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
private Mono<Void> onError(ServerWebExchange exchange, String err, HttpStatus httpStatus) {
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
response.setStatusCode(httpStatus);
|
||||
|
||||
log.error(err);
|
||||
|
||||
return response.setComplete();
|
||||
}
|
||||
}
|
||||
@@ -34,10 +34,28 @@ spring:
|
||||
- id: user-service
|
||||
uri: lb://USER-SERVICE
|
||||
predicates:
|
||||
- Path=/user-service/**
|
||||
- Path=/user-service/login
|
||||
- Method=POST
|
||||
filters:
|
||||
- RemoveRequestHeader=Cookie
|
||||
- RewritePath=/user-service/(?<segment>.*), /$\{segment}
|
||||
- id: user-service
|
||||
uri: lb://USER-SERVICE
|
||||
predicates:
|
||||
- Path=/user-service/users
|
||||
- Method=POST
|
||||
filters:
|
||||
- RemoveRequestHeader=Cookie
|
||||
- RewritePath=/user-service/(?<segment>.*), /$\{segment}
|
||||
- id: user-service
|
||||
uri: lb://USER-SERVICE
|
||||
predicates:
|
||||
- Path=/user-service/**
|
||||
- Method=GET
|
||||
filters:
|
||||
- RemoveRequestHeader=Cookie
|
||||
- RewritePath=/user-service/(?<segment>.*), /$\{segment}
|
||||
- AuthorizationHeaderFilter
|
||||
|
||||
- id: first-service
|
||||
uri: lb://MY-FIRST-SERVICE
|
||||
@@ -61,3 +79,6 @@ spring:
|
||||
baseMessage: Hi, there.
|
||||
preLogger: true
|
||||
postLogger: true
|
||||
|
||||
token:
|
||||
secret: user_token
|
||||
Reference in New Issue
Block a user