首页 > 解决方案 > 如何使用 Netty 在 Spring Boot 2 中记录请求?

问题描述

我将 spring boot 2 与反应器项目(webflux 等)一起使用。我有几个端点,我想将每个请求记录到标准输出或文件。我怎样才能正确地做到这一点?我知道有一些过滤器等,但那些是用于 tomcat 的。

标签: spring-bootspring-webflux

解决方案


您可以WebFilter用于 Servlet API 过滤器的反应式实现。

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/server/WebFilter.html

org.springframework.web.server.WebFilter

这是来自 Spring Security 的 AuthenticationFilter 的反应式示例

https://github.com/spring-projects/spring-security-reactive/blob/master/spring-security-reactive/src/main/java/org/springframework/security/web/server/AuthenticationWebFilter.java

    /*
 * Copyright 2002-2016 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.
 */
package org.springframework.security.web.server;

import org.springframework.core.annotation.Order;
import org.springframework.core.convert.converter.Converter;
import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.web.server.context.WebSessionSecurityContextRepository;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;

import reactor.core.publisher.Mono;

/**
 *
 * @author Rob Winch
 * @since 5.0
 */
@Order(0)
public class AuthenticationWebFilter implements WebFilter {

    private WebSessionSecurityContextRepository securityContextRepository;

    private Converter<ServerWebExchange,Mono<Authentication>> authenticationConverter;

    private ReactiveAuthenticationManager authenticationManager;

    private AuthenticationEntryPoint entryPoint;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        return authenticationConverter.convert(exchange)
            .flatMap( token -> {
                return authenticationManager.authenticate(token)
                    .flatMap(authentication -> {
                        SecurityContext context = new SecurityContextImpl();
                        context.setAuthentication(authentication);
                        return securityContextRepository
                            .save(exchange, context)
                            .flatMap( value ->{
                                return chain.filter(exchange);
                            });
                    })
                    .onErrorResume( AuthenticationException.class, t -> {
                        return entryPoint.commence(exchange, t);
                    });
            })
            .switchIfEmpty(Mono.defer(() -> {
                return chain.filter(exchange);
            }));
    }

    public void setSecurityContextRepository(WebSessionSecurityContextRepository securityContextRepository) {
        this.securityContextRepository = securityContextRepository;
    }

    public void setAuthenticationConverter(Converter<ServerWebExchange,Mono<Authentication>> authenticationConverter) {
        this.authenticationConverter = authenticationConverter;
    }

    public void setAuthenticationManager(ReactiveAuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

    public void setEntryPoint(AuthenticationEntryPoint entryPoint) {
        this.entryPoint = entryPoint;
    }
}

推荐阅读