时间:2023-01-07 09:29:05 | 栏目:JAVA代码 | 点击:次
在Spring中request的值只能被获取一次,拦截器获取之后就会导致Controller拿不到值
将request的值进行备份,请求到达Controller的时候就会拿到这个值
创建一个自己的HttpServletRequestWrapper并继承servlet的HttpServletRequestWrapper,为了备份request中的值。
public class MyHttpServletRequestWrapper extends HttpServletRequestWrapper { private final byte[] buff; public MyHttpServletRequestWrapper(HttpServletRequest request) throws IOException { super(request); InputStream is = request.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int len; while ((len = is.read(b)) != -1) { baos.write(b, 0, len); } buff = baos.toByteArray(); } @Override public ServletInputStream getInputStream() throws IOException { final ByteArrayInputStream bais = new ByteArrayInputStream(buff); return new ServletInputStream() { @Override public int read() throws IOException { return bais.read(); } }; } @Override public BufferedReader getReader() throws IOException { return new BufferedReader(new InputStreamReader(getInputStream())); } }
创建一个Filter,将该过滤器配置在项目中,为了调用备份的HttpServletRequestWrapper
public class MyRequestBodyFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; servletRequest = new MyHttpServletRequestWrapper(httpServletRequest); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } }
创建一个工具类,在拦截器中获取request的值
public class RequestUtils { public static String getRequestValue(HttpServletRequest request) throws IOException { StringBuffer sb = new StringBuffer(); MyHttpServletRequestWrapper myHttpServletRequestWrapper = new MyHttpServletRequestWrapper(request); InputStream is = myHttpServletRequestWrapper.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str; while ((str = br.readLine()) != null) { sb.append(str); } return sb.toString(); } }
在使用SpringMVC进行项目的时候用到了权限验证。
表分为:
用户-角色-资源都是多对多的关系,验证无非就是收到请求后,在拦截器循环判断用户是否有权限执行操作。
再逐一循环判断是否可以操作
只是这种方法很让人难受。
preHandle方法中handler实际为HandlerMethod,(看网上说的有时候不是HandlerMethod),加个instanceof验证吧
自定义注解代码:
@Retention(RUNTIME) @Target(METHOD) public @interface MyOperation { String value() default "";//默认为空,因为名字是value,实际操作中可以不写"value=" }
Controller代码:
@Controller("testController") public class TestController { @MyOperation("用户修改")//主要看这里 @RequestMapping("test") @ResponseBody public String test(String id) { return "Hello,2018!"+id; } }
拦截器的代码:
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("进入拦截器"); if(handler instanceof HandlerMethod) { HandlerMethod h = (HandlerMethod)handler; System.out.println("用户想执行的操作是:"+h.getMethodAnnotation(MyOperation.class).value()); //判断后执行操作... } return HandlerInterceptor.super.preHandle(request, response, handler); }
补充
在每个方法上面加注解太麻烦啦,可以在类上加注解
@Retention(RUNTIME) @Target(TYPE) public @interface MyOperation { String value() default ""; } //拦截器中这样获得 h.getMethod().getDeclaringClass().getAnnotation(MyOperation.class);
我可以获取requestMapping,不用创建自定义注解啊,值得注意的是,不要使用GetMapping等,要使用requestMapping。