Commit c079aa122b806201df79f52d96f27f4eeffd540c

Authored by jiangjiazhi
1 parent f87e8b5fe3

增加单机版登录

Showing 6 changed files with 100 additions and 78 deletions

platform-common/src/main/java/com/lyms/platform/common/utils/LoginUtil.java View file @ c079aa1
... ... @@ -65,7 +65,7 @@
65 65 NameValuePair[] data = {
66 66 new NameValuePair("account", account),
67 67 new NameValuePair("typeId", typeId),
68   - new NameValuePair("password", org.apache.commons.lang.StringUtils.isBlank(pwd)?"p":md5(pwd).toUpperCase())
  68 + new NameValuePair("password", org.apache.commons.lang.StringUtils.isBlank(pwd)?"p":MD5Utils.md5(pwd).toUpperCase())
69 69 };
70 70 post.setRequestBody(data);
71 71 post.setRequestHeader("Authorization", token);
... ... @@ -139,7 +139,7 @@
139 139 NameValuePair[] data = {
140 140 new NameValuePair("account", account),
141 141 new NameValuePair("typeId", typeId),
142   - new NameValuePair("password", org.apache.commons.lang.StringUtils.isBlank(pwd)?"p":md5(pwd).toUpperCase())
  142 + new NameValuePair("password", org.apache.commons.lang.StringUtils.isBlank(pwd)?"p":MD5Utils.md5(pwd).toUpperCase())
143 143 };
144 144 post.setRequestBody(data);
145 145 post.setRequestHeader("Authorization", token);
... ... @@ -275,31 +275,6 @@
275 275 return loginState;
276 276 }
277 277  
278   -
279   - public static String md5(String plainText) {
280   - try {
281   - if (plainText == null) {
282   - return null;
283   - }
284   - MessageDigest md = MessageDigest.getInstance("MD5");
285   - md.update(plainText.getBytes());
286   - byte b[] = md.digest();
287   - int i;
288   - StringBuffer buf = new StringBuffer("");
289   - for (int offset = 0; offset < b.length; offset++) {
290   - i = b[offset];
291   - if (i < 0)
292   - i += 256;
293   - if (i < 16)
294   - buf.append("0");
295   - buf.append(Integer.toHexString(i));
296   - }
297   - return buf.toString().toUpperCase();
298   - } catch (NoSuchAlgorithmException e) {
299   - e.printStackTrace();
300   - return null;
301   - }
302   - }
303 278 //
304 279 // public static void main(String[] args) {
305 280 // System.out.println(md5("123789456"));
platform-common/src/main/java/com/lyms/platform/common/utils/MD5Utils.java View file @ c079aa1
  1 +package com.lyms.platform.common.utils;
  2 +
  3 +import java.security.MessageDigest;
  4 +import java.security.NoSuchAlgorithmException;
  5 +
  6 +/**
  7 + * Created by Administrator on 2016/6/3 0003.
  8 + */
  9 +public class MD5Utils {
  10 + public static String md5(String plainText) {
  11 + try {
  12 + if (plainText == null) {
  13 + return null;
  14 + }
  15 + MessageDigest md = MessageDigest.getInstance("MD5");
  16 + md.update(plainText.getBytes());
  17 + byte b[] = md.digest();
  18 + int i;
  19 + StringBuffer buf = new StringBuffer("");
  20 + for (int offset = 0; offset < b.length; offset++) {
  21 + i = b[offset];
  22 + if (i < 0)
  23 + i += 256;
  24 + if (i < 16)
  25 + buf.append("0");
  26 + buf.append(Integer.toHexString(i));
  27 + }
  28 + return buf.toString().toUpperCase();
  29 + } catch (NoSuchAlgorithmException e) {
  30 + e.printStackTrace();
  31 + return null;
  32 + }
  33 + }
  34 +}
