Commit 8e33252f authored by zhangzhenbang's avatar zhangzhenbang

试探性写了一两个接口

parent 00b54e79
...@@ -112,6 +112,13 @@ ...@@ -112,6 +112,13 @@
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
</dependency> </dependency>
<!-- AES-128-CBC加解密包 -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk16</artifactId>
<version>1.46</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.yingxin.beijingvehicleflow.controller;
import com.yingxin.beijingvehicleflow.entity.IdentityInformation;
import com.yingxin.beijingvehicleflow.response.Response;
import com.yingxin.beijingvehicleflow.service.IdentityInformationService;
import com.yingxin.beijingvehicleflow.util.AESUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* description //TODO
*
* @author 226
* @version 1.0
* @date 2020/2/24
*/
@RestController
@CrossOrigin
@RequestMapping("/api/v1")
public class IdentityInfoController {
private static Logger LOGGER = LoggerFactory.getLogger(IdentityInfoController.class);
@Autowired
private IdentityInformationService identityInformationService;
/**
* 根据tempCode去获取openId,然后在数据库中查看是否有与其相同的openId
*
* @date 2020/2/21
* @param json {"code":""}
* @return com.yingxin.covid19prevention.response.Response
*/
@PostMapping("/resident/exist/state")
public Response isResidentExist(@RequestBody String json) {
JSONObject jsonObject = JSONObject.fromObject(json);
JSONObject jsonAfterLogin = identityInformationService.getwechatInfoByTempCode(jsonObject.getString("code"));
IdentityInformation info = identityInformationService.getAllInfoByOpenid(jsonAfterLogin.getString("openid"));
if (info == null) {
return Response.fail();
}
return new Response<IdentityInformation>(true,"","",info);
}
/**
* 将获取到的加密手机号进行解密,将解密的手机号和获取到的openId一起存到用户表中,
*
* @date 2020/2/21
* @param json code、iv、encryptedData
* @return com.yingxin.covid19prevention.response.Response userId
*/
@PostMapping("/resident/wechat")
public Response getDecryptData(@RequestBody String json) {
JSONObject jsonObject = JSONObject.fromObject(json);
String tempCode = jsonObject.getString("code");
String iv = jsonObject.getString("iv");
String encryptedData = jsonObject.getString("encryptedData");
JSONObject loginJson = identityInformationService.getwechatInfoByTempCode(tempCode);
String sessionKey = loginJson.getString("session_key");
String result;
try {
result = AESUtil.decryptForWeChatApplet(encryptedData, sessionKey, iv);
} catch (Exception e) {
LOGGER.error("获取微信手机号码解密异常:", e);
return Response.fail("Decrypt-ERROR", "获取微信手机号码解密异常");
}
JSONObject wechatJson;
String weChatPhone;
String openid;
try {
wechatJson = JSONObject.fromObject(result);
weChatPhone = wechatJson.getString("phoneNumber");
openid = loginJson.getString("openid");
} catch (Exception e) {
LOGGER.error("解密结果json解析异常", e);
return Response.fail("JSON-PARSE-ERROR", "解密结果json解析异常");
}
IdentityInformation info = new IdentityInformation();
info.setWechatPhone(weChatPhone);
info.setWechatOpenid(openid);
int resultAfterInsert = identityInformationService.insertIdentityInfo(info);
if (resultAfterInsert > 0) {
return new Response<Integer>(true,"","",resultAfterInsert);
}
return Response.fail();
}
}
...@@ -23,7 +23,6 @@ public class ReservationController { ...@@ -23,7 +23,6 @@ public class ReservationController {
private static Logger LOGGER = LoggerFactory.getLogger(ReservationController.class); private static Logger LOGGER = LoggerFactory.getLogger(ReservationController.class);
@Autowired @Autowired
private ReservationService reservationService; private ReservationService reservationService;
......
package com.yingxin.beijingvehicleflow.mapper; package com.yingxin.beijingvehicleflow.mapper;
import com.yingxin.beijingvehicleflow.entity.IdentityInformation; import com.yingxin.beijingvehicleflow.entity.IdentityInformation;
import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Update;
/** /**
* 数据库表单对应Mapper * 数据库表单对应Mapper
...@@ -39,4 +36,7 @@ public interface IdentityInformationMapper { ...@@ -39,4 +36,7 @@ public interface IdentityInformationMapper {
"phone_number=#{phoneNumber},face_image=#{faceImage},toBeijing_date=#{toBeijingDate}," + "phone_number=#{phoneNumber},face_image=#{faceImage},toBeijing_date=#{toBeijingDate}," +
"is_worker=#{isWorker} where id =#{id}") "is_worker=#{isWorker} where id =#{id}")
int updateIdentityInfo(IdentityInformation identityInformation); int updateIdentityInfo(IdentityInformation identityInformation);
@Select("select * from identity_information where openid = #{openid}")
IdentityInformation getAllInfoByOpenid(String openid);
} }
package com.yingxin.beijingvehicleflow.service; package com.yingxin.beijingvehicleflow.service;
import com.yingxin.beijingvehicleflow.entity.IdentityInformation; import com.yingxin.beijingvehicleflow.entity.IdentityInformation;
import net.sf.json.JSONObject;
/** /**
* description //TODO * description //TODO
...@@ -28,4 +29,9 @@ public interface IdentityInformationService { ...@@ -28,4 +29,9 @@ public interface IdentityInformationService {
* @return int 更新数据条数 * @return int 更新数据条数
*/ */
boolean isUpdateIdentityInfoSucc(IdentityInformation identityInformation); boolean isUpdateIdentityInfoSucc(IdentityInformation identityInformation);
JSONObject getwechatInfoByTempCode(String code);
IdentityInformation getAllInfoByOpenid(String openid);
} }
...@@ -5,9 +5,19 @@ import com.yingxin.beijingvehicleflow.entity.IdentityInformation; ...@@ -5,9 +5,19 @@ import com.yingxin.beijingvehicleflow.entity.IdentityInformation;
import com.yingxin.beijingvehicleflow.mapper.IdentityInformationMapper; import com.yingxin.beijingvehicleflow.mapper.IdentityInformationMapper;
import com.yingxin.beijingvehicleflow.service.IdentityInformationService; import com.yingxin.beijingvehicleflow.service.IdentityInformationService;
import com.yingxin.beijingvehicleflow.util.PicUtils; import com.yingxin.beijingvehicleflow.util.PicUtils;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/** /**
* description //TODO * description //TODO
* *
...@@ -18,6 +28,8 @@ import org.springframework.stereotype.Service; ...@@ -18,6 +28,8 @@ import org.springframework.stereotype.Service;
@Service @Service
public class IdentityInformationServiceImpl implements IdentityInformationService { public class IdentityInformationServiceImpl implements IdentityInformationService {
private static Logger LOGGER = LoggerFactory.getLogger(IdentityInformationService.class);
@Autowired @Autowired
private IdentityInformationMapper identityInformationMapper; private IdentityInformationMapper identityInformationMapper;
...@@ -34,4 +46,47 @@ public class IdentityInformationServiceImpl implements IdentityInformationServic ...@@ -34,4 +46,47 @@ public class IdentityInformationServiceImpl implements IdentityInformationServic
Const.FACE_IMG_SIZE)); Const.FACE_IMG_SIZE));
return identityInformationMapper.updateIdentityInfo(identityInformation) == 1; return identityInformationMapper.updateIdentityInfo(identityInformation) == 1;
} }
@Override
public JSONObject getwechatInfoByTempCode(String code) {
BufferedReader in = null;
String url = "https://api.weixin.qq.com/sns/jscode2session?appid=wx8784a1a35b9c76af" +
"&secret=bbde177fbebb080ad3ad5f57db3db8a2" +
"&js_code="+code+
"&grant_type=authorization_code";
URL realUrl;
try {
realUrl = new URL(url);
} catch (MalformedURLException e) {
LOGGER.error("通过tempCode获取openId时,URL错误:", e);
return null;
}
URLConnection connection;
JSONObject jsonObject;
try {
connection = realUrl.openConnection();
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
return JSONObject.fromObject(in.readLine());
} catch (IOException e) {
LOGGER.error("通过tempCode获取openId时,打开连接错误:", e);
return null;
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
LOGGER.error("通过tempCode获取openId时,流关闭异常:", e);
}
}
// return jsonObject.getString("openid");
}
@Override
public IdentityInformation getAllInfoByOpenid(String openid) {
return identityInformationMapper.getAllInfoByOpenid(openid);
}
} }
package com.yingxin.beijingvehicleflow.util;
import org.bouncycastle.util.encoders.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* AES解密(JAVA版本)
* Add by 成长的小猪(Jason.Song) on 2018/10/26
* http://blog.csdn.net/jasonsong2008
*/
public class AESUtil {
/**
* 微信小程序 开放数据解密
* AES解密(Base64)
* Add by 成长的小猪(Jason.Song) on 2018/10/26
* @param encryptedData 已加密的数据
* @param sessionKey 解密密钥
* @param iv IV偏移量
* @return
* @throws Exception
*/
public static String decryptForWeChatApplet(String encryptedData, String sessionKey, String iv) throws Exception {
byte[] decryptBytes = Base64.decode(encryptedData);
byte[] keyBytes = Base64.decode(sessionKey);
byte[] ivBytes = Base64.decode(iv);
return new String(decryptByAesBytes(decryptBytes, keyBytes, ivBytes));
}
/**
* AES解密
* Add by 成长的小猪(Jason.Song) on 2018/10/26
* @param decryptedBytes 待解密的字节数组
* @param keyBytes 解密密钥字节数组
* @param ivBytes IV初始化向量字节数组
* @return
* @throws Exception
*/
public static byte[] decryptByAesBytes(byte[] decryptedBytes, byte[] keyBytes, byte[] ivBytes) throws Exception {
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec iv = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, key, iv);
byte[] outputBytes = cipher.doFinal(decryptedBytes);;
return outputBytes;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment