����λ�ã���ҳ > �����̳� > �̳� > ��ȳ��Ľ���SpringWebFlux��Ӧʽ���15���������Դ��

��ȳ��Ľ���SpringWebFlux��Ӧʽ���15���������Դ��

��Դ������������|��ʱ�䣺2024-07-04 11:48:59 |���Ķ���111��|�� ��ǩ�� Ri ���� S in �� |����������

������Spring WebFlux ��ܺ��������ȫ�������ˣ�ϣ�����԰�����ȫ����������� WebFlux��ԭ������ע�����簮��̡�����ҳ��ɲ鿴V��ÿ����µ�ԭ���������ݣ�������һ��ɳ���

Spring WebFlux ����

Spring WebFlux �� Spring Framework 5.0 �汾�����һ����Ӧʽ Web ��ܣ����� Spring MVC ���棬�ṩ��һ��ȫ�µı�̷�ʽ��֧���첽�������� Web Ӧ�ÿ�����WebFlux ��ȫ������Ӧʽ���ģ�ͣ�֧�� Reactive Streams �淶������������ Netty��Undertow �Լ� Servlet 3.1+ ���������С�

WebFlux �ĺ��Ŀ������� DispatcherHandler���������� Spring MVC �е� DispatcherServlet����������ַ�����Ӧ�Ĵ�������DispatcherHandler ͨ������ Spring �����е� HandlerMapping��HandlerAdapter �� HandlerResultHandler ����������

�� WebFlux �У�Flux �� Mono �� Reactor ���е�������������ֱ����ڱ�ʾ���� 0 �� N ��Ԫ�غ� 0 �� 1 ��Ԫ�ص��첽���С�Flux �������ڱ�ʾһ�����������ӦʽԪ�ص������� Mono ���ڱ�ʾ����Ԫ�ص���Ӧʽ����

Spring WebFlux ֧�ֶ��ֱ��ģʽ����������ע��Ŀ������ͺ���ʽ�˵㡣�����߿���ʹ�� @RestController ע����������Ӧʽ����������ʹ�� @GetMapping��@PostMapping ��ע�������� HTTP ����ͬʱ��WebFlux Ҳ֧��ʹ�� WebClient ��Ϊ�������� HTTP �ͻ������������������ͨ�š�

WebFlux �IJ���ģ���봫ͳ�� Spring MVC ��������ͬ�����������������߳������������IJ��������������������������ԡ��������� Netty ��������ʱ��WebFlux ʹ���¼�ѭ���߳����������󣬱����˴�ͳ Servlet ������ÿ��������Ҫһ���̵߳�ģ�͡�

Spring WebFlux �����ó�����Ҫ�� IO �ܼ��͵�Ӧ�ã�����΢�������أ��������������������η���ת����������
��Ȼ����������е� Spring MVC Ӧ���ܹ������������󣬲�����Ŀ��ʹ����������� Servlet �߳�ģ�͵Ŀ⣬��ô����û�б�ҪǨ�Ƶ� WebFlux��

Դ����棬Spring WebFlux �������������漰�������������� Netty �������ij�ʼ��������Ľ��ա�DispatcherHandler ������ַ����Լ����յ�����������Ӧ���� Netty �������У��������漰�� ChannelHandler��ConnectionObserver���Լ� HttpHandler �ȶ���������Щ���Эͬ������ʵ���� WebFlux �ķ���������Ӧʽ���ԡ�

Spring WebFlux �������������

Spring WebFlux �������������������ǹ�ͬ��������������Ӧʽ Web Ӧ�ÿ�ܡ�������һЩ��Ҫ�ĺ��������

  1. DispatcherHandler������ WebFlux ������������������� Spring MVC �е� DispatcherServlet���������ֺ͵��� HTTP ����������handlers��������������ӳ�䡢���úͽ��������

  2. HandlerMapping������ӿ����ڽ�����ӳ�䵽��Ӧ�Ĵ�������handler��������Ӧ�ó����������б���⵽��������ȷ������Ӧ�����ĸ�������������

  3. HandlerAdapter������ӿڰ��� DispatcherHandler �����κ����͵Ĵ�������������Ҫ���ľ���ĵ��÷�ʽ����Ϊ��ͬ�Ĵ������ṩ�˵��ò��ԡ�

  4. HandlerResultHandler������ӿڴ������������ú�Ľ�������������յ���Ӧ�������𽫴������Ľ��ת��Ϊ�ͻ��˿��Խ��յĸ�ʽ��

  5. WebFilter��WebFilter �ӿڶ�����һ�����������Щ���������Զ��������Ӧ����Ԥ�����ͺ�����

  6. ServerWebExchange��������װ�� HTTP �������Ӧ��������Ϣ����������ͷ�������塢URI�������ȡ�

  7. ServerHttpRequest �� ServerHttpResponse����������ֱ�������������յ� HTTP ����ͷ��͵� HTTP ��Ӧ��

  8. WebSession�����ڹ����ض��ͻ��˵ĻỰ��Ϣ��

  9. Reactive Streams��WebFlux ���� Reactive Streams �淶��ʹ�÷�������ѹ������������������

  10. Reactor �⣺��Ϊ Spring 5 �ķ�Ӧʽ��̻�����Reactor �ṩ�˷������ı��ģ�ͺ͹��ߣ����� Flux �� Mono �ȷ�Ӧʽ���͡�

  11. WebClient������ Spring 5 ������ķ�������֧����Ӧʽ���� HTTP �ͻ��ˣ������������������ͨ�š�

  12. Spring Data Reactive���ṩ����Ӧʽ���ݷ��ʵ�֧�֣����� Reactive Repositories��

  13. Spring Security Reactive���ṩ����Ӧʽ��ȫ���ʿ��Ƶ�֧�֡�

  14. HttpHandler����������ͼ���ķ�Ӧʽ HTTP ��������ͬ����Ϊ��ͬ����ʱ֮��Ĺ�ͬ������

  15. ContextPathCompositeHandler�������ڲ�ͬ��������·����ע����Ӧ�ó���

��Щ�����ͬ������Ϊ������Ա�ṩ��һ��ǿ����������Ӧʽ Web Ӧ�ÿ���ƽ̨��ͨ����Щ����������߿��Թ������ܹ���Ч�����������������Ӧ�ó������������Щ�����V �罫һһ��ϸ���ܺ���Դ���ʵ�ֹ��̣������ֵ��dz������⡣

1. DispatcherHandler

DispatcherHandler �� Spring WebFlux �ĺ���������������������� Spring MVC �е� DispatcherServlet�������𽫴���� HTTP ����ַ�����Ӧ�Ĵ�������handler���������������ӳ�䡢���úͽ�������������Ƕ� DispatcherHandler ���Դ��ʵ���߼��Ͳ������ϸ������

��ʼ������

  • ApplicationContextAware ʵ�֣�DispatcherHandler ʵ���� ApplicationContextAware �ӿڣ�����ζ�������Է��ʵ� Spring Ӧ���������е� Bean��

  • HandlerMapping��HandlerAdapter �� HandlerResultHandler �ij�ʼ����DispatcherHandler �ڳ�ʼ��ʱ����� Spring Ӧ�������������е� HandlerMapping��HandlerAdapter �� HandlerResultHandler ����ʼ�����ǡ�

    protected void initStrategies(ApplicationContext context) {
        // ... ʡ�Բ��ִ��� ...
        this.handlerMappings = ...;
        this.handlerAdapters = ...;
        this.resultHandlers = ...;
    }

����������

  • ��ȡ HandlerMappings��DispatcherHandler ��ͨ�� handlerMappings �������ܹ�������ǰ����� HandlerMapping��

  • ӳ������ Handler��ʹ���ҵ��� HandlerMapping ������ӳ�䵽����Ĵ�������������һ�� @Controller ��������һ�� RouterFunction����

  • ���� Handler��һ���ҵ���������DispatcherHandler ��ʹ���ʵ��� HandlerAdapter ����������

  • �����������������ִ�н���ᱻ HandlerResultHandler ������������Ӧ��

