Spring Cloud - 20. Users Microservice AuthorizationHeaderFilter 추가
목차 Spring Cloud - 20. Users Microservice AuthorizationHeaderFilter 추가 Spring Cloud - 19. Users Microservice JWT 생성 Spring Cloud - 18. Users Microservice 로그인 성공 처리 Spring Cloud - 17. Users Microservice Routes 정보 변경 Spring Cloud - 16. Users Microservice loadUserByUsername() 구현 Spring Cloud - 15. Users Microservice AuthenticationFilter Spring Cloud - 14. Users Microservice Order Service Spring Cloud - 13. Users Microservice Catalog Spring Cloud - 12. Users Microservice 사용자 조회 Spring Cloud - 11. Users Microservice Gateway 연동 Spring Cloud - 10. Users Microservice 사용자 추가 Spring Cloud로 개발하는 마이크로서비스 애플리케이션 9 Spring Cloud - Users Microservice Users Microservice AuthorizationHeaderFilter 추가@Component@Slf4jpublic class AuthorizationHeaderFilter extends AbstractGatewayFilterFactory<AuthorizationHeaderFilter.Config> { private Environment env; public AuthorizationHeaderFilter(Environment env){ super(Config.class); this.env = env; } // login -> token -> users (with token) -> header(include token) @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 valid", HttpStatus.UNAUTHORIZED); } return chain.filter(exchange); }); } private boolean isJwtValid(String jwt){ boolean returnValue = true; String subject = null; String key = env.getProperty("token.secret"); try { subject = Jwts.parser() .setSigningKey(env.getProperty("token.secret")) .parseClaimsJws(jwt).getBody() .getSubject(); } catch (Exception ex){ returnValue = false; } if(subject == null || subject.isEmpty()){ returnValue = false; } return returnValue; } private Mono<Void> onError(ServerWebExchange exchange, String error, HttpStatus httpStatus) { ServerHttpResponse response = exchange.getResponse(); response.setStatusCode(httpStatus); log.error(error); return response.setComplete(); } public static class Config{ }} // https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1' server: port: 8080eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:8761/eurekaspring: application: name: apigateway-service cloud: gateway: default-filters: - name: GlobalFilter args: baseMessage: Spring Cloud Gateway Global Filter preLogger: true postLogger: true routes:# - id: user-service# uri: lb://USER-SERVICE# predicates:# - Path=/user-service/**# filters:# - CustomFilter - id: user-service uri: lb://USER-SERVICE predicates: - 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: catalog-service uri: lb://CATALOG-SERVICE predicates: - Path=/catalog-service/** filters: - CustomFilter - id: order-service uri: lb://ORDER-SERVICE predicates: - Path=/order-service/** filters: - CustomFiltertoken: secret: user_token