[Spring] spring blocker - - - spring mvc interceptor

 

 

1. Profiles injected Bean

<!-- 权限拦截 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**/save/**"/>
            <bean class="com.proengine.partner.common.interceptor.PromotionStoreAuthInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

 

 

2. interceptor class

Package com.proengine.partner.common.interceptor; 

Import javax.annotation.Resource;
 Import the javax.servlet.http.HttpServletRequest;
 Import javax.servlet.http.HttpServletResponse;
 Import java.io.PrintWriter; 

/ ** 
 * @author: SXD 
 * @Description: interceptor 
 * @date: 2020/1/7 15:47 Create in 
 * / 
public  class PromotionStoreAuthInterceptor the extends BaseController the implements HandlerInterceptor from { 



    
    / ** 
     * this method of processing the request into the controller before performing the method, which returns value indicates whether an interrupt subsequent operations proceed down returns true, false returns an interrupt subsequent operations. 
     * / 
    @Override 
    public  booleanThe preHandle (the HttpServletRequest Request, Response the HttpServletResponse, Object Handler) throws Exception { 
         the Map <String, String []> = request.getParameterMap the parameterMap (); // Get request parameter 

        the StringBuilder errMsg An = new new the StringBuilder ();                    
         IF (errMsg.length () > 0 ) { 
            Response (request, Response, errMsg.toString ()); 
            return  to false ; // interception request 
        }
         return  to true ; // let request 
    } 


    
    / ** 
     * this method requests in the processing method is called after the controller, performed prior to parsing view, this method can make further changes to the domain model and view requests.
     * / 
    @Override 
    public  void The postHandle (the HttpServletRequest Request, Response the HttpServletResponse, Object Handler, ModelAndView ModelAndView) throws Exception { 

    } 


    / ** 
     * This method after completion of the execution processing request controller method, i.e., after the rendering view to be achieve some resource cleanup, logs and other information to work through this method. 
     * / 
    @Override 
    public  void afterCompletion (the HttpServletRequest Request, Response the HttpServletResponse, Object Handler, Exception EX) throws Exception { 

    } 


    // return intercepted message to the front end, to solve the cross-domain, coding problems 
    Private  void Response (the HttpServletRequest Request, Response the HttpServletResponse, String Message ) throws Exception {
        Origin StringRequest.getHeader = ( "Origin" );
         IF (StringUtils.isNotBlank (Origin)) {
             IF (. Origin.toLowerCase () the indexOf (. "Dmall.com")> 0 ) { 
                response.setHeader ( "Access-Control- Origin-the allow ", Origin);   // allow access domain 
                response.setHeader (" access-Control-the allow-Methods "," POST, the GET "); // allow GET, POST request domain 
                response.setHeader (" access the allow-Credentials--Control "," to true "); // permission request to the server with a cookie 
                response.setContentType (" text / html; charset = utf-8 ");// set the standard output JSON format, and coding 
            } 
        }

        Pw PrintWriter = response.getWriter ()? 
        pw.write (JSON.toJSONString (message)); 
        pw.flush (); 
        pw.close (); 
    } 



}
View Code

 

 

Reference Address: http://c.biancheng.net/view/4431.html

Guess you like

Origin www.cnblogs.com/sxdcgaq8080/p/12167651.html