自定义注解的使用(将request参数中的数据解释成对象)
来源:互联网 发布:html5抓娃娃游戏源码 编辑:程序博客网 时间:2024/06/08 11:46
项目中,每个controller方法都需要验证用户登录,这里工程写了一个简单的注解,来将cookie中的用户信息还原成对象
注解类
package com.cheshangma.operation.wx.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.PARAMETER)@Retention(RetentionPolicy.RUNTIME)//@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含//@Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得//@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到@Documentedpublic @interface ObjectConvertAnno { /** * 指定该参数的值是否允许为 null 默认是不允许 Whether the parameter is required. * <p> * Default is <code>true</code>, leading to an exception thrown in case of the parameter missing * in the request. Switch this to <code>false</code> if you prefer a <code>null</value> in case of * the parameter missing. */ boolean required() default true;}
然后写注解的实现方法
package com.cheshangma.operation.wx.configuration;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.context.annotation.Configuration;import org.springframework.core.MethodParameter;import org.springframework.web.bind.support.WebDataBinderFactory;import org.springframework.web.context.request.NativeWebRequest;import org.springframework.web.method.support.HandlerMethodArgumentResolver;import org.springframework.web.method.support.ModelAndViewContainer;import com.cheshangma.operation.entity.ThirdPartUserEntity;import com.cheshangma.operation.entity.UserEntity;import com.cheshangma.operation.wx.annotation.ObjectConvertAnno;import com.cheshangma.operation.wx.service.ThirdPartUserService;import com.cheshangma.operation.wx.service.UserService;@Configurationpublic class ObjectConvertResolver implements HandlerMethodArgumentResolver, ApplicationContextAware { private static boolean isReloaded = false; private static ApplicationContext applicationContext; private UserService userService; private ThirdPartUserService thirdPartUserService; /** * *判断是否支持要转换的参数类型 * */ @Override public boolean supportsParameter(MethodParameter parameter) { return parameter.getParameterAnnotation(ObjectConvertAnno.class) != null; } /** *当supportsParameter返回true,支持后进行相应的转换 * */ @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { Object[] paramAnnos = parameter.getParameterAnnotations(); if (paramAnnos == null || paramAnnos.length == 0) { return new Object(); } for (Object anno : paramAnnos) { if (!ObjectConvertAnno.class.isInstance(anno)) { continue; } ObjectConvertAnno objectConvertAnno = (ObjectConvertAnno) anno; Object object = execute(parameter, webRequest); if (objectConvertAnno.required() && object == null) { return null; } return object; } return new Object(); } private Object execute(MethodParameter methodParameter, NativeWebRequest webRequest) { Class<?> paramType = methodParameter.getParameterType(); if (paramType.equals(UserEntity.class)) { return getCurrentUser((HttpServletRequest) webRequest.getNativeRequest()); } else if (paramType.equals(ThirdPartUserEntity.class)) { return getCurrentThirdPartUser((HttpServletRequest) webRequest.getNativeRequest()); } return null; } /** * 获取当前用户 * * @return */ private UserEntity getCurrentUser(HttpServletRequest request) { synchronized (ObjectConvertResolver.class) { while (!isReloaded) { try { ObjectConvertResolver.class.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // TODO 这里写业务 userService = ObjectConvertResolver.applicationContext .getBean(com.cheshangma.operation.wx.service.UserService.class); return userService.getWeChatCurrentUser(request); } } /** * 获取当前登陆的第三方用户 * * @return */ private ThirdPartUserEntity getCurrentThirdPartUser(HttpServletRequest request) { synchronized (ObjectConvertResolver.class) { while (!isReloaded) { try { ObjectConvertResolver.class.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // TODO 这里写业务 thirdPartUserService = ObjectConvertResolver.applicationContext .getBean(com.cheshangma.operation.wx.service.ThirdPartUserService.class); return thirdPartUserService.getWeChatCurrentUser(request); } } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { synchronized (ObjectConvertResolver.class) { ObjectConvertResolver.applicationContext = applicationContext; isReloaded = true; ObjectConvertResolver.class.notifyAll(); } }}
这里的thirdPartUserService.getWeChatCurrentUser(request);和userService.getWeChatCurrentUser(request);实现差不多,就贴一个的
@Override public UserEntity getWeChatCurrentUser(HttpServletRequest request) { UserEntity user = null; Cookie cookie = CookieUtils.getCookie(request, PRINCIPALCOOKIE_NAME); if (cookie == null) { return null; } String cookieValue = cookie.getValue(); LOG.info("cookie="+cookie.getName()+"="+cookieValue); if (cookieValue == null) { return null; } String[] values = cookieValue.split(SPLIT); String id = values[0]; LOG.info("userid:"+id); String cookieSHAValue = values[1]; user = userRepository.findOne(id); if (user == null) { return null; } String summary = null; try { summary = SHACoder.encryptShaB64(user.getId(),user.getId()); } catch (Exception e) { LOG.error(e.getMessage()); return null; } if (summary == null || !summary.equals(cookieSHAValue)) { return null; } else { return user; } }
这样就可以了.
阅读全文
0 0
- 自定义注解的使用(将request参数中的数据解释成对象)
- 将json数据映射成对象的处理办法
- 将JSON数据转换成对象
- request 转换成对象
- BaseMode 数据的封装成对象
- Sharp-ORM 自动将数据库封装成对象的生成器,三层数据访问结构
- javascript 字符串原型封装将url里的数据转换成对象
- Python 通过 BeautifulSoup 将 html 中的表格解析成对象
- 使用XStream将XML转化成对象,忽略没有关联的属性
- 使用JSONObject将json文件转化成对象
- 将Url解析成对象
- 将网址转换成对象
- 如何将PHP的数组转化成对象
- 如何将PHP的数组转化成对象
- 将属性的XML解析成对象数组列表
- 一个通用的方法(将数据库记录转化成对象)
- 如何将PHP的数组转化成对象
- NSNumber[将基础的数据类型存储成对象]
- VSCode的快捷键
- Android图表库MPAndroidChart(十)——散点图的孪生兄弟气泡图
- 成员内部类里面为什么不能有静态成员和方法?
- mailchimp for wordpress
- Java算法给定一个整数数组,找出其中两个数相加等于目标值
- 自定义注解的使用(将request参数中的数据解释成对象)
- iOS 使用masonry布局,并实现展示动画
- spark数据倾斜优化
- 22种手机使用中的常见问题及处理方法
- myeclipse写xml文件时,属性值没有自动提示的解决办法
- 15.整理药名
- HyperLogLog-Redis中的基数统计算法
- Mysql主从同步(1)-主从/主主环境部署梳理
- Qt开发-使用QWebEngineView开发一个简易的浏览器(一)