spring cloud gateway authentication filter code example

Example: spring boot api gateway authentication filter

@RefreshScope@Componentpublic class AuthenticationFilter implements GatewayFilter {    @Autowired    private RouterValidator routerValidator;//custom route validator    @Autowired    private 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, "Authorization header is missing in request", HttpStatus.UNAUTHORIZED);            final String token = this.getAuthHeader(request);            if (jwtUtil.isInvalid(token))                return this.onError(exchange, "Authorization header is invalid", HttpStatus.UNAUTHORIZED);            this.populateRequestWithHeaders(exchange, token);        }        return chain.filter(exchange);    }    /*PRIVATE*/    private Mono<Void> onError(ServerWebExchange exchange, String err, HttpStatus httpStatus) {        ServerHttpResponse response = exchange.getResponse();        response.setStatusCode(httpStatus);        return response.setComplete();    }    private String getAuthHeader(ServerHttpRequest request) {        return request.getHeaders().getOrEmpty("Authorization").get(0);    }    private boolean isAuthMissing(ServerHttpRequest request) {        return !request.getHeaders().containsKey("Authorization");    }    private void populateRequestWithHeaders(ServerWebExchange exchange, String token) {        Claims claims = jwtUtil.getAllClaimsFromToken(token);        exchange.getRequest().mutate()                .header("id", String.valueOf(claims.get("id")))                .header("role", String.valueOf(claims.get("role")))                .build();    }}

Tags:

Java Example