�������handle

DispatcherHandler �ĺ��ķ����� handle���������������������̣�

public Mono handle(ServerWebExchange exchange) {
    // ����Ƿ��ʼ���� handlerMappings
    if (this.handlerMappings == null) {
        return createNotFoundError();
    }
    // ʹ�� handlerMappings ������ handler
    return Flux.fromIterable(this.handlerMappings)
            .concatMap(mapping -> mapping.getHandler(exchange))
            .next() // ��ȡ��һ�� handler
            .switchIfEmpty(createNotFoundError()) // ���û���ҵ� handler�����ش���
            .flatMap(handler -> invokeHandler(exchange, handler)) // ���� handler
            .flatMap(result -> handleResult(exchange, result)); // �������
}

������

  • createNotFoundError�����û���ҵ����ʵĴ�������DispatcherHandler �ᴴ��һ����ʾ "Not Found" ����Ӧ��

���������Эͬ����

  • HandlerMapping���������� URL ӳ�䵽����Ĵ�������
  • HandlerAdapter��������þ���Ĵ�������Spring WebFlux ֧�ֶ������͵Ĵ�������HandlerAdapter ʹ�� DispatcherHandler ������ľ���ĵ���ϸ�ڡ�
  • HandlerResultHandler���������������ķ���ֵ��������ת��Ϊ HTTP ��Ӧ��

DispatcherHandler �����ʹ�����dz������Ժ����׵���չ�µ� HandlerMapping��HandlerAdapter �� HandlerResultHandler ��֧�ֲ�ͬ�Ĵ��������ͺͷ������͡�

���Ͼ��� DispatcherHandler �����Դ��ʵ���߼��Ͳ���ķ�����ͨ�����ַ�ʽ��Spring WebFlux �ܹ��Է������ķ�ʽ���� Web �������Ӧ�õ����ܺͿ������ԡ�

2. HandlerMapping

HandlerMapping �� Spring WebFlux �е�һ���ӿڣ��������˽�����ӳ�䵽��������handler�����߼���HandlerMapping ��ʵ���ฺ�������������͡�URL ģʽ����Ϣ��ȷ���ĸ�����Ĵ�����Ӧ�ô�����ǰ�����������Ƕ� HandlerMapping �����Դ��ʵ���߼��Ͳ������ϸ������

HandlerMapping �ӿڶ���

HandlerMapping �ӿڶ��������¹ؼ�������

public interface HandlerMapping {
    Mono getHandler(ServerWebExchange exchange);
    void afterPropertiesSet();
}

 
  • getHandler�����ݸ����� ServerWebExchange ���󣬷���һ�� Mono ���󣬸� Mono ���ʱ��������Ĵ�������
  • afterPropertiesSet�����������Զ�����֮����ã����� HandlerMapping ʵ�ֽ��г�ʼ����

��Ҫʵ����

Spring WebFlux �ṩ�˼��� HandlerMapping ��ʵ���࣬��Ҫ������

  • RequestMappingHandlerMapping����������ע���ӳ�䣬���� @RequestMapping��@GetMapping �ȡ�

  • RouterFunctionMapping���������� RouterFunction �ĺ���ʽ·�ɡ�

  • SimpleUrlHandlerMapping�������òµ¥µï¿½ URL �������ӳ�䡣

RequestMappingHandlerMapping Դ�����

RequestMappingHandlerMapping ����õ� HandlerMapping ʵ��֮һ������������һЩ�ؼ�ʵ���߼���

  • ע��ͽ������ڳ�ʼ��ʱ��RequestMappingHandlerMapping ��ɨ�����е� beans�����Ҵ��� @RequestMapping ע��ķ�������ע����Щ������Ϊ����Ĵ�������

  • ӳ�䴦����RequestMappingHandlerMapping ʹ�� Pattern �������洢��ƥ�� URL ģʽ��

  • getHandler ����ʵ�֣�

@Override
public Mono getHandler(ServerWebExchange exchange) {
    String lookupPath = getPath(exchange);
    return getHandlerInternal(exchange)
            .filter(h -> matchesRoute(lookupPath, h))
            .switchIfEmpty(Mono.defer(() -> getBestMatchingHandler(lookupPath, exchange)));
}

 
  • getPath���� ServerWebExchange ����ȡ����·����
  • getHandlerInternal������һ����������ע�ᴦ������ Mono��
  • filter �� matchesRoute����鴦�����Ƿ�������·��ƥ�䡣
  • getBestMatchingHandler�����û���ҵ���ȷƥ��Ĵ������������ҵ����ƥ��Ĵ�������

ӳ��ƥ���߼�

ӳ��ƥ���߼�ͨ���漰���²��裺

  • ·��ƥ�䣺��������·���Ƿ���ע��� URL ģʽƥ�䡣

  • ���󷽷�ƥ�䣺��� URL ģʽƥ�䣬��һ���������ķ�����GET��POST �ȣ��Ƿ��봦����֧�ֵķ���ƥ�䡣

  • ��������ƥ�䣺��������Ƿ��������������IJ�����

  • ͷ��Ϣƥ�䣺�������ͷ�Ƿ������ض���������

  • ���ѺͲ���ý������ƥ�䣺�������� Accept ͷ�� Content-Type �Ƿ��봦����֧�ֵ�ý������ƥ�䡣

������

RequestMappingHandlerMapping ��ʵ����һЩ�����Ż���ʩ�����绺��ƥ��� URL ģʽ���Լ����ظ���ģʽƥ�������

С��һ��

HandlerMapping ����� Spring WebFlux �����������еĹؼ����֣������𽫽��������ӳ�䵽��ȷ�Ĵ�������ͨ��ʹ�ò�ͬ�� HandlerMapping ʵ�֣�Spring WebFlux ֧����������ӳ����ԣ�����Ӧ��ͬ��Ӧ�ó�����

3. HandlerAdapter

HandlerAdapter �ӿ��� Spring WebFlux �а�����������Ҫ�Ľ�ɫ�����������ǽ� DispatcherHandler �ҵ��Ĵ�������handler�����䵽�����ִ���߼��ϡ�HandlerAdapter ʹ�� DispatcherHandler ������ľ���Ĵ��������ͣ�ֻ��Ҫͨ�� HandlerAdapter �����ô��������ɡ������Ƕ� HandlerAdapter �����Դ��ʵ���߼��Ͳ������ϸ������
HandlerAdapter �ӿڶ���

HandlerAdapter �ӿڶ��������¹ؼ�������

public interface HandlerAdapter {
    boolean supports(Object handler);
    Mono handle(ServerWebExchange exchange, Object handler, Object... args);
}
  • supports���������Ĵ������Ƿñ±»µï¿½Ç° HandlerAdapter ֧�֡�
  • handle�����ô�������������һ�� Mono ���󣬱�ʾ�첽�ĵ��ù��̡�

��Ҫʵ����

Spring WebFlux �ṩ�˼��� HandlerAdapter ��ʵ���࣬��Ҫ������

  • RequestMappingHandlerAdapter��֧�ֻ���ע��Ŀ���������������� @RequestMapping ע��ķ�����

  • HttpHandlerAdapter��֧�� HttpHandler �ӿڵĴ�������

  • ControllerEndpointHandlerAdapter��֧�� ControllerEndpoint �ӿڵĴ�������ͨ������ WebFlux ����ʽ��̡�

  • RouterFunctionHandlerAdapter��֧�� RouterFunction �ӿڣ����ں���ʽ·�ɡ�

RequestMappingHandlerAdapter Դ�����

RequestMappingHandlerAdapter ����õ� HandlerAdapter ʵ��֮һ������������һЩ�ؼ�ʵ���߼���

  • ֧���Լ�飺supports �����������Ĵ������Ƿ��� Controller ���� RequestMapping ע��ķ�����
