Просмотр исходного кода

Merge remote-tracking branch 'remotes/origin/dev_1.0.0' into release_1.0.0

xiatian 2 лет назад
Родитель
Сommit
dceb37118d

+ 0 - 11
src/main/java/cn/com/qmth/mps/config/SysProperty.java

@@ -16,9 +16,6 @@ public class SysProperty {
 	@Value("${wxapp-appid}")
     private String wxappAppid;
 	
-	@Value("${wxapp-secret}")
-    private String wxappSecret;
-
 	public Integer getSessionTimeout() {
 		return sessionTimeout;
 	}
@@ -35,14 +32,6 @@ public class SysProperty {
 		this.wxappAppid = wxappAppid;
 	}
 
-	public String getWxappSecret() {
-		return wxappSecret;
-	}
-
-	public void setWxappSecret(String wxappSecret) {
-		this.wxappSecret = wxappSecret;
-	}
-
 	public String getWxappUrl() {
 		return wxappUrl;
 	}

+ 8 - 25
src/main/java/cn/com/qmth/mps/service/impl/AuthServiceImpl.java

@@ -16,6 +16,7 @@ import com.qmth.boot.core.exception.StatusException;
 import com.qmth.boot.core.security.annotation.AuthorizationComponent;
 import com.qmth.boot.core.security.service.AuthorizationService;
 import com.qmth.boot.core.solar.model.WxappAccessToken;
+import com.qmth.boot.core.solar.model.WxappSession;
 import com.qmth.boot.core.solar.service.SolarService;
 import com.qmth.boot.tools.signature.SignatureType;
 import com.qmth.boot.tools.uuid.FastUUID;
@@ -61,8 +62,8 @@ public class AuthServiceImpl implements AuthorizationService<User>, AuthService
 	
 	@Override
 	public AdminLoginVo loginWxAppCode(String loginCode) {
-		JSONObject auth=getAuthorization(loginCode);
-		String openid=ByteUtil.toHexAscii(SHA256.encode(auth.getString("openid")));
+		WxappSession ws=solarService.getWxappSessionByCode(sysProperty.getWxappAppid(), loginCode);
+		String openid=ByteUtil.toHexAscii(SHA256.encode(ws.getOpenId()));
 		WxappInfoEntity wi=wxappInfoService.getByOpenId(openid);
 		if(wi==null) {
 			throw ParameterExceptions.OPENID_NOT_FOUND;
@@ -195,9 +196,9 @@ public class AuthServiceImpl implements AuthorizationService<User>, AuthService
 	@Transactional
 	@Override
 	public AdminLoginVo loginWxAppByEncryptedData(String loginCode, String encryptedData, String iv) {
-		JSONObject auth=getAuthorization(loginCode);
-		String openid=ByteUtil.toHexAscii(SHA256.encode(auth.getString("openid")));
-		JSONObject jo=decrypt(encryptedData, iv, auth.getString("session_key"));
+		WxappSession ws=solarService.getWxappSessionByCode(sysProperty.getWxappAppid(), loginCode);
+		String openid=ByteUtil.toHexAscii(SHA256.encode(ws.getOpenId()));
+		JSONObject jo=decrypt(encryptedData, iv, ws.getSessionKey());
 		String phone=jo.getString("purePhoneNumber");
 		UserEntity userEntity = userService.getByLoginName(phone);
 		if (userEntity == null) {
@@ -238,24 +239,6 @@ public class AuthServiceImpl implements AuthorizationService<User>, AuthService
 		return vo;
 	}
 
-	private JSONObject getAuthorization(String loginCode) {
-		Map<String, String> params = new HashMap<>();
-		params.put("appid", sysProperty.getWxappAppid());
-		params.put("secret", sysProperty.getWxappSecret());
-		params.put("js_code", loginCode);
-		params.put("grant_type", "authorization_code");
-		String ret;
-		try {
-			ret = HttpUtil.httpsActionGet("https://api.weixin.qq.com/sns/jscode2session", null, params);
-		} catch (Exception e) {
-			throw new StatusException("登录失败", e);
-		}
-		JSONObject jo = JSONObject.fromObject(ret);
-		if (jo.containsKey("errcode")) {
-			throw new StatusException("登录失败," + jo.getString("errmsg"));
-		}
-		return jo;
-	}
 
 	private JSONObject decrypt(String encryptedData, String iv, String sessionKey){
 		String cipherString = "AES/CBC/PKCS5Padding";
@@ -319,8 +302,8 @@ public class AuthServiceImpl implements AuthorizationService<User>, AuthService
 		if (!school.getEnable()) {
 			throw new StatusException("该学校已禁用");
 		}
-		JSONObject auth=getAuthorization(loginCode);
-		String openid=ByteUtil.toHexAscii(SHA256.encode(auth.getString("openid")));
+		WxappSession ws=solarService.getWxappSessionByCode(sysProperty.getWxappAppid(), loginCode);
+		String openid=ByteUtil.toHexAscii(SHA256.encode(ws.getOpenId()));
 		WxappInfoEntity wi=wxappInfoService.getByOpenId(openid);
 		if(wi==null) {
 			wi=new WxappInfoEntity();

+ 20 - 15
src/main/java/cn/com/qmth/mps/service/impl/SchoolServiceImpl.java

@@ -5,6 +5,7 @@ import java.util.List;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -18,6 +19,8 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.qmth.boot.core.collection.PageResult;
 import com.qmth.boot.core.exception.StatusException;
+import com.qmth.boot.core.solar.model.OrgInfo;
+import com.qmth.boot.core.solar.service.SolarService;
 
 import cn.com.qmth.mps.bean.User;
 import cn.com.qmth.mps.dao.SchoolDao;
@@ -25,23 +28,17 @@ import cn.com.qmth.mps.entity.SchoolEntity;
 import cn.com.qmth.mps.enums.Role;
 import cn.com.qmth.mps.service.SchoolService;
 import cn.com.qmth.mps.util.PageUtil;
-import cn.com.qmth.mps.util.SolarHttpUtil;
 import cn.com.qmth.mps.vo.school.SchoolDomain;
 import cn.com.qmth.mps.vo.school.SchoolQuery;
 import cn.com.qmth.mps.vo.school.SchoolVo;
 
 @Service
 public class SchoolServiceImpl extends ServiceImpl<SchoolDao, SchoolEntity> implements SchoolService {
-	@Value("${qmth.solar.host}")
-	private String host;
-	@Value("${qmth.solar.org.uri}")
-	private String uri;
-	@Value("${com.qmth.solar.access-key}")
-	private String accessKey;
-	@Value("${com.qmth.solar.access-secret}")
-	private String accessSecret;
 	@Value("${wxapp-url}")
 	private String wxappUrl;
+	
+	@Autowired
+	private SolarService solarService;
 
 	@Transactional
 	@Override
@@ -49,17 +46,17 @@ public class SchoolServiceImpl extends ServiceImpl<SchoolDao, SchoolEntity> impl
 		if (!user.getRole().equals(Role.SUPER_ADMIN)) {
 			throw new StatusException("没有权限");
 		}
-		SolarHttpUtil orgUtil = new SolarHttpUtil(accessKey, accessSecret, host, uri);
-		List<SchoolEntity> list = orgUtil.getOrgs();
-		if (CollectionUtils.isEmpty(list)) {
+		List<OrgInfo> orgs=solarService.getOrgList();
+		if (CollectionUtils.isEmpty(orgs)) {
 			return;
 		}
-		for (SchoolEntity org : list) {
+		for (OrgInfo org : orgs) {
 			addSchoolForSync(user, org);
 		}
 	}
 
-	private void addSchoolForSync(User user, SchoolEntity school) {
+	private void addSchoolForSync(User user, OrgInfo org) {
+		SchoolEntity school=of(org);
 		SchoolEntity old = this.findSchoolByCode(school.getCode());
 		if (old != null) {
 			old.setAccessKey(school.getAccessKey());
@@ -71,7 +68,15 @@ public class SchoolServiceImpl extends ServiceImpl<SchoolDao, SchoolEntity> impl
 			this.save(school);
 		}
 	}
-
+	private SchoolEntity of(OrgInfo info) {
+		SchoolEntity org=new SchoolEntity();
+		org.setCode(info.getCode());
+		org.setName(info.getName());
+		org.setAccessKey(info.getAccessKey());
+		org.setAccessSecret(info.getAccessSecret());
+		org.setEnable(true);
+		return org;
+	}
 	private SchoolEntity findSchoolByCode(String code) {
 		QueryWrapper<SchoolEntity> wrapper = new QueryWrapper<>();
 		LambdaQueryWrapper<SchoolEntity> lw = wrapper.lambda();

+ 0 - 248
src/main/java/cn/com/qmth/mps/util/SolarHttpUtil.java

@@ -1,248 +0,0 @@
-package cn.com.qmth.mps.util;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.security.cert.CertificateException;
-import java.security.cert.X509Certificate;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.net.ssl.HttpsURLConnection;
-import javax.net.ssl.SSLContext;
-import javax.net.ssl.SSLSocketFactory;
-import javax.net.ssl.TrustManager;
-import javax.net.ssl.X509TrustManager;
-
-import com.alibaba.fastjson.JSONArray;
-import com.alibaba.fastjson.JSONObject;
-import com.qmth.boot.core.exception.StatusException;
-import com.qmth.boot.core.solar.model.AppLicense;
-import com.qmth.boot.tools.signature.SignatureEntity;
-import com.qmth.boot.tools.signature.SignatureType;
-
-import cn.com.qmth.mps.entity.SchoolEntity;
-
-public class SolarHttpUtil {
-
-	/** 默认的编码格式 */
-	private static final String DEFAULT_CHARSET = "UTF-8";
-
-	private static final String CONTENT_TYPE = "Content-Type";
-
-	private static final String APPLICATION_JSON = "application/x-www-form-urlencoded;charset=utf-8";
-
-	private static final String METHOD_POST = "POST";
-
-	private static final String AUTH = "Authorization";
-
-	private String host = null;
-
-	private String uri = null;
-
-	private String accessKey = null;
-
-	private String accessSecret = null;
-
-	public SolarHttpUtil(String accessKey, String accessSecret, String host, String uri) {
-		this.accessKey = accessKey;
-		this.accessSecret = accessSecret;
-		this.uri = uri;
-		this.host = host;
-	}
-
-	/**
-	 * 
-	 * @param params headers参数
-	 * @param datas  requestParams参数
-	 * @return
-	 * @throws Exception
-	 */
-	public String httpAction(Map<String, String> params, Map<String, Object> datas) {
-		String result = null;
-		HttpsURLConnection conn = null;
-		OutputStream os = null;
-		InputStream is = null;
-
-		try {
-
-			// 获取链接
-			URL url = new URL(host + uri);
-			conn = (HttpsURLConnection) url.openConnection();
-
-			conn.setRequestMethod(METHOD_POST);
-			conn.setRequestProperty(CONTENT_TYPE, APPLICATION_JSON);
-			// 设置鉴权
-			long time = System.currentTimeMillis();
-			String signature = SignatureEntity.build(SignatureType.SECRET, METHOD_POST, uri, time, accessKey,
-					accessSecret);
-			conn.setRequestProperty(AUTH, signature);
-			conn.setRequestProperty("time", String.valueOf(time));
-			// ssl
-			SSLContext context = SSLContext.getInstance("SSL", "SunJSSE");
-			TrustManager[] tm = new TrustManager[] { new X509TrustManager() {
-
-				@Override
-				public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
-				}
-
-				@Override
-				public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
-				}
-
-				@Override
-				public X509Certificate[] getAcceptedIssuers() {
-					return null;
-				}
-			} };
-			// 初始化
-			context.init(null, tm, new java.security.SecureRandom());
-			// 获取SSLSocketFactory对象
-			SSLSocketFactory ssf = context.getSocketFactory();
-			conn.setSSLSocketFactory(ssf);
-
-			conn.setUseCaches(false);
-			conn.setDoOutput(true);
-
-			// 设置额外的参数
-			if (params != null && !params.isEmpty()) {
-
-				for (Map.Entry<String, String> param : params.entrySet()) {
-					conn.setRequestProperty(param.getKey(), param.getValue());
-				}
-			}
-			// 创建链接
-			conn.connect();
-			// 设置请求参数
-			if (datas != null) {
-				StringBuilder sb = new StringBuilder();
-				for (Map.Entry<String, Object> data : datas.entrySet()) {
-					sb.append(data.getKey()).append("=").append(data.getValue()).append("&");
-				}
-				os = conn.getOutputStream();
-				os.write(sb.toString().getBytes());
-				os.flush();
-			}
-
-			result = getResult(conn);
-		} catch (IOException e) {
-			try {
-				JSONObject js = JSONObject.parseObject(e.getMessage());
-				if (js.getString("code").startsWith("401")) {
-					throw new StatusException("无效的授权");
-				} else {
-					throw new StatusException("授权服务器访问失败", e);
-				}
-			} catch (StatusException e1) {
-				throw e1;
-			} catch (Exception e2) {
-				throw new StatusException("授权服务器访问失败", e);
-			}
-		} catch (Exception e) {
-			throw new StatusException("授权服务器访问失败", e);
-		} finally {
-			try {
-				if (os != null) {
-					os.close();
-					os = null;
-				}
-				if (is != null) {
-					is.close();
-					is = null;
-				}
-			} catch (IOException e) {
-			}
-
-			if (conn != null) {
-				conn.disconnect();
-				conn = null;
-			}
-		}
-
-		return result;
-	}
-
-	/**
-	 * 获得连接请求的返回数据
-	 * 
-	 * @param conn
-	 * 
-	 * @return 字符串
-	 */
-	private String getResult(HttpURLConnection conn) throws IOException {
-
-		StringBuilder text = new StringBuilder();
-
-		InputStream is = null;
-		InputStreamReader sr = null;
-		BufferedReader br = null;
-
-		int code = conn.getResponseCode();
-
-		try {
-			is = code >= 400 ? conn.getErrorStream() : conn.getInputStream();
-
-			sr = new InputStreamReader(is, DEFAULT_CHARSET);
-			br = new BufferedReader(sr);
-
-			char[] chars = new char[4096];
-			int length = 0;
-
-			while ((length = br.read(chars)) != -1) {
-				text.append(chars, 0, length);
-			}
-		} finally {
-			if (br != null) {
-				br.close();
-				br = null;
-			}
-			if (sr != null) {
-				sr.close();
-				sr = null;
-			}
-			if (is != null) {
-				is.close();
-				is = null;
-			}
-		}
-		if (code >= 400) {
-			throw new IOException(text.toString());
-		}
-		return text.toString();
-	}
-
-	public AppLicense getAppInfo() {
-		String str = this.httpAction(null, null);
-		AppLicense appInfo = JSONObject.parseObject(str, AppLicense.class);
-		return appInfo;
-	}
-
-	public List<SchoolEntity> getOrgs() {
-		List<SchoolEntity> orgs = new ArrayList<SchoolEntity>();
-		int pageNumber = 1;
-		int pageSize = 100;
-		for (;;) {
-			Map<String, Object> datas = new HashMap<String, Object>();
-			datas.put("pageNumber", pageNumber);
-			datas.put("pageSize", pageSize);
-			String str = this.httpAction(null, datas);
-			JSONArray orgArray = JSONArray.parseArray(str);
-			for (int i = 0; i < orgArray.size(); i++) {
-				SchoolEntity school = JSONObject.parseObject(orgArray.getString(i), SchoolEntity.class);
-				orgs.add(school);
-			}
-			if (orgArray.size() != pageSize) {
-				break;
-			}
-			pageNumber++;
-		}
-
-		return orgs;
-	}
-}

+ 1 - 4
src/main/resources/application-test.properties

@@ -6,7 +6,7 @@ server.servlet.session.timeout=PT2H
 server.servlet.context-path=/
 spring.servlet.multipart.max-request-size=100MB
 spring.servlet.multipart.max-file-size=100MB
-com.qmth.mybatis.logLevel=error
+com.qmth.mybatis.log-level=error
 #
 # ********** db config **********
 #
@@ -29,10 +29,7 @@ session-timeout=7200
 
 wxapp-url=wxapp5.qmth.com.cn
 wxapp-appid=wx3564271a274bd400
-wxapp-secret=0bcbea00b9283890f3b6b0a672d5cda5
 
 markingcloud.server=http://192.168.10.224:8080
-qmth.solar.host=https://solar.qmth.com.cn
-qmth.solar.org.uri=/api/open/org/query
 com.qmth.solar.access-key=e5d4ccee19d34fcf99246a770ce62293
 com.qmth.solar.access-secret=Zfqq3nXAGLUGevDdJV1PieeKWJXCIKxG

+ 1 - 4
src/main/resources/application.properties

@@ -6,7 +6,7 @@ server.servlet.session.timeout=PT2H
 server.servlet.context-path=/
 spring.servlet.multipart.max-request-size=100MB
 spring.servlet.multipart.max-file-size=100MB
-com.qmth.mybatis.logLevel=error
+com.qmth.mybatis.log-level=error
 #
 # ********** db config **********
 #
@@ -29,10 +29,7 @@ session-timeout=7200
 
 wxapp-url=wxapp5.qmth.com.cn
 wxapp-appid=wx3564271a274bd400
-wxapp-secret=0bcbea00b9283890f3b6b0a672d5cda5
 
 markingcloud.server=http://192.168.10.224:8080
-qmth.solar.host=https://solar.qmth.com.cn
-qmth.solar.org.uri=/api/open/org/query
 com.qmth.solar.access-key=e5d4ccee19d34fcf99246a770ce62293
 com.qmth.solar.access-secret=Zfqq3nXAGLUGevDdJV1PieeKWJXCIKxG