Authored by zhaoyue

add qrcode related code

@@ -2,7 +2,7 @@ git pull @@ -2,7 +2,7 @@ git pull
2 git add --all src/* 2 git add --all src/*
3 git add push.sh 3 git add push.sh
4 git add pom.xml 4 git add pom.xml
5 -git commit -m "add push" 5 +git commit -m "add qrcode related code"
6 git push origin zhaoyue-dev 6 git push origin zhaoyue-dev
7 git status 7 git status
8 git pull 8 git pull
@@ -23,6 +23,11 @@ public interface Constants { @@ -23,6 +23,11 @@ public interface Constants {
23 int TOKEN_EXPIRES_HOUR = 72; 23 int TOKEN_EXPIRES_HOUR = 72;
24 24
25 /** 25 /**
  26 + * qrcode有效期(小时)
  27 + */
  28 + int QRCODE_EXPIRES_HOUR = 24;
  29 +
  30 + /**
26 * 存放Authorization的header字段 31 * 存放Authorization的header字段
27 */ 32 */
28 String AUTHORIZATION = "authorization"; 33 String AUTHORIZATION = "authorization";
@@ -30,18 +35,18 @@ public interface Constants { @@ -30,18 +35,18 @@ public interface Constants {
30 /** 35 /**
31 * 单项打分标准 36 * 单项打分标准
32 */ 37 */
33 - Map<Integer,XklAmpReportMetaScoreStandardEntity> scoreMap=new HashMap<>(); 38 + Map<Integer, XklAmpReportMetaScoreStandardEntity> scoreMap = new HashMap<>();
34 39
35 /** 40 /**
36 * 大项综合加权标准 41 * 大项综合加权标准
37 */ 42 */
38 - Map<Integer,XklAmpReportCategoryEntity> weightedScoreMap=new HashMap<>(); 43 + Map<Integer, XklAmpReportCategoryEntity> weightedScoreMap = new HashMap<>();
39 44
40 - Map<Integer,XklAmpReportMetaItemsEntity> itemMetaMap = new HashMap<Integer, XklAmpReportMetaItemsEntity>(); 45 + Map<Integer, XklAmpReportMetaItemsEntity> itemMetaMap = new HashMap<Integer, XklAmpReportMetaItemsEntity>();
41 /** 46 /**
42 * 分年龄段平均得分 47 * 分年龄段平均得分
43 */ 48 */
44 - Map<String,Double> aveScoreMap = new HashMap<>(); 49 + Map<String, Double> aveScoreMap = new HashMap<>();
45 50
46 public static final int MALE = 0; 51 public static final int MALE = 0;
47 public static final int FEMALE = 1; 52 public static final int FEMALE = 1;
@@ -55,5 +60,5 @@ public interface Constants { @@ -55,5 +60,5 @@ public interface Constants {
55 60
56 public static final double SMALL_DOUBLE = 0.001; 61 public static final double SMALL_DOUBLE = 0.001;
57 62
58 - 63 + public static final String WEB_LOGIN_URL = "http://testingurl/?qrcode=";
59 } 64 }
@@ -36,7 +36,6 @@ public enum ResultStatus { @@ -36,7 +36,6 @@ public enum ResultStatus {
36 36
37 37
38 38
39 -  
40 /** 39 /**
41 * 返回码 40 * 返回码
42 */ 41 */
  1 +package com.xkl.controller.qrcode;
  2 +
  3 +import com.wordnik.swagger.annotations.ApiImplicitParam;
  4 +import com.wordnik.swagger.annotations.ApiImplicitParams;
  5 +import com.wordnik.swagger.annotations.ApiOperation;
  6 +import com.xkl.authorization.annotation.Authorization;
  7 +import com.xkl.authorization.annotation.CurrentUser;
  8 +import com.xkl.authorization.annotation.LogAnnotation;
  9 +import com.xkl.authorization.annotation.Sign;
  10 +import com.xkl.authorization.manager.ITokenManager;
  11 +import com.xkl.authorization.model.TokenModel;
  12 +import com.xkl.config.Constants;
  13 +import com.xkl.config.ResultStatus;
  14 +import com.xkl.domain.User;
  15 +import com.xkl.domain.XklMemberEntity;
  16 +import com.xkl.domain.XklMemberOpenidEntity;
  17 +import com.xkl.model.QrCodeModel;
  18 +import com.xkl.model.ResultModel;
  19 +import com.xkl.model.UsrInfoModel;
  20 +import com.xkl.repository.UserRepository;
  21 +import com.xkl.repository.XklMemberOpenidRespository;
  22 +import com.xkl.repository.XklMemberRespository;
  23 +import com.xkl.security.AntiXSS;
  24 +import com.xkl.service.ILoginService;
  25 +import com.xkl.service.IQRCodeService;
  26 +import com.xkl.tools.DatetimeTools;
  27 +import org.springframework.beans.factory.annotation.Autowired;
  28 +import org.springframework.http.HttpStatus;
  29 +import org.springframework.http.ResponseEntity;
  30 +import org.springframework.util.Assert;
  31 +import org.springframework.web.bind.annotation.RequestMapping;
  32 +import org.springframework.web.bind.annotation.RequestMethod;
  33 +import org.springframework.web.bind.annotation.RequestParam;
  34 +import org.springframework.web.bind.annotation.RestController;
  35 +
  36 +import javax.servlet.http.HttpServletRequest;
  37 +import java.util.Date;
  38 +
  39 +/**
  40 + * Created by zhaoyue on 2017/03/11.
  41 + */
  42 +@RestController
  43 +@RequestMapping("/qrcode")
  44 +public class QRCodeController {
  45 + @Autowired
  46 + private IQRCodeService qrCodeService;
  47 + @Autowired
  48 + private ILoginService loginService;
  49 + @Autowired
  50 + private XklMemberOpenidRespository xklMemberOpenidRespository;
  51 + @Autowired
  52 + private UserRepository userRepository;
  53 + @Autowired
  54 + private XklMemberRespository xklMemberRespository;
  55 +
  56 +
  57 + @LogAnnotation
  58 + @AntiXSS
  59 + @RequestMapping(value = "/getQrWithAccPwd", method = RequestMethod.GET)
  60 + @ApiOperation(value = "使用用户名密码得到qrcode")
  61 + public ResponseEntity<ResultModel> getQrWithAccPwd(HttpServletRequest request, @RequestParam String username, @RequestParam String password, @RequestParam long t) {
  62 + Assert.notNull(username, "username can not be empty");
  63 + Assert.notNull(password, "password can not be empty");
  64 +
  65 + User user = loginService.check(username, password);
  66 + if (user == null) {//用户,密码错误
  67 + return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.OK);
  68 + } else {
  69 + String qrCode = qrCodeService.getQRCodeWithAccount(user.getLoginAccount());
  70 + QrCodeModel qrModel = new QrCodeModel(qrCode);
  71 + return new ResponseEntity<>(ResultModel.ok(qrModel), HttpStatus.OK);
  72 + }
  73 + }
  74 + @LogAnnotation
  75 + @AntiXSS
  76 + @Sign
  77 + @RequestMapping(value = "/getQrWithOpenId", method = RequestMethod.GET)
  78 + @ApiOperation(value = "使用openid获取qrCode")
  79 + public ResponseEntity<ResultModel> getQrWithOpenId(HttpServletRequest request, @RequestParam String openId, @RequestParam int openIdType,
  80 + @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
  81 + if (!(boolean) request.getAttribute("signAspect")) {
  82 + return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);
  83 + }
  84 + XklMemberOpenidEntity openidEntity = xklMemberOpenidRespository.findByOpenidAndType(openId, openIdType);
  85 + // openId 未找到
  86 + if (openidEntity == null) {
  87 + return new ResponseEntity<>(ResultModel.ok(ResultStatus.OPENID_ERROR), HttpStatus.OK);
  88 + }
  89 + User user = userRepository.findOne(openidEntity.getAccountId());
  90 + // 用户不存在
  91 + if (user == null) {
  92 + return new ResponseEntity<>(ResultModel.ok(ResultStatus.USER_NOT_FOUND), HttpStatus.OK);
  93 + }
  94 + String qrCode = qrCodeService.getQRCodeWithAccount(user.getLoginAccount());
  95 + QrCodeModel qrModel = new QrCodeModel(qrCode);
  96 + return new ResponseEntity<>(ResultModel.ok(qrModel), HttpStatus.OK);
  97 + }
  98 +
  99 +
  100 + @LogAnnotation
  101 + @AntiXSS
  102 + @Authorization
  103 + @Sign
  104 + @RequestMapping(value = "/getQrWithToken", method = RequestMethod.GET)
  105 + @ApiOperation(value = "使用token得到qrcode")
  106 + @ApiImplicitParams({
  107 + @ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
  108 + })
  109 + public ResponseEntity<ResultModel> getQrWithToken(HttpServletRequest request, @CurrentUser User user,
  110 + @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
  111 + if (!(boolean) request.getAttribute("signAspect")) {
  112 + return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);
  113 + }
  114 + String qrCode = qrCodeService.getQRCodeWithAccount(user.getLoginAccount());
  115 + QrCodeModel qrModel = new QrCodeModel(qrCode);
  116 + return new ResponseEntity<>(ResultModel.ok(qrModel), HttpStatus.OK);
  117 + }
  118 +
  119 + @LogAnnotation
  120 + @AntiXSS
  121 + @RequestMapping(value = "/getUsrInfoWithQr", method = RequestMethod.GET)
  122 + @ApiOperation(value = "使用qrcode查询个人信息")
  123 + public ResponseEntity<ResultModel> getUsrInfoWithQr(HttpServletRequest request,
  124 + @RequestParam String qrcode, @RequestParam long t) {
  125 + String account = qrCodeService.getAccountWithQRCode(qrcode);
  126 + User user = userRepository.findByLoginAccountAndStatus(account, true);
  127 + if (user == null) {
  128 + return new ResponseEntity<>(ResultModel.error(ResultStatus.USER_NOT_FOUND), HttpStatus.OK);
  129 + }
  130 + XklMemberEntity xklMemberEntity = xklMemberRespository.findOne((long) user.getMemberId());
  131 + int age = DatetimeTools.getAge(xklMemberEntity.getBirthDate(), new Date());
  132 + UsrInfoModel usrInfo = new UsrInfoModel(user.getLoginAccount(), xklMemberEntity.getName(),
  133 + xklMemberEntity.getIdcard(), xklMemberEntity.getPhone(), xklMemberEntity.getSex(), age, qrcode, genWebloginUrl(qrcode));
  134 + return new ResponseEntity<>(ResultModel.ok(usrInfo), HttpStatus.OK);
  135 + }
  136 +
  137 + private static String genWebloginUrl(String qrcode) {
  138 + return Constants.WEB_LOGIN_URL + qrcode.trim();
  139 + }
  140 +
  141 +
  142 +}
@@ -54,4 +54,108 @@ public class XklMemberEntity { @@ -54,4 +54,108 @@ public class XklMemberEntity {
54 @Basic 54 @Basic
55 @Column(name = "status") 55 @Column(name = "status")
56 private Byte status; 56 private Byte status;
  57 +
  58 + public long getId() {
  59 + return id;
  60 + }
  61 +
  62 + public void setId(long id) {
  63 + this.id = id;
  64 + }
  65 +
  66 + public String getName() {
  67 + return name;
  68 + }
  69 +
  70 + public void setName(String name) {
  71 + this.name = name;
  72 + }
  73 +
  74 + public Byte getSex() {
  75 + return sex;
  76 + }
  77 +
  78 + public void setSex(Byte sex) {
  79 + this.sex = sex;
  80 + }
  81 +
  82 + public Date getBirthDate() {
  83 + return birthDate;
  84 + }
  85 +
  86 + public void setBirthDate(Date birthDate) {
  87 + this.birthDate = birthDate;
  88 + }
  89 +
  90 + public String getIdcard() {
  91 + return idcard;
  92 + }
  93 +
  94 + public void setIdcard(String idcard) {
  95 + this.idcard = idcard;
  96 + }
  97 +
  98 + public String getPhone() {
  99 + return phone;
  100 + }
  101 +
  102 + public void setPhone(String phone) {
  103 + this.phone = phone;
  104 + }
  105 +
  106 + public String getRegisterTime() {
  107 + return registerTime;
  108 + }
  109 +
  110 + public void setRegisterTime(String registerTime) {
  111 + this.registerTime = registerTime;
  112 + }
  113 +
  114 + public long getCompanyId() {
  115 + return companyId;
  116 + }
  117 +
  118 + public void setCompanyId(long companyId) {
  119 + this.companyId = companyId;
  120 + }
  121 +
  122 + public long getProvince() {
  123 + return province;
  124 + }
  125 +
  126 + public void setProvince(long province) {
  127 + this.province = province;
  128 + }
  129 +
  130 + public long getCity() {
  131 + return city;
  132 + }
  133 +
  134 + public void setCity(long city) {
  135 + this.city = city;
  136 + }
  137 +
  138 + public long getCountry() {
  139 + return country;
  140 + }
  141 +
  142 + public void setCountry(long country) {
  143 + this.country = country;
  144 + }
  145 +
  146 + public long getRegisterBy() {
  147 + return registerBy;
  148 + }
  149 +
  150 + public void setRegisterBy(long registerBy) {
  151 + this.registerBy = registerBy;
  152 + }
  153 +
  154 + public Byte getStatus() {
  155 + return status;
  156 + }
  157 +
  158 + public void setStatus(Byte status) {
  159 + this.status = status;
  160 + }
57 } 161 }
@@ -27,4 +27,44 @@ public class XklMemberOpenidEntity { @@ -27,4 +27,44 @@ public class XklMemberOpenidEntity {
27 @Basic 27 @Basic
28 @Column(name = "type") 28 @Column(name = "type")
29 private int type; 29 private int type;
  30 +
  31 + public long getId() {
  32 + return id;
  33 + }
  34 +
  35 + public void setId(long id) {
  36 + this.id = id;
  37 + }
  38 +
  39 + public long getAccountId() {
  40 + return accountId;
  41 + }
  42 +
  43 + public void setAccountId(long accountId) {
  44 + this.accountId = accountId;
  45 + }
  46 +
  47 + public long getMemberId() {
  48 + return memberId;
  49 + }
  50 +
  51 + public void setMemberId(long memberId) {
  52 + this.memberId = memberId;
  53 + }
  54 +
  55 + public String getOpenid() {
  56 + return openid;
  57 + }
  58 +
  59 + public void setOpenid(String openid) {
  60 + this.openid = openid;
  61 + }
  62 +
  63 + public int getType() {
  64 + return type;
  65 + }
  66 +
  67 + public void setType(int type) {
  68 + this.type = type;
  69 + }
30 } 70 }
  1 +package com.xkl.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +/**
  6 + * QrCode信息
  7 + */
  8 +@Data
  9 +public class QrCodeModel {
  10 +
  11 + public String qrcode;
  12 +
  13 +
  14 + public QrCodeModel(String qrcode) {
  15 + this.qrcode = qrcode;
  16 + }
  17 +
  18 +
  19 + public String getQrcode() {
  20 + return qrcode;
  21 + }
  22 +
  23 + public void setQrcode(String qrcode) {
  24 + this.qrcode = qrcode;
  25 + }
  26 +}
  1 +package com.xkl.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +/**
  6 + * QrCode信息
  7 + */
  8 +@Data
  9 +public class UsrInfoModel {
  10 + private String account;
  11 + private String name;
  12 + private String id_cardnum;
  13 + private String phone;
  14 + private int sex;
  15 + private int age;
  16 + private String qrcode;
  17 + private String webloginUrl;
  18 +
  19 + public UsrInfoModel(String account, String name, String id_cardnum, String phone, int sex, int age, String qrcode,String webloginUrl) {
  20 + this.account = account;
  21 + this.name = name;
  22 + this.id_cardnum = id_cardnum;
  23 + this.phone = phone;
  24 + this.sex = sex;
  25 + this.age = age;
  26 + this.qrcode = qrcode;
  27 + this.webloginUrl = webloginUrl;
  28 + }
  29 +
  30 + public UsrInfoModel(String account, String name, String id_cardnum, String phone, int sex, int age, String qrcode) {
  31 + this.account = account;
  32 + this.name = name;
  33 + this.id_cardnum = id_cardnum;
  34 + this.phone = phone;
  35 + this.sex = sex;
  36 + this.age = age;
  37 + this.qrcode = qrcode;
  38 + }
  39 + public String getAccount() {
  40 + return account;
  41 + }
  42 +
  43 + public void setAccount(String account) {
  44 + this.account = account;
  45 + }
  46 +
  47 + public String getName() {
  48 + return name;
  49 + }
  50 +
  51 + public void setName(String name) {
  52 + this.name = name;
  53 + }
  54 +
  55 + public String getId_cardnum() {
  56 + return id_cardnum;
  57 + }
  58 +
  59 + public void setId_cardnum(String id_cardnum) {
  60 + this.id_cardnum = id_cardnum;
  61 + }
  62 +
  63 + public String getPhone() {
  64 + return phone;
  65 + }
  66 +
  67 + public void setPhone(String phone) {
  68 + this.phone = phone;
  69 + }
  70 +
  71 + public int getSex() {
  72 + return sex;
  73 + }
  74 +
  75 + public void setSex(int sex) {
  76 + this.sex = sex;
  77 + }
  78 +
  79 + public int getAge() {
  80 + return age;
  81 + }
  82 +
  83 + public void setAge(int age) {
  84 + this.age = age;
  85 + }
  86 +
  87 + public String getQrcode() {
  88 + return qrcode;
  89 + }
  90 +
  91 + public void setQrcode(String qrcode) {
  92 + this.qrcode = qrcode;
  93 + }
  94 +
  95 + public String getWebloginUrl() {
  96 + return webloginUrl;
  97 + }
  98 +
  99 + public void setWebloginUrl(String webloginUrl) {
  100 + this.webloginUrl = webloginUrl;
  101 + }
  102 +}
  1 +package com.xkl.service;
  2 +
  3 +/**
  4 + * Created by zhaoyue on 2017/3/11.
  5 + */
  6 +public interface IQRCodeService {
  7 + public String getQRCodeWithAccount(String account);
  8 +
  9 + public String getAccountWithQRCode(String qrcode);
  10 +}
  1 +package com.xkl.service;
  2 +
  3 +import com.xkl.config.Constants;
  4 +import com.xkl.tools.DESTools;
  5 +import lombok.extern.apachecommons.CommonsLog;
  6 +import org.springframework.beans.factory.annotation.Autowired;
  7 +import org.springframework.data.redis.core.RedisTemplate;
  8 +import org.springframework.stereotype.Service;
  9 +
  10 +import java.text.SimpleDateFormat;
  11 +import java.util.*;
  12 +import java.util.concurrent.TimeUnit;
  13 +
  14 +/**
  15 + * create by zhaoyue
  16 + * 2017-03-11
  17 + */
  18 +
  19 +@Service
  20 +@CommonsLog
  21 +public class QRCodeServiceImpl implements IQRCodeService, Constants {
  22 + private static final String REDIS_ACC_QR_PREFIX = "acc_qr-";
  23 + private static final String REDIS_QR_ACC_PREFIX = "qr_acc-";
  24 +
  25 + private RedisTemplate<String, String> redis;
  26 +
  27 + @Autowired
  28 + public void setRedis(RedisTemplate redis) {
  29 + this.redis = redis;
  30 + }
  31 +
  32 + @Override
  33 + public String getAccountWithQRCode(String qrcode) {
  34 + String qrAcckey = genQrAccKey(qrcode);
  35 + String account = null;
  36 + if (redis.hasKey(qrAcckey)) {
  37 + account = redis.boundValueOps(qrAcckey).get();
  38 + }
  39 + return account;
  40 + }
  41 +
  42 + @Override
  43 + public String getQRCodeWithAccount(String account) {
  44 + String accQrkey = genAccQrKey(account);
  45 + String qrCodeStr = "";
  46 + if (redis.hasKey(accQrkey)) {
  47 + qrCodeStr = redis.boundValueOps(accQrkey).get();
  48 + } else {
  49 + Date dt = new Date();
  50 + SimpleDateFormat matter1 = new SimpleDateFormat("yyyyMMdd");
  51 + String dateStr = matter1.format(dt);
  52 + try {
  53 + qrCodeStr = DESTools.encrypt(account + dateStr);
  54 + } catch (Exception e) {
  55 + e.printStackTrace();
  56 + }
  57 + String qrAcckey = genQrAccKey(qrCodeStr);
  58 + redis.boundValueOps(qrAcckey).set(account, Constants.QRCODE_EXPIRES_HOUR, TimeUnit.HOURS);
  59 + redis.boundValueOps(accQrkey).set(qrCodeStr, Constants.QRCODE_EXPIRES_HOUR, TimeUnit.HOURS);
  60 + }
  61 + return qrCodeStr;
  62 + }
  63 +
  64 + private static String genAccQrKey(String account) {
  65 + return REDIS_ACC_QR_PREFIX + account;
  66 + }
  67 +
  68 + private static String genQrAccKey(String account) {
  69 + return REDIS_QR_ACC_PREFIX + account;
  70 + }
  71 +
  72 +}
  1 +package com.xkl.tools;
  2 +
  3 +import javax.crypto.Cipher;
  4 +import javax.crypto.SecretKey;
  5 +import javax.crypto.SecretKeyFactory;
  6 +import javax.crypto.spec.DESKeySpec;
  7 +import java.security.SecureRandom;
  8 +import java.util.Random;
  9 +
  10 +/**
  11 + * DES加密解密类.
  12 + */
  13 +public class DESTools {
  14 + /**
  15 + * 加密、解密key.
  16 + */
  17 + private static final String PASSWORD_CRYPT_KEY = "HhiTuqm7lsMVHYD4Bkj2B5jSUh0IKPNk";
  18 +
  19 + private final static String ALGORITHM = "DES";
  20 +
  21 + public static void main(String[] args) throws Exception {
  22 + String orgStr = "202cb962ac59075b964b07152d234b70";
  23 + long start = System.currentTimeMillis();
  24 + for (int i = 0; i < 1000; i++) {
  25 + orgStr = DESTools.getRandomString(20);
  26 + String encrptStr = DESTools.encrypt(orgStr);
  27 + String decrptStr = DESTools.decrypt(encrptStr);
  28 + if (!orgStr.equals(decrptStr)) {
  29 + System.out.println("error!!");
  30 + }
  31 + }
  32 + long end = System.currentTimeMillis();
  33 + System.out.println("time:" + (end - start));
  34 +
  35 + }
  36 +
  37 + /**
  38 + * 生成随机字符串
  39 + *
  40 + * @param length
  41 + * @return
  42 + */
  43 + public static String getRandomString(int length) { //length表示生成字符串的长度
  44 + String base = "abcdefghijklmnopqrstuvwxyz0123456789";
  45 + Random random = new Random();
  46 + StringBuffer sb = new StringBuffer();
  47 + for (int i = 0; i < length; i++) {
  48 + int number = random.nextInt(base.length());
  49 + sb.append(base.charAt(number));
  50 + }
  51 + return sb.toString();
  52 + }
  53 +
  54 + /**
  55 + * 对数据进行DES加密.
  56 + *
  57 + * @param data 待进行DES加密的数据
  58 + * @return 返回经过DES加密后的数据
  59 + * @throws Exception
  60 + * @author <a href="mailto:xiexingxing1121@126.com" mce_href="mailto:xiexingxing1121@126.com">AmigoXie</a>
  61 + * Creation date: 2007-7-31 - 下午12:06:24
  62 + */
  63 + public final static String decrypt(String data) throws Exception {
  64 + return new String(decrypt(hex2byte(data.getBytes()),
  65 + PASSWORD_CRYPT_KEY.getBytes()));
  66 + }
  67 +
  68 + /**
  69 + * 对用DES加密过的数据进行解密.
  70 + *
  71 + * @param data DES加密数据
  72 + * @return 返回解密后的数据
  73 + * @throws Exception
  74 + * @author <a href="mailto:xiexingxing1121@126.com" mce_href="mailto:xiexingxing1121@126.com">AmigoXie</a>
  75 + * Creation date: 2007-7-31 - 下午12:07:54
  76 + */
  77 + public final static String encrypt(String data) throws Exception {
  78 + return byte2hex(encrypt(data.getBytes(), PASSWORD_CRYPT_KEY
  79 + .getBytes()));
  80 + }
  81 +
  82 + /**
  83 + * 用指定的key对数据进行DES加密.
  84 + *
  85 + * @param data 待加密的数据
  86 + * @param key DES加密的key
  87 + * @return 返回DES加密后的数据
  88 + * @throws Exception
  89 + * @author <a href="mailto:xiexingxing1121@126.com" mce_href="mailto:xiexingxing1121@126.com">AmigoXie</a>
  90 + * Creation date: 2007-7-31 - 下午12:09:03
  91 + */
  92 + private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
  93 + // DES算法要求有一个可信任的随机数源
  94 + SecureRandom sr = new SecureRandom();
  95 + // 从原始密匙数据创建DESKeySpec对象
  96 + DESKeySpec dks = new DESKeySpec(key);
  97 + // 创建一个密匙工厂,然后用它把DESKeySpec转换成
  98 + // 一个SecretKey对象
  99 + SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
  100 + SecretKey securekey = keyFactory.generateSecret(dks);
  101 + // Cipher对象实际完成加密操作
  102 + Cipher cipher = Cipher.getInstance(ALGORITHM);
  103 + // 用密匙初始化Cipher对象
  104 + cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
  105 + // 现在,获取数据并加密
  106 + // 正式执行加密操作
  107 + return cipher.doFinal(data);
  108 + }
  109 +
  110 + /**
  111 + * 用指定的key对数据进行DES解密.
  112 + *
  113 + * @param data 待解密的数据
  114 + * @param key DES解密的key
  115 + * @return 返回DES解密后的数据
  116 + * @throws Exception
  117 + * @author <a href="mailto:xiexingxing1121@126.com" mce_href="mailto:xiexingxing1121@126.com">AmigoXie</a>
  118 + * Creation date: 2007-7-31 - 下午12:10:34
  119 + */
  120 + private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
  121 + // DES算法要求有一个可信任的随机数源
  122 + SecureRandom sr = new SecureRandom();
  123 + // 从原始密匙数据创建一个DESKeySpec对象
  124 + DESKeySpec dks = new DESKeySpec(key);
  125 + // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
  126 + // 一个SecretKey对象
  127 + SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
  128 + SecretKey securekey = keyFactory.generateSecret(dks);
  129 + // Cipher对象实际完成解密操作
  130 + Cipher cipher = Cipher.getInstance(ALGORITHM);
  131 + // 用密匙初始化Cipher对象
  132 + cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
  133 + // 现在,获取数据并解密
  134 + // 正式执行解密操作
  135 + return cipher.doFinal(data);
  136 + }
  137 +
  138 + public static byte[] hex2byte(byte[] b) {
  139 + if ((b.length % 2) != 0)
  140 + throw new IllegalArgumentException("长度不是偶数");
  141 + byte[] b2 = new byte[b.length / 2];
  142 + for (int n = 0; n < b.length; n += 2) {
  143 + String item = new String(b, n, 2);
  144 + b2[n / 2] = (byte) Integer.parseInt(item, 16);
  145 + }
  146 + return b2;
  147 + }
  148 +
  149 + public static String byte2hex(byte[] b) {
  150 + String hs = "";
  151 + String stmp = "";
  152 + for (int n = 0; n < b.length; n++) {
  153 + stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
  154 + if (stmp.length() == 1)
  155 + hs = hs + "0" + stmp;
  156 + else
  157 + hs = hs + stmp;
  158 + }
  159 + return hs.toUpperCase();
  160 + }
  161 +}
  1 +package com.xkl.tools;
  2 +
  3 +import java.util.Calendar;
  4 +import java.util.Date;
  5 +
  6 +/**
  7 + * Created by zhaoyue on 2017/3/11.
  8 + */
  9 +public class DatetimeTools {
  10 + public static int getAge(Date start, Date end) {
  11 + Calendar c1 = Calendar.getInstance();
  12 + Calendar c2 = Calendar.getInstance();
  13 + c1.setTime(start);
  14 + c2.setTime(end);
  15 + int year1 = c1.get(Calendar.YEAR);
  16 + int year2 = c2.get(Calendar.YEAR);
  17 + return Math.abs(year1 - year2);
  18 + }
  19 +}