@Override
public boolean supports(Object handler) {
    return (handler instanceof HandlerFunction) ||
           (handler instanceof Controller) ||
           AnnotationUtils.findAnnotation(handler.getClass(), RequestMapping.class) != null;
}

���ô�������handle �������ô�����������������ֵ��

    @Override
    public Mono handle(ServerWebExchange exchange, Object handler) {
        // ���þ���Ĵ�����
        return ((HandlerFunction) handler).handle(exchange);
    }

���ô��������߼�

���ô��������߼�ͨ���漰���²��裺

  • �������������������еIJ�����������ת��Ϊ����������

  • ���÷��������ô������ķ���������������IJ������ݸ�������

  • ��������ֵ�����������ķ���ֵ������ת��Ϊ��Ӧ��

  • �첽������������������ص��� Mono �� Flux��HandlerAdapter ��Ҫ������Щ�첽�����

������

HandlerAdapter �����������ù����е��쳣�����쳣ת��Ϊ���ʵ���Ӧ��

С��һ��

HandlerAdapter ����� Spring WebFlux �����������еĹؼ����֣��������� DispatcherHandler �;���Ĵ�����ʵ�֡�ͨ��ʹ�ò�ͬ�� HandlerAdapter ʵ�֣�Spring WebFlux ֧���˶������͵Ĵ���������������ע��Ŀ�����������ʽ·���Լ� HttpHandler �ӿڵ�ʵ�֡������������˿�ܵ�����ԺͿ���չ�ԡ�

4. HandlerResultHandler

HandlerResultHandler ����� Spring WebFlux �и������� HandlerAdapter ���ô������󷵻صĽ����������Щ���ת��Ϊ�ͻ��˿��Խ��յ� HTTP ��Ӧ�������Ƕ� HandlerResultHandler �����Դ��ʵ���߼��Ͳ������ϸ������

HandlerResultHandler �ӿڶ���

HandlerResultHandler �ӿڶ��������¹ؼ�������

public interface HandlerResultHandler {
    boolean supports(HandlerResult result);
    Mono handleResult(ServerWebExchange exchange, HandlerResult result);
}
  • supports���������� HandlerResult �Ƿñ±»µï¿½Ç° HandlerResultHandler ֧�֡�
  • handleResult������ HandlerResult��������Ӧ������һ�� Mono ���󣬱�ʾ�첽�Ĵ������̡�

��Ҫʵ����

Spring WebFlux �ṩ�˼��� HandlerResultHandler ��ʵ���࣬��Ҫ������

  • ServerResponseResultHandler������ ServerResponse ���͵ķ���ֵ��
  • ResponseEntityResultHandler������ ResponseEntity ���͵ķ���ֵ��
  • ModelAndViewResultHandler������ ModelAndView ���͵ķ���ֵ��ͨ��������ͼ��Ⱦ��

ServerResponseResultHandler Դ�����

ServerResponseResultHandler �Ǵ��� ServerResponse ���ͽ���� HandlerResultHandler ʵ�֣�

  • ֧���Լ�飺supports ������� HandlerResult �Ƿ���� ServerResponse ����
@Override
public boolean supports(HandlerResult result) {
    return result.getReturnValue() instanceof ServerResponse;
}
  • ���������handleResult �������� ServerResponse ���󣬲�������Ӧ��
    @Override
    public Mono handleResult(ServerWebExchange exchange, HandlerResult result) {
        ServerResponse response = (ServerResponse) result.getReturnValue();
        return response.writeTo(exchange, result.isCommitted());
    }

����������߼�

����������߼�ͨ���漰���²��裺

  • ��ȡ����ֵ���� HandlerResult �л�ȡ�������ķ���ֵ��

  • ������ͣ����ݷ���ֵ�����ͣ�ѡ����ʵĴ����߼���

  • ������Ӧ��������ֵת��Ϊ HTTP ��Ӧ�����磬ServerResponse �Ѿ���������Ӧ��״̬�롢ͷ��Ϣ���塣

  • �첽�������������ֵ���첽�ģ��� Mono �� Flux��������Ҫ������Щ�첽�����

  • д����Ӧ�������ɵ���Ӧд�뵽 ServerWebExchange �С�

������

HandlerResultHandler ��������������������е��쳣�����쳣ת��Ϊ���ʵ���Ӧ��

С��һ��

HandlerResultHandler ����� Spring WebFlux �����������еĹؼ����֣������𽫴������ķ���ֵת��Ϊ HTTP ��Ӧ��ͨ��ʹ�ò�ͬ�� HandlerResultHandler ʵ�֣�Spring WebFlux ֧���˶��ַ���ֵ���ͣ����� ServerResponse��ResponseEntity �� ModelAndView�������������˿�ܵ�����ԺͿ���չ�ԣ������������Բ�ͬ�ķ�ʽ������Ӧ�����

HandlerResultHandler ��ʵ��ͨ����Ҫ������Ӧ���첽���ԣ�ȷ����ʹ���첽���������Ҳ����ȷ�����ɺͷ�����Ӧ�����⣬������Ҫ�� ServerWebExchange ����Э�����Ա���ʺͲ����������Ӧ����������Ϣ��

5. WebFilter

WebFilter �ӿ��� Spring WebFlux ���������غʹ��� Web �������Ӧ������������������������󵽴����Ĵ�����֮ǰ��֮�󣬶��������Ӧ���ж���Ĵ�����������־��¼����ȫ�Լ�顢�������ȡ������Ƕ� WebFilter �����Դ��ʵ���߼��Ͳ������ϸ������

WebFilter �ӿڶ���

WebFilter �ӿڶ��������¹ؼ�������

public interface WebFilter {
    Mono filter(ServerWebExchange exchange, WebFilterChain chain);
}
  • filter���Ը����� ServerWebExchange ������д�������ͨ�� WebFilterChain �������е���һ�� WebFilter �����յĴ�������

��������

�� Spring WebFlux �У�WebFilter ͨ���ᱻ��֯��һ������������ÿ�� WebFilter �����Ծ����Ǽ������������ǽ����󴫵ݸ����е���һ�� WebFilter��������ʽ����ģʽʹ�ù�������ִ��˳��dz���Ҫ��

��Ҫʵ����

Spring WebFlux �ṩ��һЩ���õ� WebFilter ʵ���࣬���磺

  • ServerHttpSecurity�����ڰ�ȫ�Լ�顣
  • CorsFilter�����ڴ�����Դ��Դ������CORS����
  • WebFilterChain���������������������ģ������������е���һ�� WebFilter��

������������

��������ͨ����Ӧ�ó���������й���������ʹ�� WebFilter �ӿڵ�ʵ���ࣺ

@Configuration
public class WebFluxConfig {
    @Bean
    public WebFilter myCustomFilter() {
        return (exchange, chain) -> {
            // ��������Զ��������Ԥ����
            return chain.filter(exchange).subscriberContext(ctx -> ctx.put("customKey", "customValue"));
        };
    }
}

WebFilter ��ʵ���߼�

ʵ�� WebFilter �ӿڵ� filter ����ͨ���漰���²��裺

  • Ԥ�������ڵ��� chain.filter(exchange) ֮ǰ���� ServerWebExchange �����κα�Ҫ��Ԥ�����������޸�����ͷ����ѯ�����ȡ�

  • ��������ʹ�� WebFilterChain �� filter ���������󴫵ݸ����е���һ�� WebFilter����ͨ���᷵��һ�� Mono ����ʾ�첽�Ĺ��˹��̡�

  • �������� chain.filter(exchange) ��ɺ󣬶� ServerWebExchange �����κα�Ҫ�ĺ����������޸���Ӧͷ����Ӧ��ȡ�

  • �������������ڹ��˹����п��ܷ������쳣�����������׳��µĴ��󡢷����ض�����Ӧ�������������

�첽����

