Authored by zhaoyue

add qrcode related code

... ... @@ -2,7 +2,8 @@ git pull
git add --all src/*
git add push.sh
git add pom.xml
git commit -m "add push"
git push origin zhaoyue-dev
git commit -m "add qrcode related code"
git push origin master
#git push origin zhaoyue-dev
git status
git pull
... ...
... ... @@ -24,6 +24,11 @@ public interface Constants {
int TOKEN_EXPIRES_HOUR = 72;
/**
* qrcode有效期(小时)
*/
int QRCODE_EXPIRES_HOUR = 24;
/**
* 存放Authorization的header字段
*/
String AUTHORIZATION = "authorization";
... ... @@ -31,17 +36,18 @@ public interface Constants {
/**
* 单项打分标准
*/
Map<Integer,XklAmpReportMetaScoreStandardEntity> scoreMap=new HashMap<>();
Map<Integer, XklAmpReportMetaScoreStandardEntity> scoreMap = new HashMap<>();
/**
* 大项综合加权标准
*/
Map<Integer,XklAmpReportCategoryEntity> weightedScoreMap=new HashMap<>();
Map<Integer, XklAmpReportCategoryEntity> weightedScoreMap = new HashMap<>();
Map<Integer,XklAmpReportMetaItemsEntity> itemMetaMap = new HashMap<Integer, XklAmpReportMetaItemsEntity>();
Map<Integer, XklAmpReportMetaItemsEntity> itemMetaMap = new HashMap<Integer, XklAmpReportMetaItemsEntity>();
/**
* 分年龄段平均得分
*/
Map<String,Double> aveScoreMap = new HashMap<>();
/**
* 身份证前六位-省市区
... ... @@ -57,6 +63,7 @@ public interface Constants {
*/
String ADMIN_TOKEN_PREFIX = "ADMINTOKENPREFIX";
public static final int MALE = 0;
public static final int FEMALE = 1;
... ... @@ -69,5 +76,5 @@ public interface Constants {
public static final double SMALL_DOUBLE = 0.001;
public static final String WEB_LOGIN_URL = "http://testingurl/?qrcode=";
}
... ...
... ... @@ -36,7 +36,6 @@ public enum ResultStatus {
/**
* 返回码
*/
... ...
package com.xkl.controller.qrcode;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xkl.authorization.annotation.Authorization;
import com.xkl.authorization.annotation.CurrentUser;
import com.xkl.authorization.annotation.LogAnnotation;
import com.xkl.authorization.annotation.Sign;
import com.xkl.authorization.manager.ITokenManager;
import com.xkl.authorization.model.TokenModel;
import com.xkl.config.Constants;
import com.xkl.config.ResultStatus;
import com.xkl.domain.User;
import com.xkl.domain.XklMemberEntity;
import com.xkl.domain.XklMemberOpenidEntity;
import com.xkl.model.QrCodeModel;
import com.xkl.model.ResultModel;
import com.xkl.model.UsrInfoModel;
import com.xkl.repository.UserRepository;
import com.xkl.repository.XklMemberOpenidRespository;
import com.xkl.repository.XklMemberRespository;
import com.xkl.security.AntiXSS;
import com.xkl.service.ILoginService;
import com.xkl.service.IQRCodeService;
import com.xkl.tools.DatetimeTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
/**
* Created by zhaoyue on 2017/03/11.
*/
@RestController
@RequestMapping("/qrcode")
public class QRCodeController {
@Autowired
private IQRCodeService qrCodeService;
@Autowired
private ILoginService loginService;
@Autowired
private XklMemberOpenidRespository xklMemberOpenidRespository;
@Autowired
private UserRepository userRepository;
@Autowired
private XklMemberRespository xklMemberRespository;
@LogAnnotation
@AntiXSS
@RequestMapping(value = "/getQrWithAccPwd", method = RequestMethod.GET)
@ApiOperation(value = "使用用户名密码得到qrcode")
public ResponseEntity<ResultModel> getQrWithAccPwd(HttpServletRequest request, @RequestParam String username, @RequestParam String password, @RequestParam long t) {
Assert.notNull(username, "username can not be empty");
Assert.notNull(password, "password can not be empty");
User user = loginService.check(username, password);
if (user == null) {//用户,密码错误
return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.OK);
} else {
String qrCode = qrCodeService.getQRCodeWithAccount(user.getLoginAccount());
QrCodeModel qrModel = new QrCodeModel(qrCode);
return new ResponseEntity<>(ResultModel.ok(qrModel), HttpStatus.OK);
}
}
@LogAnnotation
@AntiXSS
@Sign
@RequestMapping(value = "/getQrWithOpenId", method = RequestMethod.GET)
@ApiOperation(value = "使用openid获取qrCode")
public ResponseEntity<ResultModel> getQrWithOpenId(HttpServletRequest request, @RequestParam String openId, @RequestParam int openIdType,
@RequestParam String sign, @RequestParam long t, @RequestParam int type) {
if (!(boolean) request.getAttribute("signAspect")) {
return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);
}
XklMemberOpenidEntity openidEntity = xklMemberOpenidRespository.findByOpenidAndType(openId, openIdType);
// openId 未找到
if (openidEntity == null) {
return new ResponseEntity<>(ResultModel.ok(ResultStatus.OPENID_ERROR), HttpStatus.OK);
}
User user = userRepository.findOne(openidEntity.getAccountId());
// 用户不存在
if (user == null) {
return new ResponseEntity<>(ResultModel.ok(ResultStatus.USER_NOT_FOUND), HttpStatus.OK);
}
String qrCode = qrCodeService.getQRCodeWithAccount(user.getLoginAccount());
QrCodeModel qrModel = new QrCodeModel(qrCode);
return new ResponseEntity<>(ResultModel.ok(qrModel), HttpStatus.OK);
}
@LogAnnotation
@AntiXSS
@Authorization
@Sign
@RequestMapping(value = "/getQrWithToken", method = RequestMethod.GET)
@ApiOperation(value = "使用token得到qrcode")
@ApiImplicitParams({
@ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
})
public ResponseEntity<ResultModel> getQrWithToken(HttpServletRequest request, @CurrentUser User user,
@RequestParam String sign, @RequestParam long t, @RequestParam int type) {
if (!(boolean) request.getAttribute("signAspect")) {
return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);
}
String qrCode = qrCodeService.getQRCodeWithAccount(user.getLoginAccount());
QrCodeModel qrModel = new QrCodeModel(qrCode);
return new ResponseEntity<>(ResultModel.ok(qrModel), HttpStatus.OK);
}
@LogAnnotation
@AntiXSS
@RequestMapping(value = "/getUsrInfoWithQr", method = RequestMethod.GET)
@ApiOperation(value = "使用qrcode查询个人信息")
public ResponseEntity<ResultModel> getUsrInfoWithQr(HttpServletRequest request,
@RequestParam String qrcode, @RequestParam long t) {
String account = qrCodeService.getAccountWithQRCode(qrcode);
User user = userRepository.findByLoginAccountAndStatus(account, true);
if (user == null) {
return new ResponseEntity<>(ResultModel.error(ResultStatus.USER_NOT_FOUND), HttpStatus.OK);
}
XklMemberEntity xklMemberEntity = xklMemberRespository.findOne((long) user.getMemberId());
int age = DatetimeTools.getAge(xklMemberEntity.getBirthDate(), new Date());
UsrInfoModel usrInfo = new UsrInfoModel(user.getLoginAccount(), xklMemberEntity.getName(),
xklMemberEntity.getIdcard(), xklMemberEntity.getPhone(), xklMemberEntity.getSex(), age, qrcode, genWebloginUrl(qrcode));
return new ResponseEntity<>(ResultModel.ok(usrInfo), HttpStatus.OK);
}
private static String genWebloginUrl(String qrcode) {
return Constants.WEB_LOGIN_URL + qrcode.trim();
}
}
... ...
... ... @@ -54,4 +54,5 @@ public class XklMemberEntity {
@Basic
@Column(name = "status")
private int status;
}
... ...
... ... @@ -27,4 +27,44 @@ public class XklMemberOpenidEntity {
@Basic
@Column(name = "type")
private int type;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getAccountId() {
return accountId;
}
public void setAccountId(long accountId) {
this.accountId = accountId;
}
public long getMemberId() {
return memberId;
}
public void setMemberId(long memberId) {
this.memberId = memberId;
}
public String getOpenid() {
return openid;
}
public void setOpenid(String openid) {
this.openid = openid;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
... ...
package com.xkl.model;
import lombok.Data;
/**
* QrCode信息
*/
@Data
public class QrCodeModel {
public String qrcode;
public QrCodeModel(String qrcode) {
this.qrcode = qrcode;
}
public String getQrcode() {
return qrcode;
}
public void setQrcode(String qrcode) {
this.qrcode = qrcode;
}
}
... ...
package com.xkl.model;
import lombok.Data;
/**
* QrCode信息
*/
@Data
public class UsrInfoModel {
private String account;
private String name;
private String id_cardnum;
private String phone;
private int sex;
private int age;
private String qrcode;
private String webloginUrl;
public UsrInfoModel(String account, String name, String id_cardnum, String phone, int sex, int age, String qrcode,String webloginUrl) {
this.account = account;
this.name = name;
this.id_cardnum = id_cardnum;
this.phone = phone;
this.sex = sex;
this.age = age;
this.qrcode = qrcode;
this.webloginUrl = webloginUrl;
}
public UsrInfoModel(String account, String name, String id_cardnum, String phone, int sex, int age, String qrcode) {
this.account = account;
this.name = name;
this.id_cardnum = id_cardnum;
this.phone = phone;
this.sex = sex;
this.age = age;
this.qrcode = qrcode;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId_cardnum() {
return id_cardnum;
}
public void setId_cardnum(String id_cardnum) {
this.id_cardnum = id_cardnum;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getQrcode() {
return qrcode;
}
public void setQrcode(String qrcode) {
this.qrcode = qrcode;
}
public String getWebloginUrl() {
return webloginUrl;
}
public void setWebloginUrl(String webloginUrl) {
this.webloginUrl = webloginUrl;
}
}
... ...
package com.xkl.service;
/**
* Created by zhaoyue on 2017/3/11.
*/
public interface IQRCodeService {
public String getQRCodeWithAccount(String account);
public String getAccountWithQRCode(String qrcode);
}
\ No newline at end of file
... ...
package com.xkl.service;
import com.xkl.config.Constants;
import com.xkl.tools.DESTools;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* create by zhaoyue
* 2017-03-11
*/
@Service
@CommonsLog
public class QRCodeServiceImpl implements IQRCodeService, Constants {
private static final String REDIS_ACC_QR_PREFIX = "acc_qr-";
private static final String REDIS_QR_ACC_PREFIX = "qr_acc-";
private RedisTemplate<String, String> redis;
@Autowired
public void setRedis(RedisTemplate redis) {
this.redis = redis;
}
@Override
public String getAccountWithQRCode(String qrcode) {
String qrAcckey = genQrAccKey(qrcode);
String account = null;
if (redis.hasKey(qrAcckey)) {
account = redis.boundValueOps(qrAcckey).get();
}
return account;
}
@Override
public String getQRCodeWithAccount(String account) {
String accQrkey = genAccQrKey(account);
String qrCodeStr = "";
if (redis.hasKey(accQrkey)) {
qrCodeStr = redis.boundValueOps(accQrkey).get();
} else {
Date dt = new Date();
SimpleDateFormat matter1 = new SimpleDateFormat("yyyyMMdd");
String dateStr = matter1.format(dt);
try {
qrCodeStr = DESTools.encrypt(account + dateStr);
} catch (Exception e) {
e.printStackTrace();
}
String qrAcckey = genQrAccKey(qrCodeStr);
redis.boundValueOps(qrAcckey).set(account, Constants.QRCODE_EXPIRES_HOUR, TimeUnit.HOURS);
redis.boundValueOps(accQrkey).set(qrCodeStr, Constants.QRCODE_EXPIRES_HOUR, TimeUnit.HOURS);
}
return qrCodeStr;
}
private static String genAccQrKey(String account) {
return REDIS_ACC_QR_PREFIX + account;
}
private static String genQrAccKey(String account) {
return REDIS_QR_ACC_PREFIX + account;
}
}
... ...
package com.xkl.tools;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;
import java.util.Random;
/**
* DES加密解密类.
*/
public class DESTools {
/**
* 加密、解密key.
*/
private static final String PASSWORD_CRYPT_KEY = "HhiTuqm7lsMVHYD4Bkj2B5jSUh0IKPNk";
private final static String ALGORITHM = "DES";
public static void main(String[] args) throws Exception {
String orgStr = "202cb962ac59075b964b07152d234b70";
long start = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
orgStr = DESTools.getRandomString(20);
String encrptStr = DESTools.encrypt(orgStr);
String decrptStr = DESTools.decrypt(encrptStr);
if (!orgStr.equals(decrptStr)) {
System.out.println("error!!");
}
}
long end = System.currentTimeMillis();
System.out.println("time:" + (end - start));
}
/**
* 生成随机字符串
*
* @param length
* @return
*/
public static String getRandomString(int length) { //length表示生成字符串的长度
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}
/**
* 对数据进行DES加密.
*
* @param data 待进行DES加密的数据
* @return 返回经过DES加密后的数据
* @throws Exception
* @author <a href="mailto:xiexingxing1121@126.com" mce_href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-7-31 - 下午12:06:24
*/
public final static String decrypt(String data) throws Exception {
return new String(decrypt(hex2byte(data.getBytes()),
PASSWORD_CRYPT_KEY.getBytes()));
}
/**
* 对用DES加密过的数据进行解密.
*
* @param data DES加密数据
* @return 返回解密后的数据
* @throws Exception
* @author <a href="mailto:xiexingxing1121@126.com" mce_href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-7-31 - 下午12:07:54
*/
public final static String encrypt(String data) throws Exception {
return byte2hex(encrypt(data.getBytes(), PASSWORD_CRYPT_KEY
.getBytes()));
}
/**
* 用指定的key对数据进行DES加密.
*
* @param data 待加密的数据
* @param key DES加密的key
* @return 返回DES加密后的数据
* @throws Exception
* @author <a href="mailto:xiexingxing1121@126.com" mce_href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-7-31 - 下午12:09:03
*/
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密匙工厂,然后用它把DESKeySpec转换成
// 一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance(ALGORITHM);
// 用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
// 现在,获取数据并加密
// 正式执行加密操作
return cipher.doFinal(data);
}
/**
* 用指定的key对数据进行DES解密.
*
* @param data 待解密的数据
* @param key DES解密的key
* @return 返回DES解密后的数据
* @throws Exception
* @author <a href="mailto:xiexingxing1121@126.com" mce_href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-7-31 - 下午12:10:34
*/
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建一个DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
// 一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance(ALGORITHM);
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
// 现在,获取数据并解密
// 正式执行解密操作
return cipher.doFinal(data);
}
public static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("长度不是偶数");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}
}
\ No newline at end of file
... ...
package com.xkl.tools;
import java.util.Calendar;
import java.util.Date;
/**
* Created by zhaoyue on 2017/3/11.
*/
public class DatetimeTools {
public static int getAge(Date start, Date end) {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(start);
c2.setTime(end);
int year1 = c1.get(Calendar.YEAR);
int year2 = c2.get(Calendar.YEAR);
return Math.abs(year1 - year2);
}
}
... ...