platform-operate-api/src/main/java/com/lyms/platform/operate/web/controller/UsersController.java View file @ c079aa1
... ... @@ -12,6 +12,8 @@
12 12 import com.lyms.platform.common.utils.*;
13 13 import com.lyms.platform.operate.web.facade.AccessPermissionFacade;
14 14 import com.lyms.platform.operate.web.result.FrontEndResult;
  15 +import com.lyms.platform.operate.web.session.SessionProvider;
  16 +import com.lyms.platform.operate.web.session.strategy.ISessionProvider;
15 17 import com.lyms.platform.permission.model.*;
16 18 import com.lyms.platform.permission.service.*;
17 19 import org.apache.commons.collections.CollectionUtils;
... ... @@ -60,6 +62,8 @@
60 62 private PermissionsService permissionsService;
61 63 @Autowired
62 64 private AccessPermissionFacade accessPermissionFacade;
  65 + @Autowired
  66 + private SessionProvider iSessionProvider;
63 67  
64 68 private static final String LYMS = "龙源美生";
65 69  
66 70  
... ... @@ -254,12 +258,13 @@
254 258  
255 259 LoginContext loginContext = new LoginContext();
256 260 loginContext.setErrormsg("服务器内部错误!");
257   - if(null != code) {
  261 + loginContext= iSessionProvider.login(account,password,code);
  262 + /* if(null != code) {
258 263 loginContext =LoginUtil.loginByPhone(account, code, typeId, token);
259 264 }
260 265 if(null != password) {
261 266 loginContext = LoginUtil.loginByAccount(account, password, typeId, token);
262   - }
  267 + }*/
263 268  
264 269 if(null != loginContext && loginContext.getErrorcode().equals(0)) {
265 270 Users users1 = new Users();
... ... @@ -698,7 +703,7 @@
698 703 users.setAccount(account);
699 704 users.setPhone(phone);
700 705 users.setEnable(enable);
701   - users.setPwd(password);
  706 + users.setPwd(MD5Utils.md5(password));
702 707 users.setRemarks(remarks);
703 708 users.setKsId(ksId);
704 709 users.setPublishId(user.getId());
705 710  
706 711  
... ... @@ -713,11 +718,12 @@
713 718  
714 719 //关联登录中心ID
715 720 if(StringUtils.isNotBlank(phone) || StringUtils.isNotBlank(account)) {
716   - String pwd = null;
  721 + /**/ String pwd = null;
717 722 if(StringUtils.isNotBlank(password)) {
718   - pwd = LoginUtil.md5(password).toUpperCase();
  723 + pwd = MD5Utils.md5(password).toUpperCase();
719 724 }
720   - loginContext = LoginUtil.register(null, phone,account, pwd, token, typeId);
  725 +// loginContext = LoginUtil.register(null, phone,account, pwd, token, typeId);
  726 + loginContext= iSessionProvider.register(null,phone,account, pwd);
721 727 }
722 728  
723 729 //0 成功 4009 已注册
... ... @@ -746,7 +752,6 @@
746 752 ResultUtils.buildSuccessResultAndWrite(response);
747 753 } else {
748 754 ResultUtils.buildResultAndWrite(response, ErrorCodeConstants.SYSTEM_ERROR, loginContext.getErrormsg());
749   - return;
750 755 }
751 756 }
752 757 }
... ... @@ -847,7 +852,7 @@
847 852  
848 853  
849 854  
850   - LoginContext loginContext = LoginUtil.register(users.getLogincenterId().toString(), phone, account, LoginUtil.md5(password).toUpperCase(), token, typeId);
  855 + LoginContext loginContext = LoginUtil.register(users.getLogincenterId().toString(), phone, account, MD5Utils.md5(password).toUpperCase(), token, typeId);
