消息拦截器是一个类,接收 HTTP request并返回 HTTP response,Message handler 继承自抽象类 HttpMessageHandler
可以自定义 MessageHandler,消息拦截器的作用如:
自定义 Message Handlers
自定义 MessageHandler 需要继承 System.Net.Http.DelegatingHander 并且重载SendAsync方法
Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken);
这个方法的一般的处理流程是
public class MessageHandler1 : DelegatingHandler { protected async override Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { Debug.WriteLine("Process request"); // Call the inner handler. var response = await base.SendAsync(request, cancellationToken); Debug.WriteLine("Process response"); return response; } }
消息拦截器生效
在 WebApiConfig中注册相关
public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.MessageHandlers.Add(new MessageHandler1()); config.MessageHandlers.Add(new MessageHandler2()); // Other code not shown... } }
https 请求拦截器
要求所有的请求都必须是https的请求,可以自定义一个https的拦截器,如果是https请求,就继续处理,否则就返回相关的提示信息
public class RequireHttpsHandler : DelegatingHandler { public RequireHttpsHandler(int httpsPort) { _httpsPort = httpsPort; } public RequireHttpsHandler() : this(443) { } protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { //判断是否是 https 请求 if (request.RequestUri.Scheme == Uri.UriSchemeHttps) return base.SendAsync(request, cancellationToken); var response = CreateResponse(request); var tcs = new TaskCompletionSource<HttpResponseMessage>(); tcs.SetResult(response); return tcs.Task; } private HttpResponseMessage CreateResponse(HttpRequestMessage request) { HttpResponseMessage response; var uri = new UriBuilder(request.RequestUri); uri.Scheme = Uri.UriSchemeHttps; uri.Port = _httpsPort; var body = string.Format("HTTPS is required<br/>The resource can be found at <a href=\"{0}\">{0}</a>.", uri.Uri.AbsoluteUri); if (request.Method.Equals(HttpMethod.Get) || request.Method.Equals(HttpMethod.Head)) { response = request.CreateResponse(HttpStatusCode.Found); response.Headers.Location = uri.Uri; if (request.Method.Equals(HttpMethod.Get)) response.Content = new StringContent(body, Encoding.UTF8, "text/html"); } else { response = request.CreateResponse(HttpStatusCode.NotFound); response.Content = new StringContent(body, Encoding.UTF8, "text/html"); } return response; } }
Enable CORS 拦截器
public class SimpleCorsHandler : DelegatingHandler { private const string origin = "Origin"; private const string accessControlRequestMethod = "Access-Control-Request-Method"; private const string accessControlRequestHeaders = "Access-Control-Request-Headers"; private const string accessControlAllowOrigin = "Access-Control-Allow-Origin"; private const string accessControlAllowMethods = "Access-Control-Allow-Methods"; private const string accessControlAllowHeaders = "Access-Control-Allow-Headers"; protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var isCorsRequest = request.Headers.Contains(origin); var isPreflightRequest = request.Method == HttpMethod.Options; if (isCorsRequest) { if (isPreflightRequest) { return Task.Factory.StartNew(() => { var response = new HttpResponseMessage(HttpStatusCode.OK); response.Headers.Add(accessControlAllowOrigin, request.Headers.GetValues(origin).First()); var currentAccessControlRequestMethod = request.Headers.GetValues(accessControlRequestMethod). FirstOrDefault(); if (currentAccessControlRequestMethod != null) { response.Headers.Add(accessControlAllowMethods, currentAccessControlRequestMethod); } var requestedHeaders = string.Join(", ", request.Headers.GetValues(accessControlRequestHeaders)); if (!string.IsNullOrEmpty(requestedHeaders)) { response.Headers.Add(accessControlAllowHeaders, requestedHeaders); } return response; }, cancellationToken); } else { return base.SendAsync(request, cancellationToken).ContinueWith(t => { var resp = t.Result; resp.Headers.Add( accessControlAllowOrigin, request.Headers.GetValues(origin).First()); return resp; }); } } else { return base.SendAsync(request, cancellationToken); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。
拦截消息框 bp MessageBox(A) 创建消息框 bp MessageBoxExA 创建消息框 bp MessageBoxIndirect(A) 创建定制消息框
本文向大家介绍SpringBoot拦截器实现登录拦截的方法示例,包括了SpringBoot拦截器实现登录拦截的方法示例的使用技巧和注意事项,需要的朋友参考一下 源码 GitHub:https://github.com/291685399/springboot-learning/tree/master/springboot-interceptor01 SpringBoot拦截器可以做什么 可以对UR
easyopen在1.3.1版本开始支持拦截器。 easyopen拦截器实现原理跟springmvc拦截器类似,拦截器作用在api方法上,即有@Api注解的方法。 拦截器定义如下: /** * 拦截器,原理同springmvc拦截器 * @author tanghc * */ public interface ApiInterceptor { /** * 预处理回调方法,
你可以配置处理器拦截器HandlerInterceptors或web请求拦截器WebRequestInterceptors等拦截器,并配置它们拦截所有进入容器的请求,或限定到符合特定模式的URL路径。 在MVC Java编程配置下注册拦截器的方法: @Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigure
Uragano里可以自定义拦截器,并且拦截器分服务器端拦截器和客户端拦截器 拦截器还分全局拦截器和局部拦截器,并且拦截器也是支持依赖注入的 创建拦截器 public class ClientGlobalInterceptor : InterceptorAbstract { private ILogger Logger { get; } public
我目前正在使用带有的Kafka绑定器的Spring Cloud Stream为我的Spring Boot微服务执行消息记录。 我有: 生产者将消息发布到订阅频道 在消息从生产者发布到流并被消费者收听的整个过程中,可以观察到preSend方法被触发了两次: 一次在生产者端-消息发布到流时 然而,出于日志记录的目的,我只需要在消费者端截获并记录消息。 是否有任何方法可以仅在一侧(例如消费者侧)截获SC