如何使用Netty在Spring Boot 2中记录请求?

时间:2018-07-06 10:12:42

标签: spring-boot spring-webflux

我在反应堆项目(webflux等)中使用了弹簧靴2。我有两个端点,我想将每个请求记录到stdout或文件中。我该怎么做呢?我知道有一些过滤器等,但是这些过滤器是用于tomcat的。

1 个答案:

答案 0 :(得分:1)

您可以使用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;
    }
}