851 856 //0 成功 4009 已注册
852 857 if(loginContext.getErrorcode().equals(0) || loginContext.getErrorcode().equals(4009)) {
853 858 users.setPhone(phone);
... ... @@ -1032,7 +1037,7 @@
1032 1037 users.setPwd(defaultPwd);
1033 1038 users.setModified(new Date());
1034 1039 usersService.updateUsers(users);
1035   - LoginContext loginContext = LoginUtil.register(users.getLogincenterId().toString(), users.getPhone(),users.getAccount(), LoginUtil.md5(defaultPwd).toUpperCase(), token, typeId);
  1040 + LoginContext loginContext = LoginUtil.register(users.getLogincenterId().toString(), users.getPhone(),users.getAccount(), MD5Utils.md5(defaultPwd).toUpperCase(), token, typeId);
1036 1041 if(loginContext.getErrorcode().equals(0)) {
1037 1042 ResultUtils.buildSuccessResultAndWrite(response);
1038 1043 } else {
platform-operate-api/src/main/java/com/lyms/platform/operate/web/inteceptor/TokenValidateInteceptor.java View file @ c079aa1
... ... @@ -5,12 +5,9 @@
5 5 import com.lyms.platform.common.base.BaseController;
6 6 import com.lyms.platform.common.base.ContextHolder;
7 7 import com.lyms.platform.common.base.LoginContext;
8   -import com.lyms.platform.common.exception.ParameterException;
9 8 import com.lyms.platform.common.exception.TokenException;
10 9 import com.lyms.platform.common.utils.LogUtil;
11   -import com.lyms.platform.common.utils.LoginUtil;
12   -import com.lyms.platform.permission.model.Users;
13   -import com.lyms.platform.permission.service.TokenService;
  10 +import com.lyms.platform.operate.web.session.SessionProvider;
14 11 import com.lyms.platform.permission.service.UsersService;
15 12 import org.apache.commons.lang.StringUtils;
16 13 import org.springframework.beans.factory.annotation.Autowired;
17 14  
18 15  
19 16  
20 17  
21 18  
... ... @@ -20,22 +17,21 @@
20 17 import javax.servlet.http.HttpServletRequest;
21 18 import javax.servlet.http.HttpServletResponse;
22 19 import java.lang.annotation.Annotation;
23   -import java.util.Date;
24 20  
25 21 /**
26 22 * 验证token拦截器
27   - *
  23 + * <p>
28 24 * <ul>
29   - * <li>
30   - * 1、springmvc中配置TokenValidateInteceptor的拦截
31   - * 2、在需要拦截的方法上面配置TokenRequired注解
32   - * </li>
  25 + * <li>
  26 + * 1、springmvc中配置TokenValidateInteceptor的拦截
  27 + * 2、在需要拦截的方法上面配置TokenRequired注解
  28 + * </li>
33 29 * </ul>
34   - *
35 30 */
36 31 public class TokenValidateInteceptor extends HandlerInterceptorAdapter {
  32 +
37 33 @Autowired
38   - private UsersService usersService;
  34 + private SessionProvider sessionProvider;
39 35  
40 36 public static boolean isSiteController(Object handler) {
41 37 return handler instanceof HandlerMethod && (((HandlerMethod) handler).getBean() instanceof BaseController);
42 38  
43 39  
44 40  
45 41  
... ... @@ -67,22 +63,23 @@
67 63 if (StringUtils.isEmpty(token)) {
68 64 throw new TokenException();
69 65 }
70   - LoginContext loginContext = LoginUtil.checkLoginState(token);
71   - if(!loginContext.isLogin()) {
  66 + LoginContext loginContext = sessionProvider.checkSession(httpServletRequest, httpServletResponse, token);
  67 + /* if (!loginContext.isLogin()) {
72 68 throw new TokenException();
73 69 }
74   - Users users = usersService.getUsersByLoginCenterId(loginContext.getId());
75   - if(null == users) {
  70 + Users users = usersService.getUsersByLoginCenterId(loginContext.getId());
  71 + if (null == users) {
76 72 throw new TokenException();
77 73 }
78 74 loginContext.setId(users.getId());
79 75 loginContext.setToken(token);
80   - httpServletRequest.setAttribute("loginContext", loginContext);
  76 + httpServletRequest.setAttribute("loginContext", loginContext);*/
81 77  
82   - LogUtil.tokenInfo(" userId:" + users.getId() + ", ,url:" + httpServletRequest.getRequestURI() + ",method:" + httpServletRequest.getMethod());
  78 + LogUtil.tokenInfo(" userId:" + loginContext.getId() + ", ,url:" + httpServletRequest.getRequestURI() + ",method:" + httpServletRequest.getMethod());
83 79  
84 80 return loginContext.isLogin();
85 81 }
  82 +
86 83 /**
87 84 * This implementation is empty.
88 85 */
platform-operate-api/src/main/resources/database.properties View file @ c079aa1
... ... @@ -38,4 +38,6 @@
38 38 login.typeId=2
39 39 login.token=265a841b-9bb5-434a-8c2b-e78df86fc45d
40 40 login.defaultPwd=123456
  41 +#1 本地缓存 2 sso登录(需要外网)
  42 +run.mode=1
platform-operate-api/src/main/resources/spring/applicationContext.xml View file @ c079aa1
1 1 <?xml version="1.0" encoding="UTF-8"?>
2 2 <beans xmlns="http://www.springframework.org/schema/beans"
3   - xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
4   - xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5   - xmlns:cache="http://www.springframework.org/schema/cache" xmlns:task="http://www.springframework.org/schema/task"
6   - xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
7   - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
8   - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
9   - http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
10   - http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
  3 + xmlns:context="http://www.springframework.org/schema/context"
  4 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5 + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6 + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
11 7  
12   - <!-- 采用注释的方式配置bean -->
13   - <context:annotation-config />
  8 + <!-- 采用注释的方式配置bean -->
  9 + <context:annotation-config/>
14 10  
15   - <!-- 配置要扫描的包 -->
16   - <context:component-scan base-package="com.lyms.platform.biz.service" />
17   - <!-- 配置要扫描的包 -->
18   - <context:component-scan base-package="com.lyms.platform.biz.dal.impl" />
19   - <context:component-scan base-package="com.lyms.platform.operate.web.facade" />
20   - <context:component-scan base-package="com.lyms.platform.permission.*" />
21   - <!-- 提示信息加载类 -->
22   - <bean class="com.lyms.platform.common.core.resolve.MessageResolver">
23   - <property name="messageFile" value="message.properties"/>
24   - </bean>
25   - <import resource="classpath:/spring/applicationContext_biz_patient.xml"/>
26   - <import resource="classpath:/spring/spring-mongodb.xml"/>
27   - <import resource="classpath:/spring/applicationContext-dal.xml"/>
  11 + <!-- 配置要扫描的包 -->
  12 + <context:component-scan base-package="com.lyms.platform.biz.service"/>
  13 + <!-- 配置要扫描的包 -->
  14 + <context:component-scan base-package="com.lyms.platform.biz.dal.impl"/>
  15 + <context:component-scan base-package="com.lyms.platform.operate.web.facade"/>
  16 + <context:component-scan base-package="com.lyms.platform.permission.*"/>
  17 + <!-- 提示信息加载类 -->
  18 + <bean class="com.lyms.platform.common.core.resolve.MessageResolver">
  19 + <property name="messageFile" value="message.properties"/>
  20 + </bean>
  21 + <import resource="classpath:/spring/applicationContext_biz_patient.xml"/>
  22 + <import resource="classpath:/spring/spring-mongodb.xml"/>
  23 + <import resource="classpath:/spring/applicationContext-dal.xml"/>
  24 +
  25 + <bean id="sessionProvider" class="com.lyms.platform.operate.web.session.SessionProvider">
  26 + <property name="iSessionProviderMap">
  27 + <map>
  28 + <entry key="1" value-ref="localSession"/>
  29 + <entry key="2" value-ref="usSession"/>
  30 + </map>
  31 + </property>
  32 + <property name="defaultSessionProvider" ref="localSession"/>
  33 + <property name="currentStrateger" value="${run.mode}"/>
  34 + </bean>
  35 + <bean id="localSession" class="com.lyms.platform.operate.web.session.strategy.LocalCacheSessionStrategy"/>
  36 + <bean id="usSession" class="com.lyms.platform.operate.web.session.strategy.UserCenterStrategy"/>
28 37 </beans>