提交 204326e2 authored 作者: zhoushaopan's avatar zhoushaopan

feat(合并wechat和浙里办): 合并wechat和浙里办

合并wechat和浙里办
上级 51148fdb
package com.tykj.zlbapp.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author zsp
* @version 1.0
* @date 2022/9/2 13:58
*/
@Slf4j
@Component
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("MyInterceptor1 -> preHandle执行了");
//true表示继续执行
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("MyInterceptor1 -> postHandle 执行了");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("MyInterceptor1 -> afterCompletion 执行了");
}
}
package com.tykj.zlbapp.config;
import com.sun.imageio.plugins.common.ImageUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import sun.font.FontDesignMetrics;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author zsp
......@@ -36,39 +26,8 @@ public class WebMvcConfig {
.allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH")
.maxAge(3600);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor())
// /**代表拦截所有路径
.addPathPatterns("/**")
// 忽略的路径
.excludePathPatterns("/zlbAppUser/test2");
// .addPathPatterns("/zlbAppUser/test2");
}
};
}
public static String ascii2native(String ascii){
List<String> ascii_s = new ArrayList<String>();
String zhengz= "\\\\u[0-9,a-f,A-F]{4}";
Pattern p = Pattern.compile(zhengz);
Matcher m=p.matcher(ascii);
while (m.find()){
ascii_s.add(m.group());
}
System.out.println(ascii_s);
for (int i = 0, j = 2; i < ascii_s.size(); i++) {
String code = ascii_s.get(i).substring(j, j + 4);
char ch = (char) Integer.parseInt(code, 16);
ascii = ascii.replace(ascii_s.get(i),String.valueOf(ch));
}
return ascii;
}
}
......@@ -50,7 +50,7 @@ public interface AppConstants {
String IRS_SK = "BCDSGS_a11b51242885499d4245ad5cd2c3c860";
String TOKEN_SESSION_KEY = "sessionAccessToken";
String APP_TOKEN_SESSION_KEY = "sessionAccessToken";
String USER_INFO_KEY = "sessionUserInfo";
}
package com.tykj.zlbapp.contants;
/**
* @author jie.chen
* @date 2022-03-30 15:24
*/
public interface WeChatConstants {
/**
* 单点登录(wechat) ticketId换token的地址
*
* 互联网 https://ibcdsg.zj.gov.cn:8443/restapi/prod/IC33000020220329000007/uc/sso/access_token
* 政府外网 https://bcdsg.zj.gov.cn:8443/restapi/prod/IC33000020220329000007/uc/sso/access_token
*/
// String ACCESS_TOKEN_URL = "https://bcdsg.zj.gov.cn:8443/restapi/prod/IC33000020220228000002/sso/servlet/simpleauth/uc/sso/access_token";
// String ACCESS_TOKEN_URL = "https://ibcdsg.zj.gov.cn:8443/restapi/prod/IC33000020220228000002/sso/servlet/simpleauth/uc/sso/access_token";
String ACCESS_TOKEN_URL = "https://ibcdsg.zj.gov.cn:8443/restapi/prod/IC33000020220329000007/uc/sso/access_token";
/**
* 单点登录(wechat) token获取用户信息地址
* 互联网 https://ibcdsg.zj.gov.cn:8443/restapi/prod/IC33000020220329000008/uc/sso/getUserInfo
* 政府外网 https://bcdsg.zj.gov.cn:8443/restapi/prod/IC33000020220329000008/uc/sso/getUserInfo
*/
// String GET_USER_INFO_URL = "https://bcdsg.zj.gov.cn:8443/restapi/prod/IC33000020220228000004/sso/servlet/simpleauth/uc/sso/getUserInfo";
// String GET_USER_INFO_URL = "https://ibcdsg.zj.gov.cn:8443/restapi/prod/IC33000020220228000004/sso/servlet/simpleauth/uc/sso/getUserInfo";
String GET_USER_INFO_URL = "https://ibcdsg.zj.gov.cn:8443/restapi/prod/IC33000020220329000008/uc/sso/getUserInfo";
/**
* IRS请求携带的请求头
*/
String X_BG_HMAC_ACCESS_KEY = "X-BG-HMAC-ACCESS-KEY";
String X_BG_HMAC_SIGNATURE = "X-BG-HMAC-SIGNATURE";
String X_BG_HMAC_ALGORITHM = "X-BG-HMAC-ALGORITHM";
String X_BG_DATE_TIME = "X-BG-DATE-TIME";
/**
* IRS签名算法
*/
String DEFAULT_HMAC_SIGNATURE = "hmac-sha256";
/**
* 应用ID
*/
String APP_ID = "lm5p4fkv+2002262819+qiyziy";
/**
* 微信端固定值为weixin
*/
String WEIXIN_ENDPOINT_TYPE = "weixin";
/**
* IRS 申请组件生成的AK
*/
String IRS_AK = "BCDSGA_117d3967fa52d9d62a54f03b2e3bdf8e";
/**
* IRS 申请组件生成的SK
*/
String IRS_SK = "BCDSGS_a11b51242885499d4245ad5cd2c3c860";
String WECHAT_TOKEN_SESSION_KEY = "sessionAccessToken";
String WECHAT_USER_INFO_KEY = "sessionUserInfo";
}
package com.tykj.zlbapp.subject.controller;
import com.tykj.zlbapp.demo.Limit;
import com.tykj.zlbapp.subject.service.ZlbAppAuthService;
import com.tykj.zlbapp.subject.vo.TokenVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
......@@ -11,8 +9,6 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;
/**
* @author zsp
......@@ -30,7 +26,7 @@ public class ZlbAppController {
@GetMapping("/getAppTokenByTicketId")
@ApiOperation(value = "根据票据换取token", notes = "根据票据换取token")
public ResponseEntity getAppTokenByTicketId(String ticketId, HttpServletRequest request) {
public ResponseEntity getAppTokenByTicketId(String ticketId) {
// HttpSession session = request.getSession();
//1. 通过ticketId 换取 accessToken
......@@ -51,13 +47,4 @@ public class ZlbAppController {
return ResponseEntity.ok(zlbAppAuthService.getUserInfoByToken(token));
}
@GetMapping("/test2")
@ApiOperation(value = "测试熔断", notes = "测试熔断")
@Limit(key = "limit2", permitsPerSecond = 1, timeout = 500, timeunit = TimeUnit.MILLISECONDS,msg = "当前排队人数较多,请稍后再试!")
public String limit2() {
log.info("令牌桶limit2获取令牌成功");
return "ok";
}
}
package com.tykj.zlbapp.subject.controller;
import com.alibaba.fastjson.JSONObject;
import com.tykj.zlbapp.subject.service.ZlbWeChatAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import static com.tykj.zlbapp.contants.WeChatConstants.WECHAT_TOKEN_SESSION_KEY;
import static com.tykj.zlbapp.contants.WeChatConstants.WECHAT_USER_INFO_KEY;
/**
* @author zsp
* @version 1.0
* @date 2022/8/23 14:19
*/
@RestController
@Api(tags = "浙里办人员登录模块(wechat)", description = "浙里办人员登录模块(wechat)")
@RequestMapping("/zlbWeChatUser")
@Slf4j
public class ZlbWeChatController {
@Resource
private ZlbWeChatAuthService zlbWeChatAuthService;
@GetMapping("/getWeChatTokenByTicketId")
@ApiOperation(value = "根据票据换取token", notes = "根据票据换取token")
public ResponseEntity getTokenByTicketId(String ticketId) {
// HttpSession session = request.getSession();
// //1. 通过ticketId 换取 accessToken
String token = zlbWeChatAuthService.getTokenByTicketId(ticketId);
// //2. 保存accessToken
// session.setAttribute(WECHAT_TOKEN_SESSION_KEY, token);
// //3. 通过accessToken 获取用户信息
// JSONObject userInfo = zlbWeChatAuthService.getUserInfoByToken(token);
// //4. 缓存用户信息
// session.setAttribute(WECHAT_USER_INFO_KEY, userInfo);
// log.info("session:{}",session);
return ResponseEntity.ok(token);
}
@GetMapping("/getWeChatCurrentUser")
@ApiOperation(value = "获取当前登录的用户信息", notes = "获取当前登录的用户信息")
public ResponseEntity getCurrentUser(String token) {
// HttpSession session = request.getSession();
// Object attribute = session.getAttribute(WECHAT_USER_INFO_KEY);
// if(attribute!= null){
// log.info("attribute:{}",attribute);
// return (JSONObject) attribute;
// }else{
// throw new RuntimeException("未登录");
// }
JSONObject userInfoByToken = zlbWeChatAuthService.getUserInfoByToken(token);
return ResponseEntity.ok(userInfoByToken);
}
}
package com.tykj.zlbapp.subject.service;
import com.alibaba.fastjson.JSONObject;
/**
* @author zsp
* @version 1.0
* @date 2022/8/23 13:46
*/
public interface ZlbWeChatAuthService {
/**
* 根据token获取用户信息
* @param accessToken token
* @return 对象
*/
JSONObject getUserInfoByToken(String accessToken);
/**
* 根据ticketId获取token
* @param ticketId 票据id
* @return token
*/
String getTokenByTicketId(String ticketId);
}
......@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
import com.tykj.zlbapp.contants.AppConstants;
import com.tykj.zlbapp.subject.service.ZlbAppAuthService;
import com.tykj.zlbapp.subject.vo.TokenVO;
import com.tykj.zlbapp.util.CustomEncodeUtil;
import com.tykj.zlbapp.util.HeadUtil;
import com.tykj.zlbapp.util.MD5Util;
import com.tykj.zlbapp.util.XMLParse;
......@@ -49,9 +50,15 @@ public class ZlbAppAuthServiceImpl implements ZlbAppAuthService {
HttpEntity<MultiValueMap<String, Object>> httpEntity=new HttpEntity<>(params,headers);
String body = restTemplate.postForEntity(AppConstants.ACCESS_TOKEN_URL, httpEntity, String.class).getBody();
//解析xml
// Map<String, Object> valueByNode =
// JSONObject.parseObject(JSONObject.toJSONString(valueByNode), TokenVO.class)
return XMLParse.getValueByNode(body);
Map<String, Object> nodeMap = XMLParse.getValueByNode(body);
if (nodeMap.containsKey("username")){
//说明是请求成功了
//对数据进行加密
//名称加密
String username = (String) nodeMap.get("username");
nodeMap.put("username",username);
}
return nodeMap;
}
@Override
......@@ -70,6 +77,21 @@ public class ZlbAppAuthServiceImpl implements ZlbAppAuthService {
HttpEntity<MultiValueMap<String, Object>> httpEntity=new HttpEntity<>(params,headers);
String body = restTemplate.postForEntity(AppConstants.ACCESS_TOKEN_URL, httpEntity, String.class).getBody();
//解析xml
return XMLParse.getValueByNode(body);
//对数据进行加密
Map<String, Object> nodeMap = XMLParse.getValueByNode(body);
if (nodeMap.containsKey("idnum")){
//说明请求成功
//加密身份证号码
String idnum = CustomEncodeUtil.encodeIdCard((String) nodeMap.get("idnum"));
//加密手机号码
String mobile = CustomEncodeUtil.encodeMobilePhone((String) nodeMap.get("mobile"));
//加密用户名
String username = CustomEncodeUtil.encodeName((String) nodeMap.get("username"));
//重新放入map之中
nodeMap.put("idnum",idnum);
nodeMap.put("mobile",mobile);
nodeMap.put("username",username);
}
return nodeMap;
}
}
package com.tykj.zlbapp.subject.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tykj.zlbapp.contants.WeChatConstants;
import com.tykj.zlbapp.subject.service.ZlbWeChatAuthService;
import com.tykj.zlbapp.util.IrsSignRes;
import com.tykj.zlbapp.util.IrsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.Map;
/**
* @author zsp
* @version 1.0
* @date 2022/8/23 13:56
*/
@Service
@Slf4j
public class ZlbWeChatAuthServiceImpl implements ZlbWeChatAuthService {
@Resource
private RestTemplate restTemplate;
@Override
public JSONObject getUserInfoByToken(String accessToken) {
HttpHeaders headers = getHttpHeaders(WeChatConstants.GET_USER_INFO_URL);
JSONObject body = new JSONObject();
body.put("token", accessToken);
HttpEntity<Map<String, Object>> request = new HttpEntity<>(body, headers);
ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(WeChatConstants.GET_USER_INFO_URL, request, String.class);
return checkResponse(stringResponseEntity).getJSONObject("data");
}
@Override
public String getTokenByTicketId(String ticketId) {
HttpHeaders headers = getHttpHeaders(WeChatConstants.ACCESS_TOKEN_URL);
JSONObject body = new JSONObject();
body.put("appId", WeChatConstants.APP_ID);
body.put("ticketId", ticketId);
HttpEntity<Map<String, Object>> request = new HttpEntity<>(body, headers);
ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(WeChatConstants.ACCESS_TOKEN_URL, request, String.class);
return checkResponse(stringResponseEntity).getJSONObject("data").getString("accessToken");
}
private JSONObject checkResponse(ResponseEntity<String> stringResponseEntity) {
if (!stringResponseEntity.getStatusCode().is2xxSuccessful()) {
//请求失败
throw new RuntimeException("status:" + stringResponseEntity.getStatusCodeValue() + " " + stringResponseEntity.getBody());
}
JSONObject result = JSON.parseObject(stringResponseEntity.getBody());
if (result.containsKey("errorCode") && result.getString("errorCode") != null && !result.getBooleanValue("success")) {
//业务错误
throw new RuntimeException(result.toString());
}
return result;
}
private HttpHeaders getHttpHeaders(String url) {
IrsSignRes res = IrsUtils.sign(url, "POST");
HttpHeaders headers = new HttpHeaders();
headers.add(WeChatConstants.X_BG_HMAC_ACCESS_KEY, res.getAccessKey());
headers.add(WeChatConstants.X_BG_HMAC_ALGORITHM, res.getAlgorithm());
headers.add(WeChatConstants.X_BG_HMAC_SIGNATURE, res.getSignature());
headers.add(WeChatConstants.X_BG_DATE_TIME, res.getDateTime());
return headers;
}
}
package com.tykj.zlbapp.util;
/**
* @author zsp
* @date 2022/9/15
*/
public class CustomEncodeUtil {
/**
* 加密姓名
* @param username 姓名
* @return 加密之后的姓名
*/
public static String encodeName(String username){
String decodeName = "";
char[] r = username.toCharArray();
if(r.length ==1){
decodeName = username;
}
if(r.length == 2){
decodeName = username.replaceFirst(username.substring(1),"*");
}
if (r.length > 2) {
decodeName = username.replaceFirst(username.substring(1,r.length-1) ,"*");
}
return decodeName;
}
/**
* 加密手机号
* @param phone 手机号
* @return 加密之后的手机号
*/
public static String encodeMobilePhone(String phone){
char[] chars = phone.toCharArray();
for(int i=0; i<chars.length;i++){
if(i>2 && i<7){
chars[i] = '*';
}
}
return String.valueOf(chars);
}
/**
* 加密身份证号
* @param idCard 加密前的身份证号
* @return 加密之后的身份证号
*/
public static String encodeIdCard(String idCard){
return idCard.substring(0, 7) + "****" + idCard.substring(11, idCard.length());
}
}
package com.tykj.zlbapp.util;
import lombok.Data;
/**
* @author jie.chen
* @date 2022-03-30 15:28
*/
@Data
public class IrsSignRes {
private String accessKey;
private String signature;
private String algorithm;
private String dateTime;
}
package com.tykj.zlbapp.util;
import com.tykj.zlbapp.contants.WeChatConstants;
import lombok.SneakyThrows;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @author jie.chen
* @date 2022-03-30 15:28
*/
public class IrsUtils {
@SneakyThrows
public static IrsSignRes sign(String url, String method) {
UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(url).build();
uriComponents = uriComponents.encode();
List<String> queryArr = new ArrayList<>();
MultiValueMap<String, String> queryParams = uriComponents.getQueryParams();
for (Map.Entry<String, List<String>> next : queryParams.entrySet()) {
for (String va : next.getValue()) {
if (va == null) {
queryArr.add(next.getKey() + "=");
} else {
queryArr.add(next.getKey() + "=" + va);
}
}
}
//按照字典排序
Collections.sort(queryArr);
///Tue, 09 Nov 2021 08:49:20 GMT
DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String dateTime = dateFormat.format(new Date());
String signStr = method.toUpperCase() + "\n" +
//拼接url path
uriComponents.getPath() + "\n" +
//拼接url query
String.join("&", queryArr) + "\n" +
WeChatConstants.IRS_AK + "\n" +
dateTime + "\n";
String sign = hmacSha256Base64(signStr, WeChatConstants.IRS_SK);
IrsSignRes res = new IrsSignRes();
res.setSignature(sign);
res.setAccessKey(WeChatConstants.IRS_AK);
res.setDateTime(dateTime);
res.setAlgorithm(WeChatConstants.DEFAULT_HMAC_SIGNATURE);
return res;
}
@SneakyThrows
private static String hmacSha256Base64(String content, String key) {
Mac hmacSHA256 = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
hmacSHA256.init(secretKey);
byte[] bytes = hmacSHA256.doFinal(content.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(bytes);
}
public static void main(String[] args) {
System.out.println(sign("https://bcdsg.zj.gov.cn:8443/restapi/prod/IC33000020220329000007/uc/sso/getUserInfo", "POST"));
}
}
......@@ -48,5 +48,57 @@ public class XMLParse {
"</SSO>";
String a = "\"orgcoding\": \"001006\",\n" +
" \"country\": \"\",\n" +
" \"officeaddress\": \"\",\n" +
" \"isFace\": \"0\",\n" +
" \"nation\": \"\",\n" +
" \"telephone2\": \"\",\n" +
" \"createdate\": \"2020-10-22 08:56:38\",\n" +
" \"official\": \"\",\n" +
" \"userid\": \"8a118a527527bd3901754dcec5c216f9\",\n" +
" \"officefax\": \"\",\n" +
" \"officialtype\": \"\",\n" +
" \"province\": \"\",\n" +
" \"idnumendtime\": \"\",\n" +
" \"authlevel\": \"3\",\n" +
" \"workaddr\": \"\",\n" +
" \"officenum\": \"\",\n" +
" \"companyalias\": \"\",\n" +
" \"postcode\": \"\",\n" +
" \"errmsg\": \"成功\",\n" +
" \"orderby\": \"35224225\",\n" +
" \"telephone\": \"\",\n" +
" \"firmname\": \"\",\n" +
" \"virtualnum\": \"\",\n" +
" \"idtype\": \"1\",\n" +
" \"companyname\": \"\",\n" +
" \"companydesc\": \"\",\n" +
" \"companysize\": \"\",\n" +
" \"driverlicense\": \"\",\n" +
" \"birthday\": \"\",\n" +
" \"aliuserid\": \"2088022756317892\",\n" +
" \"homephone\": \"\",\n" +
" \"city\": \"\",\n" +
" \"headpicture\": \"\",\n" +
" \"result\": \"0\",\n" +
" \"loginaddr\": \"\",\n" +
" \"useable\": \"1\",\n" +
" \"permitlicense\": \"\",\n" +
" \"mobile2\": \"\",\n" +
" \"servicecontent\": \"\",\n" +
" \"email\": \"\",\n" +
" \"loginname\": \"zj410526199611145814\",\n" +
" \"sex\": \"\",\n" +
" \"homeaddress\": \"\",\n" +
" \"mobile\": \"15237690773\",\n" +
" \"companypro\": \"\",\n" +
" \"SSO\": \"\",\n" +
" \"idnumstarttime\": \"\",\n" +
" \"companytype\": \"\",\n" +
" \"idnum\": \"410526199611145814\",\n" +
" \"username\": \"周少攀\"";
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论