須要作的事情比較簡單,就是經過filter 重寫httpservletrequest ,同時給予request 添加header
主要是經過HttpServletRequestWrapper 進行處理,代碼寫死了,只是一個簡單的演示。java
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); } }
調用自定義HttpServletRequestWrapper 實現header 添加git
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 自動注入filtergithub
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); } }
rest api 測試web
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; } }
用戶實體,用來測試的spring
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; } }
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
不生效json
http://wilddiary.com/adding-custom-headers-java-httpservletrequest/
https://github.com/rongfengliang/HttpServletRequst-Rewrite-Demoapi