Commit 05fe6b85083151db7f839cc7797235bfbf624595
1 parent
120f0d36ab
Exists in
master
设置用户默认值,增加MD5方法类
Showing 3 changed files with 77 additions and 4 deletions
center.manager/src/main/java/com/lyms/cm/service/sys/impl/SysUsersServiceImpl.java
View file @
05fe6b8
| ... | ... | @@ -10,6 +10,8 @@ |
| 10 | 10 | import com.lyms.cm.entity.sys.SysUsers; |
| 11 | 11 | import com.lyms.cm.enums.UserStatusEnum; |
| 12 | 12 | import com.lyms.cm.service.sys.ISysUsersService; |
| 13 | +import com.lyms.util.MD5Utils; | |
| 14 | +import com.lyms.util.StrUtils; | |
| 13 | 15 | |
| 14 | 16 | /** |
| 15 | 17 | * <p> |
| 16 | 18 | |
| 17 | 19 | |
| ... | ... | @@ -25,16 +27,37 @@ |
| 25 | 27 | @Autowired |
| 26 | 28 | private SysUsersMapper userMapper; |
| 27 | 29 | |
| 30 | + /** | |
| 31 | + * 系统默认密码 | |
| 32 | + */ | |
| 33 | + private static final String DEFAULT_PWD = "123456"; | |
| 34 | + | |
| 28 | 35 | // 模块基础操作信息 |
| 29 | 36 | private boolean empty(SysUsers user) { |
| 30 | 37 | return user == null; |
| 31 | 38 | } |
| 32 | 39 | |
| 40 | + /** | |
| 41 | + * 初始密码设置 | |
| 42 | + */ | |
| 43 | + private void initPwd(SysUsers user) { | |
| 44 | + if (empty(user)) | |
| 45 | + return; | |
| 46 | + String pwd = user.getPwd(); | |
| 47 | + pwd = DEFAULT_PWD; | |
| 48 | + String encode = MD5Utils.md5(pwd); | |
| 49 | + user.setPwd(encode); | |
| 50 | + } | |
| 51 | + | |
| 33 | 52 | @Override |
| 34 | 53 | @Transactional |
| 35 | 54 | public boolean addUser(SysUsers user) { |
| 36 | 55 | if (empty(user)) |
| 37 | 56 | return false; |
| 57 | + // 设置user ID | |
| 58 | + user.setId(StrUtils.uuid()); | |
| 59 | + // 设置用户密码 | |
| 60 | + this.initPwd(user); | |
| 38 | 61 | |
| 39 | 62 | Integer tag = userMapper.insert(user); |
| 40 | 63 | return tag != null && tag >= 1; |
center.manager/src/test/java/center/manager/test/user/UserTest.java
View file @
05fe6b8
| 1 | 1 | package center.manager.test.user; |
| 2 | 2 | |
| 3 | -import java.util.UUID; | |
| 3 | +import java.util.Date; | |
| 4 | 4 | |
| 5 | 5 | import org.junit.Assert; |
| 6 | 6 | import org.junit.Before; |
| 7 | 7 | |
| ... | ... | @@ -9,12 +9,11 @@ |
| 9 | 9 | import org.springframework.beans.factory.annotation.Autowired; |
| 10 | 10 | import org.springframework.test.context.ContextConfiguration; |
| 11 | 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 12 | -import org.springframework.transaction.annotation.Transactional; | |
| 13 | 12 | |
| 14 | 13 | import com.lyms.cm.entity.sys.SysUsers; |
| 15 | 14 | import com.lyms.cm.service.sys.ISysUsersService; |
| 16 | 15 | |
| 17 | -@Transactional | |
| 16 | +//@Transactional | |
| 18 | 17 | @RunWith(SpringJUnit4ClassRunner.class) |
| 19 | 18 | @ContextConfiguration(locations = { "classpath:app-context.xml" }) |
| 20 | 19 | public class UserTest { |
| 21 | 20 | |
| 22 | 21 | |
| 23 | 22 | |
| ... | ... | @@ -27,15 +26,32 @@ |
| 27 | 26 | @Before |
| 28 | 27 | public void init() { |
| 29 | 28 | user = new SysUsers(); |
| 30 | - user.setId(UUID.randomUUID().toString().replaceAll("-", "")); | |
| 29 | + // user.setId(UUID.randomUUID().toString().replaceAll("-", "")); | |
| 31 | 30 | user.setName("欧阳拉闸"); |
| 31 | + user.setExpirTime(new Date()); | |
| 32 | 32 | user.setPhone("18682531229"); |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | @Test |
| 36 | 36 | public void addUserTest() { |
| 37 | 37 | boolean tag = userService.addUser(user); |
| 38 | + System.out.println(tag); | |
| 38 | 39 | Assert.assertTrue("添加成功", tag); |
| 40 | + } | |
| 41 | + | |
| 42 | + @Test | |
| 43 | + public void updateUserTest() { | |
| 44 | + | |
| 45 | + SysUsers user = userService.getUserById("6204903E7E1741688350349EF920ED88"); | |
| 46 | + user.setName("hellow"); | |
| 47 | + boolean tag = userService.updateUser(user); | |
| 48 | + System.out.println(tag); | |
| 49 | + } | |
| 50 | + | |
| 51 | + @Test | |
| 52 | + public void enableUserTest() { | |
| 53 | + boolean tag = userService.enabled("6204903E7E1741688350349EF920ED88"); | |
| 54 | + System.out.println(tag); | |
| 39 | 55 | } |
| 40 | 56 | |
| 41 | 57 | } |
core.sdk/src/main/java/com/lyms/util/MD5Utils.java
View file @
05fe6b8
| 1 | +package com.lyms.util; | |
| 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 | +} |