spring-boot - 如何使用 Netty 在 Spring Boot 2 中记录请求?
问题描述
我将 spring boot 2 与反应器项目(webflux 等)一起使用。我有几个端点,我想将每个请求记录到标准输出或文件。我怎样才能正确地做到这一点?我知道有一些过滤器等,但那些是用于 tomcat 的。
解决方案
您可以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 的反应式示例
/*
* 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;
}
}
推荐阅读
- facebook-graph-api - Fb/Instagram Graph API:(#10)business_discovery 权限被拒绝
- python - Kivy 动态类 - 多个对象
- r - 为什么在使用动态变量名时在 Sparklyr 中加 1 实际上加 2?
- python - Python - 从与参数匹配的文件中提取数据
- javascript - Javascript函数只工作一次
- mysql - mysql:获取独特产品的更简单、更有效的查询
- laravel - Route 中缺少必需的参数
- java - 如何修复此 java 错误 - $javac LoginAssignment.java
- typescript - 如何在扩展接口方法中设置`this`来输入变量
- node.js - 将变量从 Rails 应用程序传递到节点包