zoukankan      html  css  js  c++  java
  • spring mvc自定义注解--访问时验证

    作用:在访问controller的方法时,判断用户是否是登陆状态。

    step1:定义注解

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target({ ElementType.TYPE, ElementType.METHOD })
    @Retention(RetentionPolicy.RUNTIME)
    public @interface LoginRequired {
    
    	public String loginUrl() default "";
    
    }
    

    step2:定义拦截器,继承HandlerInterceptorAdapter抽象类,重新preHandle方法

    public class TicketInterceptor extends HandlerInterceptorAdapter {
    
    
        @Autowired
        private UserDetailMapper userDetailMapper;
    
        @Autowired
        private UserRoleService userRoleService;
    
        @Autowired
        private UserOrgMapper userOrgMapper;// add by cuiyan 20150604 用户机构
    
        @Override
        public boolean preHandle(HttpServletRequest request,
                HttpServletResponse response, Object handler) throws Exception {
            LoginRequired loginRequired = MethodInterceptorUtils.getAnnotaion(
                    handler, LoginRequired.class);
              //..........判断逻辑
          
        }
    }
    

     step3:springmvc的配置文件,拦截器

    <mvc:interceptors>
        <bean class="xxxx" />
    </mvc:interceptors>
  • 相关阅读:
    专业英语阅读(二)
    专业英语阅读(一)
    高精度运算
    高精度运算——加减乘除阶乘
    python常见编程面试题汇总
    python线程
    反射
    单例模式
    生成器、迭代器
    python装饰器
  • 原文地址:https://www.cnblogs.com/yimiyan/p/6780608.html
Copyright © 2011-2022 走看看