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
- java拦截器实例
- Hibernate 拦截器实例
- 拦截器 实例
- SpringMVC拦截器实例
- springmvc拦截器实例
- 拦截器实例
- struts--拦截器实例
- java WebService CXF Spring 自定义拦截器 附实例源码
- Struts2拦截器实例-权限拦截器
- struts2 拦截器实现实例
- Struts2拦截器的实例
- struts2拦截器配置实例
- springmvc拦截器Interceptor实例
- Struts2拦截器配置实例
- struts2-timer拦截器实例
- Java简单拦截器
- java实现拦截器
- java 拦截器
- Android深入理解dp,px,以及density。
- 求组合数(c(m,n))
- Shell及 子Shell关系
- boost锁使用总结
- 打造属于自己的MVVM框架: 1.什么是MVVM
- java拦截器实例
- 你所访问的站点在微博的认证失败 21322
- 运用2to3.py将python2脚本转换为python3脚本
- Web 服务器配置和管理
- java入门之overload和override
- android5.1+获取当前运行的app(Android5.1-也支持)
- Oracle控制文件
- iOS开发之常用知识储备与开发技巧总结篇
- 安装Vmtools的时候出现的问题