java拦截器实例

来源:互联网 发布:网络与信息安全工程师 编辑:程序博客网 时间:2024/06/08 13:34
public class EntranceInterceptor extends HandlerInterceptorAdapter {private Logger LOG = Logger.getLogger(EntranceInterceptor.class);@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {MDC.put(LogUtil.KEY_REQUEST_ID, RandomStringUtils.randomAlphanumeric(8));LOG.debug(LogUtil.getLogStr(request));//过滤掉静态资源if(handler instanceof HandlerMethod){//获取游戏String url = request.getRequestURI();if(!"".equals(url) && !"/".equals(url)){String str = url.substring(url.indexOf("/") + 1);StringTokenizer st = new StringTokenizer(str, "/");String game = "";int c = 0;while (st.hasMoreTokens()) {if(++c == 2){game = st.nextToken();}st.nextToken();}LOG.debug(LogUtil.getLogStr("[当前访问活动名称]:[" + game + "]"));TempleConfigContext.setCurrentGameActivity(GameActivityEnum.getGameActivityEnum(game));}//是否有AjaxAnno注解HandlerMethod method = (HandlerMethod) handler;AjaxAnno anno = method.getBean().getClass().getAnnotation(AjaxAnno.class);if (anno != null || (anno = method.getMethod().getAnnotation(AjaxAnno.class)) != null) {TempleConfigContext.setCurrentRequestType(TempleConfigContext.AJAX_REQUEST_TYPE);}LoginRequired required = method.getBean().getClass().getAnnotation(LoginRequired.class);if (required != null || (required = method.getMethod().getAnnotation(LoginRequired.class)) != null) {//验证是否登录GameActivityEnum activity = TempleConfigContext.getCurrentGameActivity();if (request.getSession().getAttribute(activity.getActivity() + "_" + Constant.MANAGER_SESSION_LOGIN_USER) == null) {// 判断session里是否有用户信息throw new BusinessException(CommonStateEnum.BADREQ_PARA_SESSION_TIMEOUT, "您还没有登录或者您的登录已经过期");}}PassAction pass = method.getBean().getClass().getAnnotation(PassAction.class);if (pass == null && (pass = method.getMethod().getAnnotation(PassAction.class)) == null) {//验证是否过期Date[] limit = Constant.getHdLimitTime(TempleConfigContext.getCurrentGameActivity().getActivity()); if(null != limit){SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date();if(null != limit[0] && date.before(limit[0])){CommonStateEnum error = CommonStateEnum.BADREQ_API_HD_NOT_SATRT;throw new BusinessException(error, error.getMessage(sdf.format(limit[0])));}if(null != limit[1] && date.after(limit[1])){CommonStateEnum error = CommonStateEnum.BADREQ_API_HD_ALREADY_END;throw new BusinessException(error, error.getMessage(sdf.format(limit[0]), sdf.format(limit[1])));}}}}return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {}@Overridepublic void afterCompletion(HttpServletRequest request,HttpServletResponse response, Object handler, Exception ex)throws Exception {MDC.remove(LogUtil.KEY_REQUEST_ID);}}

java拦截器 配合 自定义注解实现控制用户是否登录   控制游戏活动时间

@Retention(value = RetentionPolicy.RUNTIME)@Target({ ElementType.METHOD, ElementType.TYPE })public @interface PassAction {}
PassAction为空 则表示 不用验证时间,不为空则需要验证游戏活动时间


@Target({ ElementType.METHOD, ElementType.TYPE })@Retention(value = RetentionPolicy.RUNTIME)public @interface LoginRequired {}
LoginRequired用户验证是否登陆 有些controller需要登录才能继续下面逻辑代码,如果判断登录的逻辑在每个controller里都写一遍代码冗余很多 所以基于aop的思想通过注解加拦截器的方式来精简代码

1 0
原创粉丝点击