���� filter �������ص��� Mono ��WebFilter ��ʵ����Ҫ�����첽����������ζ���ڹ��˹����У����Է����첽����Ӧ��������������������Ĵ�����

С��һ��

WebFilter ����� Spring WebFlux ���������غʹ��� Web �������Ӧ��ǿ�󹤾ߡ�ͨ��ʵ�� WebFilter �ӿڲ��������������������߿������ض��������Ӧ����Ԥ�����ͺ������Լ�ʵ�ָ��ֺ��й�ע�㣬�簲ȫ�ԡ���־��¼��CORS �����ȡ�������������Ӧ�ó����ģ���ԺͿ�ά���ԣ�ͬʱ�����˷��������첽�����ԡ�

6. ServerWebExchange

ServerWebExchange �� Spring WebFlux �е�һ���������������װ�� HTTP �������Ӧ����������Ϣ��Ϊ Web ��������Ӧ�ó���֮���ṩ��һ�������Ľӿڡ������Ƕ� ServerWebExchange �����Դ��ʵ���߼��Ͳ������ϸ������

ServerWebExchange �ӿڶ���

ServerWebExchange �ӿڶ����˶� HTTP �������Ӧ�ķ��ʺͲ�����

public interface ServerWebExchange {
    ServerHttpRequest getRequest();
    ServerHttpResponse getResponse();
    void beforeCommit();
    boolean isCommitted();
    void setCommitted(boolean committed);
    Context getContext();
}
  • getRequest()�����ص�ǰ�� ServerHttpRequest ���󣬰����������ϸ��Ϣ��
  • getResponse()�����ص�ǰ�� ServerHttpResponse �������ڹ�����Ӧ��
  • beforeCommit()������Ӧ�ύ֮ǰ���ã���������һЩ������׼��������
  • isCommitted()�������Ӧ�Ƿ��Ѿ��ύ��
  • setCommitted(boolean committed)��������Ӧ�Ƿ��ύ��״̬��
  • getContext()�������뵱ǰ���������� Context�����ڴ洢�ʹ��ݸ�����Ϣ��

��������

ServerWebExchange ͨ���������º������ԣ�

  • request��ServerHttpRequest ���󣬷�װ�� HTTP �������ϸ��Ϣ����ͷ��Ϣ��URI�������ȡ�
  • response��ServerHttpResponse �������ڹ���ͷ��� HTTP ��Ӧ��
  • principal�����ܰ�����ǰ�������֤���壨Principal����
  • session�����ܰ�����ǰ����ĻỰ��Ϣ��
  • attributes��һ�� Map�����ڴ洢��������ص����ԡ�

�������Ӧ�Ĵ���

ServerWebExchange ���������Ӧ�Ĵ����а����ź��Ľ�ɫ��

  • �����ȡ��ͨ�� getRequest() ������ȡ������󣬷�������ĸ�����Ϣ��

  • ��Ӧ���죺ͨ�� getResponse() ������ȡ��Ӧ���󣬹�����Ӧ��״̬�롢ͷ��Ϣ����Ӧ�塣

  • �����Ĺ�����ʹ�� Context ����洢�ʹ����������Ӧ�����еĸ�����Ϣ��

  • �ύ������ͨ�� beforeCommit()��isCommitted() �� setCommitted() ����������Ӧ���ύ״̬��

  • ������������ WebFilter ��ʵ���У�ServerWebExchange �����ڹ��������д��ݣ�ÿ�������������Է��ʺ��޸��������Ӧ��

�첽����

���� WebFlux ����Ӧʽ�ģ�ServerWebExchange ֧���첽������

  • ��Ӧ����ͨ���������ķ�ʽд�룬����ʹ�� ServerHttpResponse ���첽������
  • �������Ӧ�Ĵ��������ڲ�ͬ���̻߳��¼�ѭ���н��С�

С��һ��

ServerWebExchange �� Spring WebFlux �д��� HTTP �������Ӧ�ĺ�����������ṩ��һ��ͳһ�Ľӿ������ʺͲ����������Ӧ���ݣ�ͬʱ֧���첽�������Ĵ�����ʽ��ͨ�� ServerWebExchange�������߿����� Web ��������Ӧ�ó���֮����и�Ч�����ݽ�����״̬������ʵ�ָ����ܵ���Ӧʽ Web Ӧ�á�

ServerWebExchange ��ʵ��ͨ����Ҫ������Ӧʽ�ı��ģ�ͣ�ȷ���ڴ�������͹�����Ӧʱ���������¼�ѭ�����Ӷ�������� WebFlux ���������ơ����⣬�����ṩ�˷ḻ�������Ĺ������ܣ�ʹ���ڸ��ӵ������������У����Է���ش洢�ʹ��ݸ�����Ϣ��

7. ServerHttpRequest��ServerHttpResponse

ServerHttpRequest �� ServerHttpResponse �� Spring WebFlux �е��������Ľӿڣ����Ƿֱ��ʾ���������յ� HTTP ����ͷ��͵� HTTP ��Ӧ�������Ƕ������������Դ��ʵ���߼��Ͳ������ϸ������

ServerHttpRequest �ӿڶ���

ServerHttpRequest �ӿڶ����˶� HTTP ����ķ��ʣ�

public interface ServerHttpRequest {
    URI getURI();
    HttpMethod getMethod();
    String getHeader(String headerName);
    MultiValueMap getHeaders();
    DataBufferFactory bufferFactory();
    // ʡ����������...
}
  • getURI()����������� URI��
  • getMethod()������ HTTP �������� GET��POST �ȣ���
  • getHeader(String headerName)���������ƻ�ȡ����ͷ��ֵ��
  • getHeaders()�����ذ�����������ͷ�� MultiValueMap��
  • bufferFactory()���������ڴ������ݻ�������DataBuffer��������

ServerHttpResponse �ӿڶ���

ServerHttpResponse �ӿڶ����˶� HTTP ��Ӧ�Ĺ���ͷ��ͣ�

public interface ServerHttpResponse {
    HttpStatusSeriesStatus.Series getStatusSeries();
    void setStatusCode(HttpStatus statusCode);
    String getHeader(String headerName);
    MultiValueMap getHeaders();
    void setComplete();
    DataBufferFactory bufferFactory();
    Mono writeWith(Publisher body);
    // ʡ����������...
}
  • getStatusSeries()��������Ӧ��״̬��ϵ�У��� 2xx��3xx �ȣ���
  • setStatusCode(HttpStatus statusCode)������ HTTP ״̬�롣
  • getHeader(String headerName)���������ƻ�ȡ��Ӧͷ��ֵ��
  • getHeaders()�����ذ���������Ӧͷ�� MultiValueMap��
  • setComplete()�������ӦΪ��ɡ�
  • writeWith(Publisher body) ��������Ӧ�塣

�������Ӧ�Ĵ���

ServerHttpRequest �� ServerHttpResponse �ڴ��� HTTP �������Ӧ�а����ź��Ľ�ɫ��

  • ������Ϣ��ȡ��ͨ�� ServerHttpRequest �ķ�����ȡ����� URI��������ͷ��Ϣ�ȡ�

  • ��Ӧ���죺ʹ�� ServerHttpResponse �ķ�������״̬�롢ͷ��Ϣ����������Ӧ�塣

  • ���ݻ�������ͨ�� bufferFactory() ������ȡ DataBufferFactory�����ڴ����͹������ݻ�������

  • �첽���ͣ�ServerHttpResponse �� writeWith(Publisher body) ����֧���첽������Ӧ�塣

  • ��ʽ������֧������ʽ�ķ�ʽ��ȡ�������д����Ӧ�塣

�첽������

���� WebFlux �ǻ�����Ӧʽ���ģ�͵ģ�ServerHttpRequest �� ServerHttpResponse ֧���첽�������IJ�����

  • ���������Ӧ�����ͨ�� Publisher ��ʽ�첽��ȡ�ͷ��͡�
  • ��Ӧ�ķ��Ͳ��������¼�ѭ����

