通过HttpServletRequest重写+filter 添加header
问题说明
需要做的事情比较简单,就是通过filter 重写httpservletrequest ,同时给予request 添加header
主要是通过HttpServletRequestWrapper 进行处理,代码写死了,只是一个简单的演示。
自定义HttpServletRequest(实现HttpServletRequestWrapper)
package com.example.demo;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
final class MutableHttpServletRequest extends HttpServletRequestWrapper {
private final Map<String, String> customHeaders;
public MutableHttpServletRequest(HttpServletRequest request){
super(request);
this.customHeaders = new HashMap<String, String>();
}
public void putHeader(String name, String value){
this.customHeaders.put(name, value);
}
@Override
public String getHeader(String name) {
if (name=="userid") {
return "dalong";
}
return "demo";
}
public Map<String, String> getCustomHeaders() {
return customHeaders;
}
@Override
public Enumeration<String> getHeaders(String name) {
if (null != name && name.equals("userid")) {
return new Enumeration<String>() {
private boolean hasGetted = false;
@Override
public boolean hasMoreElements() {
return !hasGetted;
}
@Override
public String nextElement() {
if (hasGetted) {
throw new NoSuchElementException();
} else {
hasGetted = true;
return "dalong";
}
}
};
}
return super.getHeaders(name);
}
}
filter
调用自定义HttpServletRequestWrapper 实现header 添加
package com.example.demo;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(urlPatterns = "/*",filterName = "tokenfilter")
public class UserIdFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
MutableHttpServletRequest newrequest = new MutableHttpServletRequest(request);
System.out.printf("filter running");
filterChain.doFilter(newrequest,response);
}
}
scan
添加scan 自动注入filter
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
service
rest api 测试
package com.example.demo;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
public class UserService {
@RequestMapping(value = "/user",method= RequestMethod.GET)
public Object Userinfo(HttpServletRequest request,@RequestHeader String userid){
// User user =new User(request.getHeader("userid"),222);
User user =new User(userid,222);
return user;
}
}
user entity
用户实体,用来测试的
package com.example.demo;
public class User {
private String username;
private int age;
public User(String username, int age) {
this.username = username;
this.age = age;
}
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
applicaiton 入口
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
测试效果
curl -i http://localhost:8080/user
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sat, 11 May 2019 15:09:19 GMT
{"username":"dalong","age":222}%
说明
getHeaders 也是需要实现的,不然基于注解的@RequestHeader
不生效
参考资料
http://wilddiary.com/adding-custom-headers-java-httpservletrequest/
https://github.com/rongfengliang/HttpServletRequst-Rewrite-Demo
通过HttpServletRequest重写+filter 添加header的更多相关文章
- 关于 preHandle 重写和添加参数问题,重写HttpServletRequestWrapper和Filter
由于 preHandle 中HttpServletRequest 只有setAttribute而没有setParameter 也没有 add 方法 所以是没办法直接添加参数的.从网上查了很多资料,基本 ...
- RecyclerView添加Header的正确方式
原文链接:http://blog.csdn.net/qibin0506/article/details/49716795 看了一下博客目录,已经有好几篇博客是关于RecyclerView的,不过对于这 ...
- ## GridView 布局:item设置的高度和宽度不起作用、自动适配列数、添加Header和Footer ##
一.item设置的高度和宽度不起作用 转自:http://www.cnblogs.com/0616--ataozhijia/p/6031875.html [Android Pro] listView和 ...
- RecyclerView添加Header和Footer
使用过RecyclerView的同学就知道它并没有添加header和footer的方法,而ListView和GirdView都有,但是开发过程中难免有需求需要添加一个自定义的header或者foote ...
- SDWebImage添加header
title: SDWebImage添加headerdate: 2016-03-07 17:32:57tags: SDWebImagecategories: IOS keywords: SDWebIma ...
- Android RecyclerView添加Header头部
Android RecyclerView添加Header头部 Android RecyclerView不像以前的ListView那样直接添加头部,如果要给RecyclerView增加头部,则需要 ...
- gridview添加header
gridview是不能添加header的,这里的解决方法是将listview改造成gridview使用,功能很好用,唯一的缺点是列数不能自适应 示例代码下载地址http://pan.baidu.com ...
- 怎样在UICollectionView中添加Header和footer
---恢复内容开始--- 怎样在UICollectionView中添加Header和footer 转载于http://my.oschina.net/zboy/blog/221525 摘要 来自-htt ...
- 在Storyboard中为UITableView添加Header和Footer
我在这里所说的Header和Footer并不是sectionHeader和sectionFooter,而是指UITableView的tableHeaderView和tableFooterView,这两 ...
随机推荐
- Linux下的应用进程监控
两个思路: 一.定时执行监控脚本 采用centos自带的crontab根据需要定时执行status.sh脚本 #!/bin/bash status=$(ps -aux | grep "rsy ...
- 入门篇-contrail-command(对接openstack)All-In-One
基础环境 系统: centos7.6(3.10.0-957) 64G内存 500G磁盘 关闭防火墙 systemctl disable firewalld 关闭selinux sed -i 's/SE ...
- C#——零散学习1
C#——零散学习1 //结构体(与C语言相似) struct Position { public float x; public float y; //不一定需要把结构体成员设置为pu ...
- asp.net core 之中间件
Http请求资源的过程可以看成一个管道:“Pipe”,并不是所有的请求都是合法的.安全的,其于功能.性能或安全方面的考虑,通常需要在这管道中装配一些处理程序来筛选和加工这些请求.这些处理程序就是中间件 ...
- IntelliJ IDEA web项目进行数据库连接时出现java.lang.ClassNotFoundException: com.mysql.jdbc.Driver错误解决办法
首先看报错信息: 意思是找不到类: com.mysql.jdbc.Driver.也就是说tomcat找不到MySQL数据库连接要用的jar包! 出现这种错误的原因是: 项目中没有导入这个jar包, ...
- JQ实现购物车全选跟总计全选
//GoodsCheck购物车每个店铺的checkBox//goods-check购物车所有的checkBox//ShopCheck店铺全选的按钮//commlistFrm店铺商品的模块//allCh ...
- EF执行存储过程(转载)
https://blog.csdn.net/xiaouncle/article/details/82914255 相关文章: https://www.cnblogs.com/Coder-ru/arch ...
- Gitlab创建一个项目(二)创建新用户以及分配项目
Gitlab创建一个项目(一) 1.进入gitlab控制台 2.点击“新建用户” 3.点击“Edit”,创建初始密码 4.分配项目,首页进入项目 5.进入Members菜单 6.选择用户 7.赋予权限 ...
- Linux SSH 服务
本篇写一些关于Linux网络中SSH服务的相关知识. 测试环境 名称 IP地址 host01 192.168.28.128 host02 192.168.28.129 host03 192.168.2 ...
- FreeBSD关机后自动重启的解决办法
我用的是华硕的笔记本电脑,不知道别的电脑有没有这个情况,按handbook关机指令为shutdown -p now,但是我执行这个指令后电脑却自动重启,用Linux关机指令shutdown -h no ...