Commit f115ef266fe8676a574c6bedf8329213ae9cd974
1 parent
ca170e3e4a
Exists in
master
新增token拦截器,并且增加根据登陆中心id获取用户
Showing 9 changed files with 151 additions and 6 deletions
- mainData/src/main/java/com/lymsh/yimiao/main/data/dao/YmUsersMapper.java
- mainData/src/main/java/com/lymsh/yimiao/main/data/service/YmUsersService.java
- mainData/src/main/java/com/lymsh/yimiao/main/data/service/impl/YmUsersServiceImpl.java
- mainData/src/main/java/com/lymsh/yimiao/main/data/util/ImageUtil.java
- mainData/src/main/resources/mainOrm/MedInoculateorder.xml
- mainData/src/main/resources/mainOrm/YmUsers.xml
- webApi/src/main/java/com/lyms/yimiao/web/controller/v1/UserController.java
- webApi/src/main/java/com/lyms/yimiao/web/interceptor/TokenValidateInteceptor.java
- webApi/src/main/resources/spring/applicationContext-mvc.xml
mainData/src/main/java/com/lymsh/yimiao/main/data/dao/YmUsersMapper.java
View file @
f115ef2
mainData/src/main/java/com/lymsh/yimiao/main/data/service/YmUsersService.java
View file @
f115ef2
... | ... | @@ -5,7 +5,20 @@ |
5 | 5 | |
6 | 6 | import java.util.List; |
7 | 7 | |
8 | -public interface YmUsersService {public void addYmUsers(YmUsers obj);public void updateYmUsers(YmUsers obj);public void deleteYmUsers (Integer id);public YmUsers getYmUsers (Integer id);public int queryYmUsersCount (YmUsersQuery query);public List<YmUsers> queryYmUsers (YmUsersQuery query); | |
8 | +public interface YmUsersService { | |
9 | + public void addYmUsers(YmUsers obj); | |
10 | + | |
11 | + public void updateYmUsers(YmUsers obj); | |
12 | + | |
13 | + public void deleteYmUsers(Integer id); | |
14 | + | |
15 | + public YmUsers getYmUsers(Integer id); | |
16 | + | |
17 | + public int queryYmUsersCount(YmUsersQuery query); | |
18 | + | |
19 | + public List<YmUsers> queryYmUsers(YmUsersQuery query); | |
20 | + | |
21 | + public YmUsers getUsersByLoginCenterId(Integer id); | |
9 | 22 | |
10 | 23 | } |
mainData/src/main/java/com/lymsh/yimiao/main/data/service/impl/YmUsersServiceImpl.java
View file @
f115ef2
mainData/src/main/java/com/lymsh/yimiao/main/data/util/ImageUtil.java
View file @
f115ef2
1 | +package com.lymsh.yimiao.main.data.util; | |
2 | + | |
3 | +/** | |
4 | + * Created by Administrator on 2016/5/2 0002. | |
5 | + */ | |
6 | +public class ImageUtil { | |
7 | + | |
8 | + //默认用户头像 | |
9 | + public static final String DEFAULTUSERAVATAR = "FsfCs4mlJeszthuOTyX9FiH-dGlA"; | |
10 | + //默认宝宝头像 | |
11 | + public static final String DEFAULTBABYAVATAR = ""; | |
12 | + | |
13 | +} |
mainData/src/main/resources/mainOrm/MedInoculateorder.xml
View file @
f115ef2
... | ... | @@ -13,7 +13,7 @@ |
13 | 13 | <result column="io_orderTime" property="ioOrderTime" jdbcType="VARCHAR" /> |
14 | 14 | </resultMap> |
15 | 15 | |
16 | -<select id="getMedInoculateOrder" resultMap="MedInoculateorderResultMap" parameterType="java.lang.String"> | |
16 | +<select id="getMedInoculateOrder" resultMap="MedInoculateOrderResultMap" parameterType="java.lang.String"> | |
17 | 17 | select io_id,io_kidId,io_vaccineId,io_organizationId,io_inoculateDate,io_inoculateTime,io_orderOper,io_orderTime |
18 | 18 | FROM med_inoculateorder |
19 | 19 | WHERE io_id = #{ioId,jdbcType=VARCHAR} |
mainData/src/main/resources/mainOrm/YmUsers.xml
View file @
f115ef2
... | ... | @@ -68,6 +68,13 @@ |
68 | 68 | from ym_users where id = #{id,jdbcType=INTEGER} |
69 | 69 | </select> |
70 | 70 | |
71 | +<select id="getUsersByLoginCenterId" resultMap="YmUsersResultMap" parameterType="java.lang.Integer"> | |
72 | + SELECT id,phone,avatar,nickname,yn,created,modified,last_login_time,login_center_id | |
73 | + FROM ym_users | |
74 | + WHERE login_center_id = #{loginCenterId,jdbcType=INTEGER} | |
75 | +</select> | |
76 | + | |
77 | + | |
71 | 78 | |
72 | 79 | <sql id="orderAndLimit"> |
73 | 80 | <if test="sort != null and sort != '' "> |
webApi/src/main/java/com/lyms/yimiao/web/controller/v1/UserController.java
View file @
f115ef2
... | ... | @@ -2,7 +2,7 @@ |
2 | 2 | |
3 | 3 | import com.lymsh.mommybaby.basecommon.base.BaseController; |
4 | 4 | import com.lymsh.mommybaby.basecommon.base.TokenRequired; |
5 | -import com.lymsh.mommybaby.basecommon.context.MommyBobyContext; | |
5 | +import com.lymsh.mommybaby.basecommon.service.TokenService; | |
6 | 6 | import com.lymsh.mommybaby.basecommon.util.ContextHolder; |
7 | 7 | import com.lymsh.mommybaby.basecommon.util.JsonUtil; |
8 | 8 | import com.lymsh.mommybaby.basecommon.util.QiniuUtil; |
... | ... | @@ -11,6 +11,7 @@ |
11 | 11 | import com.lymsh.yimiao.main.data.model.YmUsers; |
12 | 12 | import com.lymsh.yimiao.main.data.model.YmUsersQuery; |
13 | 13 | import com.lymsh.yimiao.main.data.service.YmUsersService; |
14 | +import com.lymsh.yimiao.main.data.util.ImageUtil; | |
14 | 15 | import com.lymsh.yimiao.main.data.util.LoginContext; |
15 | 16 | import com.lymsh.yimiao.main.data.util.LoginUtil; |
16 | 17 | import com.lymsh.yimiao.main.data.util.PhoneUtil; |
... | ... | @@ -22,6 +23,7 @@ |
22 | 23 | import org.springframework.web.bind.annotation.RequestMethod; |
23 | 24 | import org.springframework.web.bind.annotation.RequestParam; |
24 | 25 | |
26 | +import javax.servlet.http.HttpServletRequest; | |
25 | 27 | import javax.servlet.http.HttpServletResponse; |
26 | 28 | import java.util.Date; |
27 | 29 | import java.util.HashMap; |
... | ... | @@ -45,6 +47,8 @@ |
45 | 47 | private String typeId; |
46 | 48 | @Autowired |
47 | 49 | private YmUsersService usersService; |
50 | + @Autowired | |
51 | + private TokenService tokenService; | |
48 | 52 | |
49 | 53 | /** |
50 | 54 | * 发送验证码 |
51 | 55 | |
52 | 56 | |
... | ... | @@ -145,14 +149,18 @@ |
145 | 149 | */ |
146 | 150 | @RequestMapping(value = "/userInfo", method = RequestMethod.GET) |
147 | 151 | @TokenRequired |
148 | - public void getUserInfo(HttpServletResponse response){ | |
152 | + public void getUserInfo(HttpServletResponse response,HttpServletRequest request){ | |
153 | + | |
154 | + LoginContext loginContext = (LoginContext) request.getAttribute("loginContext"); | |
155 | + | |
156 | + System.out.println("+++++++++++++++"+loginContext.getId()); | |
149 | 157 | //取到当前登陆用户的id |
150 | - YmUsers users = usersService.getYmUsers(ContextHolder.getContext().getUserId()); | |
158 | + YmUsers users = usersService.getYmUsers(loginContext.getId()); | |
151 | 159 | Map<String,Object> map = new HashMap<>(); |
152 | 160 | //如果没有昵称,返回手机 |
153 | 161 | map.put("nickname",users.getNickname()!=null ? users.getNickname() : users.getPhone()); |
154 | 162 | //如果没有头像,返回默认头像 |
155 | - map.put("avatar", QiniuUtil.buildImg(users.getAvatar()!=null ? users.getAvatar() : "默认头像")); | |
163 | + map.put("avatar", QiniuUtil.buildImg(users.getAvatar()!=null ? users.getAvatar() : ImageUtil.DEFAULTUSERAVATAR)); | |
156 | 164 | |
157 | 165 | writeJson(response, JsonUtil.obj2JsonString(map)); |
158 | 166 | } |
webApi/src/main/java/com/lyms/yimiao/web/interceptor/TokenValidateInteceptor.java
View file @
f115ef2
1 | +package com.lyms.yimiao.web.interceptor; | |
2 | + | |
3 | + | |
4 | +import com.lymsh.mommybaby.basecommon.base.BaseController; | |
5 | +import com.lymsh.mommybaby.basecommon.base.TokenRequired; | |
6 | +import com.lymsh.mommybaby.basecommon.exception.TokenException; | |
7 | +import com.lymsh.mommybaby.basecommon.util.ContextHolder; | |
8 | +import com.lymsh.mommybaby.basecommon.util.LogUtil; | |
9 | +import com.lymsh.yimiao.main.data.model.YmUsers; | |
10 | +import com.lymsh.yimiao.main.data.service.YmUsersService; | |
11 | +import com.lymsh.yimiao.main.data.util.LoginContext; | |
12 | +import com.lymsh.yimiao.main.data.util.LoginUtil; | |
13 | +import org.apache.commons.lang.StringUtils; | |
14 | +import org.springframework.beans.factory.annotation.Autowired; | |
15 | +import org.springframework.web.method.HandlerMethod; | |
16 | +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | |
17 | + | |
18 | +import javax.servlet.http.HttpServletRequest; | |
19 | +import javax.servlet.http.HttpServletResponse; | |
20 | +import java.lang.annotation.Annotation; | |
21 | + | |
22 | +/** | |
23 | + * 验证token拦截器 | |
24 | + * | |
25 | + * <ul> | |
26 | + * <li> | |
27 | + * 1、springmvc中配置TokenValidateInteceptor的拦截 | |
28 | + * 2、在需要拦截的方法上面配置TokenRequired注解 | |
29 | + * </li> | |
30 | + * </ul> | |
31 | + * | |
32 | + */ | |
33 | +public class TokenValidateInteceptor extends HandlerInterceptorAdapter { | |
34 | + @Autowired | |
35 | + private YmUsersService usersService; | |
36 | + | |
37 | + public static boolean isSiteController(Object handler) { | |
38 | + return handler instanceof HandlerMethod && (((HandlerMethod) handler).getBean() instanceof BaseController); | |
39 | + } | |
40 | + | |
41 | + @Override | |
42 | + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | |
43 | + | |
44 | + if (!isSiteController(handler)) | |
45 | + return true; | |
46 | + TokenRequired clientRequired = findAnnotation((HandlerMethod) handler, TokenRequired.class); | |
47 | + if (null == clientRequired) | |
48 | + return true; | |
49 | + | |
50 | + validateToken(request, response); | |
51 | + | |
52 | + return true; | |
53 | + } | |
54 | + | |
55 | + private <T extends Annotation> T findAnnotation(HandlerMethod handler, Class<T> annotationType) { | |
56 | + T annotation = handler.getBeanType().getAnnotation(annotationType); | |
57 | + if (annotation != null) | |
58 | + return annotation; | |
59 | + return handler.getMethodAnnotation(annotationType); | |
60 | + } | |
61 | + | |
62 | + public boolean validateToken(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { | |
63 | + String token = httpServletRequest.getHeader("Authorization"); | |
64 | + if (StringUtils.isEmpty(token)) { | |
65 | + throw new TokenException(); | |
66 | + } | |
67 | + LoginContext loginContext = LoginUtil.checkLoginState(token); | |
68 | + if(!loginContext.isLogin()) { | |
69 | + throw new TokenException(); | |
70 | + } | |
71 | + YmUsers users = usersService.getUsersByLoginCenterId(loginContext.getId()); | |
72 | + if(null == users) { | |
73 | + throw new TokenException(); | |
74 | + } | |
75 | + loginContext.setId(users.getId()); | |
76 | + loginContext.setToken(token); | |
77 | + httpServletRequest.setAttribute("loginContext", loginContext); | |
78 | + | |
79 | + LogUtil.tokenInfo(" userId:" + users.getId() + ", ,url:" + httpServletRequest.getRequestURI() + ",method:" + httpServletRequest.getMethod()); | |
80 | + | |
81 | + return loginContext.isLogin(); | |
82 | + } | |
83 | + /** | |
84 | + * This implementation is empty. | |
85 | + */ | |
86 | + @Override | |
87 | + public void afterCompletion( | |
88 | + HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) | |
89 | + throws Exception { | |
90 | + ContextHolder.clean(); | |
91 | + } | |
92 | +} |
webApi/src/main/resources/spring/applicationContext-mvc.xml
View file @
f115ef2
... | ... | @@ -11,5 +11,11 @@ |
11 | 11 | <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> |
12 | 12 | </context:component-scan> |
13 | 13 | <mvc:annotation-driven /> |
14 | + | |
15 | + <mvc:interceptors> | |
16 | + <bean id="tokenValidateInteceptor" class="com.lyms.yimiao.web.interceptor.TokenValidateInteceptor"/> | |
17 | + </mvc:interceptors> | |
18 | + | |
19 | + | |
14 | 20 | </beans> |