С��һ��

ServerHttpRequest �� ServerHttpResponse �� Spring WebFlux �д��� HTTP �������Ӧ�Ľӿڡ������ṩ�˷ḻ�ķ���������������Ϣ��������Ӧ����֧���첽�������IJ�����ͨ���������ӿڣ������߿��Թ��������ܡ���Ӧʽ�� Web Ӧ�ã���������ִ�Ӳ���������ܹ������ơ�

��ʵ��Ӧ���У�������ͨ������Ҫֱ��ʵ����Щ�ӿڣ�����ͨ������ṩ��ʵ�����������������Ӧ����Щʵ����ͨ�������ض�������ʱ�������� Netty�����ɣ����ṩ��Ч�� I/O ������

8. WebSession

WebSession ����� Spring WebFlux �����ڱ�ʾ�͹��� Web �Ự��session�������ṩ��һ�ֻ������洢�ͼ������ض��û��Ự��ص����ݡ������Ƕ� WebSession �����Դ��ʵ���߼��Ͳ������ϸ������

WebSession �ӿڶ���

WebSession �ӿڶ����� Web �Ự�Ļ���������

public interface WebSession {
    String getId();
    Mono save();
    void invalidate();
    Map getAttributes();
     T getAttribute(String name);
     void setAttribute(String name, T value);
    default  Mono getAttributeOrDefault(String name, Supplier defaultValue);
    // ʡ����������...
}
  • getId()����ȡ�Ự��Ψһ��ʶ����
  • save()������Ự�ĸ��ġ�
  • invalidate()��ʹ�Ự��Ч���൱�ڻỰ���ڡ�
  • getAttributes()����ȡ�Ự���������ԡ�
  • getAttribute(String name)���������ƻ�ȡ�Ự���ԡ�
  • setAttribute(String name, T value)�����ûỰ���ԡ�

WebSession ��ʵ���߼�

  • �Ự������WebSession �����������������д�����ͨ���� ServerWebExchange ������

  • ���Թ������Ự���Դ洢�� getAttributes() ���ص� Map �У������洢�ͼ����û��ض�����Ϣ��

  • �첽���棺save() �����첽����Ự���ģ�������漰������д��ײ�洢��

  • �ỰʧЧ��invalidate() ��������ʹ�Ự��Ч��ȷ���Ự���ݲ��ٿ��á�

  • �Ự ID ������ÿ�� WebSession ʵ������һ��Ψһ�� id�����ڱ�ʶ�ض����û��Ự��

  • Ĭ��ֵ��ȡ��getAttributeOrDefault() �����ṩ��һ�ֱ�ݵķ�ʽ����ȡ����ֵ��������Բ����ڣ��ò·µ»ï¿½Ä¬ï¿½ï¿½Öµï¿½ï¿½

�Ự�Ĵ洢�ͼ���

WebSession ��ʵ��ͨ����Ҫ�������·��棺

  • �洢���ƣ��Ự���ݿ��Դ洢�ڲ�ͬ�Ľ����У������ڴ桢���ݿ��ֲ�ʽ���档
  • �����������ڶ��̻߳��첽�����У���Ҫȷ���Ự���ݵ�һ���ԡ�
  • �Ự��ʱ��ʵ�ֻỰ��ʱ�߼����Զ�ʹ���ڵĻỰ��Ч��

�Ự�Ĵ����Ͱ�

�������������У�WebSession ���Ա������Ͱ󶨵� ServerWebExchange��

ServerWebExchange exchange = ...;
Mono sessionMono = exchange.getSession();
sessionMono.flatMap(session -> {
    // ʹ�ûỰ
    return session.save();
});

С��һ��

WebSession ����� Spring WebFlux �����ڹ��� Web �Ự�Ľӿڡ����ṩ��һ�����ķ�ʽ���洢�ͼ������û��Ự��ص����ݣ�ͬʱ֧���첽�����Ͷ��ִ洢ѡ�ͨ�� WebSession�������߿�������ʵ���û��Ự���ٺ͹������������и��Ի��û������ Web Ӧ�á�

��ʵ��Ӧ���У������߿��Ը�����Ҫѡ��ͬ�ĻỰ�洢ʵ�֣�����ʹ�� Spring Session ��Ŀ�ṩ�Ķ��ִ洢������������� Redis��Hazelcast��JDBC �ȡ���Щʵ��ͨ���ᴦ���Ự�Ĵ��������桢ʧЧ���߼������� WebSession �ӿڽ��м��ɡ�

9. Reactive Streams

Reactive Streams ��һ���淶�����������첽�������Ľӿں���Ϊ���Ա��ڲ�ͬ�Ŀ�Ϳ��֮��ʵ�ֻ������ԡ�Spring WebFlux ��Ϊ��Ӧʽ��̵�һ���֣���ѭ Reactive Streams �淶�������Ƕ� Reactive Streams �����Դ��ʵ���߼��Ͳ������ϸ������

Reactive Streams ���Ľӿ�

Reactive Streams �淶���������¼������Ľӿڣ�

  1. Publisher �������ߣ���ʾ���Բ������ݵ�Դͷ��
  2. Subscriber �������ߣ���ʾ���ղ��������ݵ������ߡ�
  3. Subscription �����Ĺ�ϵ�����ڹ������ݵ�����ͷ��͡�
  4. Processor ������������ Publisher �� Subscriber �Ľ���塣

Publisher �ӿ�

Publisher �ӿ��� Reactive Streams �ĺ��ģ�����������ν��������͸� Subscriber��

public interface Publisher {
void subscribe( Subscriber s);
}

subscribe(`Subscriber s`)������ Subscriber ���� Publisher��

Subscriber �ӿ�

Subscriber �ӿڶ�������δ����� Publisher ���յ������ݣ�

public interface Subscriber {
    void onSubscribe(Subscription s);
    void onNext(T t);
    void onError(Throwable t);
    void onComplete();
}
  • onSubscribe(Subscription s)���� Subscriber ���� Publisher �󱻵��ã�Subscription ���ڿ�����������
  • onNext(T t)�����յ�������ʱ���á�
  • onError(Throwable t)����������ʱ���á�
  • onComplete()������������ʱ���á�

Subscription �ӿ�

Subscription �ӿ����ڹ��� Subscriber �� Publisher ֮�����������

public interface Subscription {
    void request(long n);
    void cancel();
}
  • request(long n)������ Publisher ����ָ�������������
  • cancel()��ȡ�����ģ�ֹͣ�������ݡ�

Processor �ӿ�

Processor �� Publisher �� Subscriber �Ľ���壬���Խ������ݲ������µ���������

public interface Processor extends Subscriber, Publisher {
    // �̳��� Subscriber �� Publisher �ķ���
}

Դ��ʵ���߼�

  • ������������ʹ�� Publisher ������������
  • ���Ļ��ƣ�Subscriber ͨ������ Publisher �� subscribe ����������������
  • ��������Subscriber ʹ�� Subscription �� request �����������ݵĽ������ʡ�
  • �������ͣ�Publisher ���� Subscriber ��������������� Subscriber��
  • �������ɴ�����Publisher �ڷ������������������ʱ���ֱ���� Subscriber �� onError �� onComplete ������
  • ȡ�����ģ�Subscriber ����ͨ������ Subscription �� cancel ����ȡ�����ġ�

����

  • ��ʼ�������� Publisher �� Subscriber ����
  • ���ģ�Subscriber ���� Publisher �� subscribe ������
  • �������ģ�Publisher ���� Subscriber �� onSubscribe ���������� Subscription ����
  • �������ݣ�Subscriber ʹ�� Subscription �������ݡ�
  • �������ݣ�Publisher �������������ݸ� Subscriber��
  • ��ɻ����Publisher �����ݷ�����Ϻ���� onComplete�����ڷ�������ʱ���� onError��

С��һ��

