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>

 

posted on 2017-04-28 10:52  依米艳  阅读(641)  评论(0编辑  收藏  举报

导航