Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that Controller can not get the value after the interceptor gets the value of request?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to solve the problem that the interceptor can not get the value of request after obtaining the value of Controller". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the problem that the interceptor can not get the value of Controller after obtaining the value of Controller".

Catalogue

After the interceptor gets the value of request, Controller does not get the value

Reason

Solution method

Use interceptor to get Controller method name and annotation information

Method 1: obtain the user's URL through request

Method 2: judge whether you have permission by the method that the user wants to access.

Method 3: custom annotations

After the interceptor gets the value of request, the reason why Controller can't get the value

In Spring, the value of request can only be obtained once, and after the interceptor gets it, the Controller will not get the value.

Solution method

Back up the value of request, which will be obtained when the request arrives at Controller

Create your own HttpServletRequestWrapper and inherit servlet's HttpServletRequestWrapper in order to back up the values in 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 ();}}

Create a Filter and configure the filter in the project to invoke the backed-up 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 () {}}

Create a utility class to get the value of request in the interceptor

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 () }} use interceptor to get Controller method name and annotation information

Permission verification is used when using SpringMVC for the project.

The table is divided into:

User table

Role table

Resource table

Users-roles-resources are many-to-many relationships, and verification is nothing more than determining whether the user has permission to perform operations in the interceptor loop after receiving the request.

Method 1: obtain the user's URL through request

And then cycle one by one to determine whether it can be operated or not.

It's just that this method is very uncomfortable.

Method 2: judge whether you have permission by the method that the user wants to access.

The handler in the preHandle method is actually HandlerMethod (sometimes it's not HandlerMethod on the Internet), so add an instanceof to verify it.

You can get the method name: h.getMethod () .getName ()

You can get the value in the RequestMapping annotation: h.getMethodAnnotation (RequestMapping.class)

This method is still not very convenient.

Method 3: custom annotations

Custom annotation code:

@ Retention (RUNTIME) @ Target (METHOD) public @ interface MyOperation {String value () default ""; / / default is empty, because the name is value, you can not write "value="} in practice.

Controller Code:

@ Controller ("testController") public class TestController {@ MyOperation ("user modification") / / here @ RequestMapping ("test") @ ResponseBody public String test (String id) {return "Hello,2018!" + id;}}

Code for the interceptor:

@ Overridepublic boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println ("enter interceptor"); if (handler instanceof HandlerMethod) {HandlerMethod h = (HandlerMethod) handler; System.out.println ("the action the user wants to perform is:" + h.getMethodAnnotation (MyOperation.class). Value ()); / / execute the operation after judgment.} return HandlerInterceptor.super.preHandle (request, response, handler);}

Supplement

It's too troublesome to annotate each method. You can annotate the class.

@ Retention (RUNTIME) @ Target (TYPE) public @ interface MyOperation {String value () default ";} / / the interceptor gets h.getMethod () .getDeclaringClass () .getAnnotation (MyOperation.class)

I can get requestMapping, do not create custom annotations ah, it is worth noting that do not use GetMapping, etc., to use requestMapping.

Thank you for your reading, the above is the content of "how to solve the problem that Controller can not get the value after interceptor obtains the value of request". After the study of this article, I believe you have a deeper understanding of how to solve the problem that Controller can not get the value of interceptor after obtaining the value of Controller, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report