Reactive Streams �淶�ṩ��һ���첽�������������ݴ���ģ�ͣ�Spring WebFlux ͨ��ʵ����Щ�ӿڣ�֧����Ӧʽ��̡�����ģ������ϵͳ����Ч�ش���������������������ܺͿ������ԡ������߿������� Reactive Streams �淶�ṩ�Ľӿںͻ��ƣ�������Ч�����Ե���ӦʽӦ�ó���

10. Reactor ��

Reactor ��һ������ Reactive Streams �淶�Ŀ⣬���ڹ����첽������������ӦʽӦ�ó������� Spring WebFlux �ķ�Ӧʽ��̻����������Ƕ� Reactor �������Դ��ʵ���߼��Ͳ������ϸ������

Reactor �������

Reactor �ṩ�����º��������

  • Flux������һ������ 0 �� N ��Ԫ�ص���Ӧʽ���С�
  • Mono������һ������ 0 �� 1 ��Ԫ�ص���Ӧʽ���С�
  • Scheduler�����ڿ��Ʋ�����ִ���첽�����ĵ�������

Flux �� Mono ��ʵ���߼�

  • ������������ͨ����̬�������� Flux.just(), Mono.just()�����캯������ Flux �� Mono ʵ����

  • ��������Reactor �ṩ�˷ḻ�IJ����������������������� map��flatMap��filter �ȡ�

  • ���Ļ��ƣ�ͨ�� subscribe() �������������������ṩ Subscriber ���������ݡ�

  • ��������ʹ�� request() �����������ݵ�����������

  • �������ͣ�����ͨ�� onNext() �������͸������ߡ�

  • �������ɴ�����ͨ�� onError() �� onComplete() ���������������Ĵ��������¼���

Scheduler ��ʵ���߼�

  • ���������������� Scheduler ʵ��������ʹ�� Schedulers.parallel() �������������

  • ������ȣ�ʹ�� schedule() �����������ñ£¬·ï¿½ï¿½ï¿½ Mono �� Flux��

  • �������ƣ�Scheduler ���Կ�������IJ���ִ�У��������Ʋ���������

  • �첽ִ�У������ڷ��������̳߳����첽ִ�С�

Դ��ʵ�ֲ���

  • ��������Դ������ Flux �� Mono ʵ����Ϊ����Դ��

  • Ӧ�ò�������ʹ�ò�����������������ת�������˻���ϡ�

  • ��������ʹ�� onErrorResume() �� doOnError() �Ȳ�������������

  • ��ѹ������ʹ�� onBackpressureBuffer() �� onBackpressureDrop() �Ȳ�����������ѹ��

  • ���ĺ����ѣ����� subscribe() �������������������ṩ Subscriber ���������ݡ�

  • ��������ʹ�� Scheduler �����첽����

  • ��Դ������ʹ�� dispose() �����ڲ�����Ҫʱ�ͷ���Դ��

С��һ��

Reactor ��ͨ�� Flux��Mono �� Scheduler ��������ṩ��һ��ǿ��ķ�ʽ��������ӦʽӦ�ó�������ѭ Reactive Streams �淶��֧���첽��������������������Reactor �IJ������ḻ����������ʵ�ָ��ӵ����ݴ����߼���ͬʱ�������ṩ�����IJ������ƺ͵��Ȼ��ƣ�����Ӧ��ͬ��Ӧ�ó�����

Reactor �������ѧ���ṩ����ʽ�����ݴ����������ÿ������ܹ���һ��ֱ�ۺ����ķ�ʽ������Ӧʽϵͳ��ͨ�� Reactor�������߿��Գ�������ִ�Ӳ���Ķ�����ԣ����Ӧ�ó�������ܺͿ������ԡ�

11. WebClient

WebClient �� Spring WebFlux �����ڷ��� HTTP ����ķ�������Ӧʽ�ͻ��ˡ���������������ʽ�ķ�ʽ�������󲢴�����Ӧ�������Ƕ� WebClient �����Դ��ʵ���߼��Ͳ������ϸ������

WebClient �ӿڶ���

WebClient �ṩ�˷�������ķ�����

public interface WebClient {
    default URI uri() {
        return URI.create(this.baseUrl);
    }

     Mono getForObject(String url, Class responseType, Object... uriVariables);
     Flux getForFlux(String url, Class elementType, Object... uriVariables);
    // ���� HTTP ���������أ����� postForObject, putForObject ��
}
  • uri()�����ػ��� URI��
  • getForObject(String url, ...)������ GET ����������ȡ������Ӧ��
  • getForFlux(String url, ...)������ GET ����������ȡԪ������Ӧ��

WebClient.Builder ������

WebClient ��ʵ����ͨ�� WebClient.Builder �����ģ�

public final class WebClient.Builder {
    private final String baseUrl;

    public Builder(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public WebClient build() {
        return new ExchangeStrategiesDefaultWebClient(this);
    }

    // ��������ѡ��������� ExchangeStrategies, ClientHttpRequestFactory ��
}
  • baseUrl������ͻ��˵Ļ��� URL��

���󹹽��ͷ���

  • ���� WebClient ʵ����ʹ�� WebClient.Builder ���������� WebClient ʵ����

  • ��������ʹ�� WebClient �ķ�������������ͷ����ѯ������������ȡ�

  • �������󣺵��� HTTP ������Ӧ�ķ������� getForObject��postForObject������������

  • ������Ӧ����Ӧ�� Mono �� Flux ����ʽ���أ����Խ�һ��������

Դ��ʵ�ֲ���

  • ���úʹ�����ͨ�� WebClient.Builder ���û��� URL ������ѡ�Ȼ�󴴽� WebClient ʵ����
WebClient webClient = WebClient.builder().baseUrl("http://example.com").build();

��������ʹ�� WebClient �ķ�����ʽ��������

    Mono personMono = webClient.get()
        .uri("/person/{id}", id)
        .retrieve()
        .bodyToMono(Person.class);
  • �������󲢻�ȡ��Ӧ������ retrieve() ������ָ����Ӧ��ת���ķ�ʽ��

  • ��Ӧ��ת����ʹ�� bodyToMono �� bodyToFlux �ȷ�������Ӧ��ת��Ϊָ�����͡�

  • ��������ʹ�� onErrorResume �� onErrorMap �Ȳ������������ܷ����Ĵ���

  • ���ĺ����ѣ�������Ӧ�� Mono �� Flux ���������ݡ�

�������첽����

WebClient ֧�ֲ������첽�����������Է������ķ�ʽ����������

  • ʹ�� Flux ���Դ��������Ӧ��
  • ����ʹ�� Scheduler �����Ʋ�������

С��һ��

WebClient �� Spring WebFlux ��һ��ǿ����������������ڹ�������������Ӧʽ HTTP �ͻ��ˡ�������������ʽ�ķ�ʽ�������󣬲�ͨ�� Reactive Streams �淶֧���첽���ݴ�����WebClient �����ʹ�����dz��ʺ�����ӦʽӦ�ó�����ʹ�ã����Գ�������ִ��첽��̵����ƣ����Ӧ�ó�������ܺͿ������ԡ�

�����߿������ɵ�ʹ�� WebClient ���ⲿ�������ͨ�ţ���ȡ���ݣ�������Ӧʽ�ķ�ʽ������Щ���ݡ�ͨ�� WebClient��Spring WebFlux Ӧ�ó�������޷�ؼ��ɵ��������Ӧʽϵͳ�С�

12. Spring Data Reactive

Spring Data Reactive �� Spring Data ��Ŀ��һ���֣����ṩ��һ�����ڷ�����Ӧʽ���ݴ洢�ij���������������ʽ����Ӧʽ�ķ�ʽ�������ݷ��ʺͲ�����֧���� MongoDB��Redis��R2DBC��Reactive Relational Database Connectivity������Ӧʽ���ݿ⡣�����Ƕ� Spring Data Reactive �����Դ��ʵ���߼��Ͳ������ϸ������

Spring Data Reactive ���ĸ���

