springMvc custom annotation validation

 

springMvc custom annotation validation

 

Annotation is a mark. Annotation has a special mark. If a method is marked with this annotation, it can cooperate with the interceptor, and the interceptor intercepts to meet the interception.

If the request of the rule is intercepted successfully, then check whether the method has this annotation in the interceptor, and if so, check the corresponding tag value in the annotation when it meets the requirements

 

 

package com.demo.web.auth;

 

import java.lang.annotation.Documented;

import java.lang.annotation.ElementType;

import java.lang.annotation.Inherited;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

 

@Documented

@Inherited

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)/////The highest priority @Order(Ordered.HIGHEST_PRECEDENCE) , you can also set the priority

public @interface AuthPassport {

    boolean validate() default true;

}

 

 

 

 

 

 

 

package com.demo.web.auth;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

 

public class AuthInterceptor extends HandlerInterceptorAdapter {

    

    @Override

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        

        if(handler.getClass().isAssignableFrom(HandlerMethod.class)){

            AuthPassport authPassport = ((HandlerMethod) handler).getMethodAnnotation(AuthPassport.class);

            

            //Do not declare that permission is required, or declare that permission is not verified ( check whether there is this annotation in the interceptor )

                if(authPassport == null || authPassport.validate() == false)//Get the value by calling the method

                return true;

            else{                

                // Implement your own permission verification logic here ( you can directly import the permission plug-in (shrio's verification code, if you just log in, you don't have to, just verify the login name and password directly ))

                if(false)//If the verification is successful, return true (write false here to simulate the processing of verification failure)

                    return true;

                else//If validation fails

                {

                    //return to the login interface

                    response.sendRedirect("account/login");

                    return false;

                }       

            }

        }

        else

            return true;   

     }

}

 

 

 

 

 

 

<mvc:interceptors>  

    <!-- If the internationalization operation interceptor is based on (request/Session/Cookie), it must be configured--> 

    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />  

    <!-- If mvc:mapping path is not defined, all URL requests will be intercepted -->

    <bean class="com.demo.web.auth.AuthInterceptor"></bean>

////Configure when map needs to be configured, another configuration method, precise interception

   // <mvc:interceptor>

    //        <mvc:mapping path="/*.do"  />

     //       <bean  class="com.party.common.interceptor.AuthCheckInteceptor"/>        

       // </mvc:interceptor>

</mvc:interceptors>

 

 

 

 

@AuthPassport ( validate =' false ' ) // use this KY method to change the value in the annotation

@RequestMapping(value={"/index","/hello"})

public ModelAndView index(){

    

    ModelAndView modelAndView = new ModelAndView();  

    modelAndView.addObject("message", "Hello World!");  

    modelAndView.setViewName("index");  

    return modelAndView;

}

 

 

 

 

 

 

 

 

 

 

refer to:

http://www.cnblogs.com/liukemng/p/3751338.html

 

 

http://www.cnblogs.com/parryyang/p/5413618.html

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326357447&siteId=291194637