SpringCloudGateWay가 자신의 GateWayFilterFactory를 사용자 정의할 때(exchange,chain)
package com.quantex.scg;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import java.util.List;
public class WsSecurityGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
    @Override
    public GatewayFilter apply(NameValueConfig config) {
        return (exchange, chain) -> {
            List strings = exchange.getRequest().getHeaders().get(config.getName());
            if (strings!=null){
                ServerHttpRequest request = exchange.getRequest().mutate()
                        .header(config.getName(), strings.get(0))
                        .build();
//                System.out.println("-----------------------------------------");
//                System.out.println(strings.get(0));
                return chain.filter(exchange.mutate().request(request).build());
            }else{
                return chain.filter(exchange);
            }
        };
    }
}
 왜 이 함수식의 쓰기 (exchange,chain) -> {} 도대체 이 exchange와chain이 어떻게 왔는지, 전역 변수도 못 봤어. 상속된 부류의 전역 변수가 맞는지 생각해 봤어.나는 겹겹이 찾았지만 찾을 수 없었다. 정말 모르겠다. 나는 용기 위탁 관리인인 exchange와chain이라는 실례가 있는 줄 알고 머리를 깨뜨리려고 했지만 불가능하다고 생각했다. 나중에 apply() 함수의 반환 값을 봤는데 GateWay Filter였다. 나는 이 원본을 눌러서 다음과 같이 보았다.
package org.springframework.cloud.gateway.filter;
/*
 * Copyright 2002-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import org.springframework.cloud.gateway.support.ShortcutConfigurable;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
 * Contract for interception-style, chained processing of Web requests that may
 * be used to implement cross-cutting, application-agnostic requirements such
 * as security, timeouts, and others. Specific to a Gateway
 *
 * Copied from WebFilter
 *
 * @author Rossen Stoyanchev
 * @since 5.0
 */
public interface GatewayFilter extends ShortcutConfigurable {
	String NAME_KEY = "name";
	String VALUE_KEY = "value";
	/**
	 * Process the Web request and (optionally) delegate to the next
	 * {@code WebFilter} through the given {@link GatewayFilterChain}.
	 * @param exchange the current server exchange
	 * @param chain provides a way to delegate to the next filter
	 * @return {@code Mono} to indicate when request processing is complete
	 */
	Mono filter(ServerWebExchange exchange, GatewayFilterChain chain);
}
  결국 나는 이 니마가 JAVA8의 새로운 조작이 아니라는 것을 문득 깨달았다.인터페이스가 실현되지 않은 방법이 하나밖에 없을 때 만약에 되돌아오는 것이 하나의 함수라면 사실은 이 인터페이스를 되돌아오는 것은 실현 유형의 실례이다. 대응하는 실현 방법의 논리는 바로 그 함수이다. 이곳의 Gateway Filter의 filter 방법의 실현은 사실 다음과 같다.
(exchange, chain) -> {
            List strings = exchange.getRequest().getHeaders().get(config.getName());
            if (strings!=null){
                ServerHttpRequest request = exchange.getRequest().mutate()
                        .header(config.getName(), strings.get(0))
                        .build();
//                System.out.println("-----------------------------------------");
//                System.out.println(strings.get(0));
                return chain.filter(exchange.mutate().request(request).build());
            }else{
                return chain.filter(exchange);
            }
        }; 동시에 (exchange, chain) 인터페이스에 있는 filter의 두 가지 매개 변수를 맞췄습니다.
요약:java8의 특성을 알지만 조작이 적다.이런 게 생겨요. 아는데 못 알아보는 거예요.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.