  • Reactive Repository����չ�� Reactive Streams �淶���ṩ���첽�� CRUD ������
  • ReactiveCrudRepository�������ӿڣ��ṩ������ CRUD ������
  • ReactiveMongoRepository��ReactiveRedisRepository �ȣ��ض����ݿ��ʵ�֡�

Reactive Repository �ӿڶ���

public interface ReactiveCrudRepository extends ReactiveRepository {
    Mono save(T entity);
    Flux findAll();
    Mono findById(ID id);
    Mono deleteById(ID id);
    // ��������...
}
  • save(T entity)������ʵ�塣
  • findAll()���������м�¼��
  • findById(ID id)��ͨ�� ID ���Ҽ�¼��
  • deleteById(ID id)��ͨ�� ID ɾ����¼��

��Ӧʽ���ݷ��ʲ���

  • ����ʵ���ࣺ����һ��ʵ���࣬ʹ�� JPA ע������ݿ��ض���ע�����ֶΡ�

  • ����ֿ�ӿڣ�����һ���̳��� ReactiveCrudRepository ���ض����ݿ�� Repository �ӿڡ�

    public interface MyEntityRepository extends ReactiveCrudRepository {
        // ���������Զ����ѯ����
    }
  • ��������Դ��������Ӧʽ����Դ�Ϳͻ��ˣ��������� MongoDB �� ReactiveMongoDatabase��

  • ʹ�òֿ⣺�ڷ����ע�벢ʹ�òֿ�ӿڽ������ݲ�����

  • ������ѯ��ʹ�òֿ�ӿ��ṩ�ķ������Զ����ѯ����������ѯ��

  • �첽������������ѯ�����ʹ�� Mono �� Flux ���첽���ԡ�

Դ��ʵ���߼�

  • ʵ��Ͳֿⶨ�壺��������ʵ��Ͳֿ�ӿڡ�

  • Spring Ӧ�������ģ�Spring Ӧ��������ɨ��ֿ�ӿڲ���������ʵ�֡�

  • ִ�в�ѯ�������òֿ�ӿڵķ���ʱ����������������ת��Ϊ���ݿ������

  • �����װ����ѯ�����װ�� Mono �� Flux �з��ء�

  • ���������������ܷ������쳣��������ת��Ϊ���ʵ���Ӧ��

  • ��Ӧʽ�����ƣ�ʹ�� Reactive Streams �淶������������

��Ӧʽ���ݿ����ʾ��

@Service
public class MyEntityService {
    private final MyEntityRepository repository;

    @Autowired
    public MyEntityService(MyEntityRepository repository) {
        this.repository = repository;
    }

    public Mono addMyEntity(MyEntity entity) {
        return repository.save(entity);
    }

    public Flux getAllMyEntities() {
        return repository.findAll();
    }
}

С��һ��

Spring Data Reactive ͨ���ṩ��Ӧʽ�ֿ�ӿڣ�������Ӧʽ���ݷ��ʵ�ʵ�֡��������� Reactive Streams �淶�������Է������ķ�ʽ�������ݿ�����������Ӧ�ó�������ܺͿ������ԡ������߿������ɵض���ֿ�ӿڣ���ʹ�� Spring �ṩ�� CRUD �������Զ����ѯ�����������ݲ�����

Spring Data Reactive �����������������ִ���Ӧʽ���ģ�ͺͿ�ܣ��� WebFlux���޷켯�ɣ�Ϊ������ӦʽӦ�ó����ṩ��ǿ������ݷ���������ͨ��ʹ�� Spring Data Reactive�������߿��Թ�����Ч�����Ե�Ӧ�ó���ͬʱ���ִ���ļ���ԺͿ�ά���ԡ�

13. Spring Security Reactive

Spring Security Reactive �� Spring Security ����Ӧʽ��չ����Ϊ��ӦʽӦ�ó����ṩ�˰�ȫ����֤֧�֡������Ƕ� Spring Security Reactive �����Դ��ʵ���߼��Ͳ������ϸ������

Spring Security Reactive ���ĸ���

  • ServerSecurityContextRepository�������������д洢�ͼ��� SecurityContext��
  • ReactiveSecurityContextHolder������ SecurityContext �ij����ߡ�
  • ServerSecurityConfigurer���������ð�ȫ�����ġ�
  • ServerHttpSecurity����������Ӧʽ HTTP ��ȫ���ԡ�
  • ReactiveAuthenticationManager �� ReactiveUserDetailsService�������û���֤���û��������

ServerSecurityContextRepository �ӿڶ���

public interface ServerSecurityContextRepository {
    Mono save(ServerSecurityContext context);
    Mono load();
    void invalidate();
}
  • save������ ServerSecurityContext��
  • load������ ServerSecurityContext��
  • invalidate��ʹ ServerSecurityContext ����

ServerHttpSecurity ����

public class ServerHttpSecurity {
    public ServerHttpSecurity(ReactiveAuthenticationManager authentication) {
        // ...
    }

    public SecurityWebFilterChain build() {
        // ...
    }

    public ServerHttpSecurity authorizeExchange(Consumer configurer) {
        // ...
    }

    // �������÷��������� cors, csrf, formLogin, httpBasic ��
}
  • authorizeExchange��������Ȩ���ԡ�
  • build������ SecurityWebFilterChain��

��Ӧʽ��֤����Ȩ����

  • ������֤������������������ ReactiveAuthenticationManager��

  • �����û����ñ£º´ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ ReactiveUserDetailsService��

  • ���� ServerHttpSecurity��ʹ�� ServerHttpSecurity ������ȫ���ԡ�

  • ���ð�ȫ�����Ĵ洢������ ServerSecurityContextRepository��

  • ע�� WebFilter���� SecurityWebFilterChain ע�ᵽ Web ����������

  • ������֤����Ȩ���������������У�Spring Security Reactive �������󲢴�����֤����Ȩ��

Դ��ʵ���߼�

  • ��ʼ������Ӧ�ó�������ʱ��Spring Security Reactive ��ʼ����ȫ���á�

  • �������أ�SecurityWebFilterChain �������󲢸������õİ�ȫ���Խ��д�����

  • ��֤��ʹ�� ReactiveAuthenticationManager �����û���֤��

  • ��Ȩ������ ServerHttpSecurity ���õ���Ȩ����ʹ�� ReactiveAccessDecisionManager ���з��ʿ��ơ�

  • ��ȫ�����ģ�ʹ�� ServerSecurityContextRepository ����ÿ������İ�ȫ�����ġ�

  • �쳣������������ȫ��ص��쳣������֤ʧ�ܻ���ʾܾ���

  • ��Ӧ��������֤����Ȩ�Ľ����������Ӧ�����ظ��ͻ��ˡ�

С��һ��

Spring Security Reactive Ϊ��ӦʽӦ�ó����ṩ��ȫ��İ�ȫ֧�֡������� Spring Security �ĺ��ĸ����ͨ����Ӧʽ���ģ���ṩ���첽���������İ�ȫ����������ͨ�� ServerHttpSecurity �����ã������߿������ض�����֤����Ȩ���ԣ������㲻ͬӦ�ó���İ�ȫ����

Spring Security Reactive ������������� Spring WebFlux �޷켯�ɣ�Ϊ��Ӧʽ Web Ӧ�ó����ṩǿ��İ�ȫ���ϡ�ͨ��ʹ�� Spring Security Reactive�������߿��Թ�����ȫ���ɿ�������ά������ӦʽӦ�ó���

14. HttpHandler

HttpHandler ����� Spring WebFlux ����һ�����ڴ��� HTTP ����Ľӿڣ�������Ӧʽ���ģ������Ͳ�ε� HTTP ��������Լ��HttpHandler ��Ϊһ����ͬ�Ľӿڣ�������ͬ������ʱ����ͨ����ͬ��ʵ�������� HTTP ���������Ƕ� HttpHandler �����Դ��ʵ���߼��Ͳ������ϸ������

HttpHandler �ӿڶ���

HttpHandler �ӿڶ�����һ�� handle ���������ڴ�������� HTTP ���󲢷���һ����Ӧ��

public interface HttpHandler {
    Mono handle(ServerHttpRequest request, ServerHttpResponse response);
}
handle(ServerHttpRequest request, ServerHttpResponse response)���������������󲢹�����Ӧ��

����ְ��

HttpHandler �ĺ���ְ�������

