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>
  • 相关阅读:
    Linux系统中的load average
    通过数据库评估存储设备IO性能-Oracle11gIO校准功能介绍
    ORACLE查询字段中含有空格的数据
    JavaScript&Typescript中的时间
    LeetCode
    LeetCode
    面试问题及知识汇总
    Bootstrap布局容器与栅格系统
    JVM垃圾回收(GC)
    9. 专题
  • 原文地址:https://www.cnblogs.com/yimiyan/p/6780608.html
Copyright © 2011-2022 走看看