  • �������󣺽��� ServerHttpRequest ���󣬸ö����װ�� HTTP �������ϸ��Ϣ��
  • ������Ӧ������������Ϣ���� ServerHttpResponse ��������״̬�롢��Ӧͷ�ȡ�
  • ���ؽ��������һ�� Mono ���󣬱�ʾ�첽����Ӧ�������̡�

ʵ�ֲ���

  • ���� HttpHandler ʵ����ʵ�� HttpHandler �ӿڻ�ʹ�����е�ʵ�֡�

  • ���������� handle �����б�д�߼��Դ�����������·�ɡ���֤��ҵ�����ȡ�

  • ������Ӧ����������Ĵ������������Ӧ������״̬�롢��Ӧͷ����Ӧ�塣

  • ���� Mono ������һ�� Mono ����ʾ��Ӧ�Ѿ����ͻò½«±ï¿½ï¿½ï¿½ï¿½Í¡ï¿½

  • ���������� handle �����д������ܷ������쳣��ȷ�����DZ��ʵ���ת��Ϊ��Ӧ��

ʾ��ʵ��

������һ���򵥵� HttpHandler ʵ��ʾ����������һ���̶�����Ӧ��

public class SimpleHttpHandler implements HttpHandler {

    @Override
    public Mono handle(ServerHttpRequest request, ServerHttpResponse response) {
        String body = "Hello, World!";
        response.getHeaders().add("Content-Type", "text/plain");
        return response.writeWith(Flux.just(DataBufferUtils.wrap(body)));
    }
}

С��һ��

HttpHandler ����� Spring WebFlux �����ڴ��� HTTP ����Ļ����ӿڡ����ṩ��һ���򵥶����ķ�ʽ������ HTTP ����͹�����Ӧ��ͨ��ʵ�� HttpHandler �ӿڣ������߿��Կ����������������̣��������������ҵ���߼���������Ӧ������

HttpHandler ��ʵ�ֿ��������� Spring WebFlux ������� DispatcherHandler��HandlerMapping��HandlerAdapter �ȣ����ʹ�ã��Թ���һ����������Ӧʽ Web Ӧ�ó������ֵͲ�εĽӿ�Ϊ��Ҫ�߶ȶ��Ƶ� Web Ӧ�ó����ṩ��ǿ�������ԡ�

15. ContextPathCompositeHandler

ContextPathCompositeHandler �� Spring WebFlux �е�һ���������������ͬһ�������Ͻ����Ӧ�ó���ӳ�䵽��ͬ��������·����context paths�������������ڴ�ͳ�� Servlet ������Ϊÿ�� Web Ӧ�ó������ò�ͬ�� URL ·����

�����Ƕ� ContextPathCompositeHandler �����Դ��ʵ���߼��Ͳ������ϸ������

ContextPathCompositeHandler �ӿڶ���

ContextPathCompositeHandler ʵ���ϲ���һ���ӿڣ����� HandlerMapping �ӿڵ�һ��ʵ�֣�������˶�� Handler ����ÿ�����󶼹���һ��������·����

��Ҫ����

  • contextPaths���洢������·���Ͷ�Ӧ�� Handler ӳ�䡣
  • pattern������ƥ������·�����������ʽ��

������·��ӳ��

ContextPathCompositeHandler ά����һ��ӳ�䣬��ÿ��������·��ӳ�䵽һ�� Handler��

private final Map contextPaths = new ConcurrentHashMap<>();

����Ӧ�ó���

Ӧ�ó�������ڳ�ʼ��ʱͨ�� ContextPathCompositeHandler �� addHandler �������ӵ�ӳ���У�

public void addHandler(String contextPath, HttpHandler handler) {
    this.contextPaths.put(contextPath, handler);
    // �����������ʽģʽ��ƥ������ע���������·��
    updatePattern();
}

��������

ContextPathCompositeHandler ͨ�� getHandler ������ȷ������Ӧ�����ĸ� Handler ������

@Override
public Mono getHandler(ServerWebExchange exchange) {
    String path = extractContextPath(exchange);
    return Mono.justOrEmpty(contextPaths.get(path))
            .map(HandlerAdapter::new)
            .defaultIfEmpty(Mono.defer(() -> createNotFoundError(exchange)));
}

 
  • extractContextPath����ȡ�����������·����
  • getHandler������������·����ӳ���л�ȡ��Ӧ�� Handler��

�������ʽģʽ

ContextPathCompositeHandler ʹ���������ʽ��ƥ������·����

private void updatePattern() {
    // ����ƥ������ע��������·�����������ʽ
    String regex = contextPaths.keySet().stream()
            .map(this::toRegex)
            .collect(Collectors.joining("|", "^(", ")$"));
    this.compiledPattern = Pattern.compile(regex);
}

������

���û���ҵ�ƥ���������·����ContextPathCompositeHandler �ᴴ��һ����ʾ "Not Found" �Ĵ���������

private Mono createNotFoundError(ServerWebExchange exchange) {
    return Mono.just(new HandlerAdapter() {
        @Override
        public boolean supports(Object handler) {
            return true;
        }

        @Override
        public Mono handle(ServerWebExchange exchange, Object handler) {
            return ServerResponse.notFound().build().writeTo(exchange);
        }
    });
}

С��һ��

ContextPathCompositeHandler ����� Spring WebFlux �����ڽ����Ӧ�ó���ӳ�䵽��ͬ������·���� HandlerMapping ʵ�֡���ͨ��ά��һ��������·���� HttpHandler ��ӳ�䣬����ÿ��Ӧ�ó��������Լ�������·����ͨ���������ʽƥ������·������ʹ�� HandlerAdapter ������͵�����Ӧ�Ĵ�������

�������ģʽʹ���ڵ���������ʵ���в���͹������ WebFlux Ӧ�ó����ü򵥺͸�Ч��ÿ��Ӧ�ó��򶼿������Լ���������·������ ContextPathCompositeHandler ��������·�ɵ���ȷ��Ӧ�ó���������

���

������Spring WebFlux ��ܺ��������ȫ�������ˣ�ϣ�����԰�����ȫ����������� WebFlux��ԭ������ע�����簮��̡�����ҳ��ɲ鿴V��ÿ����µ�ԭ���������ݣ�������һ��ɳ���

С���Ƽ��Ķ�

�������������Ľ�Ϊ������Ϣ����������������ͬ���޹۵��֤ʵ��������

RPG RiÐòÕÂ 0.2.1
RPG Ri���� 0.2.1
���ͣ���ɫ����������Ӫ״̬����ʽ��Ӫ�������ԣ� ���ġ���

��Ϸ����

��Ϸ���

��Ϸ��Ƶ

��Ϸ����

��Ϸ�

��RPG_Ri���¡���GameMaker'Child-Dream'������һ����������RPG���Σ���ȫ��ѵĻ��������RPG�dz���Ԫ
ºËÐÄ
����
���ͣ���������������Ӫ״̬����ʽ��Ӫ�������ԣ� Ӣ�� ����

��Ϸ����

��Ϸ���

��Ϸ��Ƶ

��Ϸ����

��Ϸ�

�����ġ���CORE������Ϸ��FURYJAM���µ�һ�������������Σ���Ϸ�У���ҽ����Ʒ���İ�ɫС���ģ�������

�����Ƶ����

����

ͬ������

����

ɨ��ά�����������ֻ��汾��

ɨ��ά����������΢�Ź��ںţ�

��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]

��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������