diff --git a/pom.xml b/pom.xml index 8e2f8b3..2fde3d2 100644 --- a/pom.xml +++ b/pom.xml @@ -13,22 +13,13 @@ 0.0.1-SNAPSHOT JdcProject JdcProject - - - - - - - - - - - - - + jar + 17 + 17 + UTF-8 17 - 1.18.4 + 1.18.20 8.0.33 1.2.19 3.5.8 @@ -42,6 +33,7 @@ 5.0.0 2.17.0 2.8.6 + 4.0.3 @@ -105,7 +97,11 @@ commons-lang ${commons-lang.version} - + + com.alibaba + easyexcel + ${easyexcel.version} + org.eclipse.jetty jetty-util diff --git a/src/main/java/com/jdc/jdcproject/controller/WxApiController.java b/src/main/java/com/jdc/jdcproject/controller/WxApiController.java new file mode 100644 index 0000000..217e252 --- /dev/null +++ b/src/main/java/com/jdc/jdcproject/controller/WxApiController.java @@ -0,0 +1,120 @@ +package com.jdc.jdcproject.controller; + +import com.google.gson.Gson; +import com.jdc.jdcproject.entity.Users; +import com.jdc.jdcproject.exceptionhandler.JdcException; +import com.jdc.jdcproject.service.IUsersService; +import com.jdc.jdcproject.utils.ConstantUtils; +import com.jdc.jdcproject.utils.HttpUtils; +import com.jdc.jdcproject.utils.JwtUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpSession; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.HashMap; + +@Controller +@RequestMapping("/api/ucenter/wx") +@CrossOrigin +public class WxApiController { + + @Autowired + private IUsersService usersService; + + @GetMapping("login") + public String genQrConnect(HttpSession session) { + // 微信开放平台授权baseUrl + String baseUrl = "https://open.weixin.qq.com/connect/qrconnect" + + "?appid=%s" + + "&redirect_uri=%s" + + "&response_type=code" + + "&scope=snsapi_login" + + "&state=%s" + + "#wechat_redirect"; + // 回调地址 + String redirectUrl = ConstantUtils.WX_OPEN_REDIRECT_URL; //获取业务服务器重定向地址 + try { + redirectUrl = URLEncoder.encode(redirectUrl, "UTF-8"); //url编码 + } catch (UnsupportedEncodingException e) { + throw new JdcException(20001, e.getMessage()); + } + + // 防止csrf攻击(跨站请求伪造攻击) + //String state = UUID.randomUUID().toString().replaceAll("-", "");//一般情况下会使用一个随机数 + String state = "imhelen";//为了让大家能够使用我搭建的外网的微信回调跳转服务器,这里填写你在ngrok的前置域名 + System.out.println("state = " + state); + + // 采用redis等进行缓存state 使用sessionId为key 30分钟后过期,可配置 + //键:"wechar-open-state-" + httpServletRequest.getSession().getId() + //值:satte + //过期时间:30分钟 + + //生成qrcodeUrl + String qrcodeUrl = String.format( + baseUrl, + ConstantUtils.WX_OPEN_APP_ID, + redirectUrl, + state); + + + return "redirect:" + qrcodeUrl; + } + + @GetMapping("callback") + public String callback(String code, String state, HttpSession session) { + try { + //得到授权临时票据code + System.out.println("code = " + code); + System.out.println("state = " + state); + + //向认证服务器发送请求换取access_token + String baseAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token" + + "?appid=%s" + + "&secret=%s" + + "&code=%s" + + "&grant_type=authorization_code"; + + String accessTokenUrl = String.format(baseAccessTokenUrl, + ConstantUtils.WX_OPEN_APP_ID, + ConstantUtils.WX_OPEN_APP_SECRET, + code); + String result = HttpUtils.get(accessTokenUrl); + System.out.println("accessToken=============" + result); + + //解析json字符串 + Gson gson = new Gson(); + HashMap map = gson.fromJson(result, HashMap.class); + String accessToken = (String) map.get("access_token"); + String openid = (String) map.get("openid"); + + //查询数据库当前用用户是否曾经使用过微信登录 + /* Users member = usersService.getByOpenid(openid); + if (member == null) { + System.out.println("新用户注册"); + //访问微信的资源服务器,获取用户信息 + String baseUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo" + + "?access_token=%s" + + "&openid=%s"; + String userInfoUrl = String.format(baseUserInfoUrl, accessToken, openid); + String resultUserInfo = null; + resultUserInfo = HttpUtils.get(userInfoUrl); + System.out.println("resultUserInfo==========" + resultUserInfo); + HashMap mapUserInfo = gson.fromJson(resultUserInfo, HashMap.class); + String nickname = (String) mapUserInfo.get("nickname"); + String headimgurl = (String) mapUserInfo.get("headimgurl"); + + }*/ + //String memberIdByJwtToken = JwtUtils.getJwtToken(member.getId(), member.getUsername()); + return "redirect:http://localhost:8000?token=" ; + } catch (Exception e) { + throw new JdcException(20001, "登陆失败"); + } + } + +} \ No newline at end of file diff --git a/src/main/java/com/jdc/jdcproject/service/impl/UsersServiceImpl.java b/src/main/java/com/jdc/jdcproject/service/impl/UsersServiceImpl.java index 47c6157..c16d1e1 100644 --- a/src/main/java/com/jdc/jdcproject/service/impl/UsersServiceImpl.java +++ b/src/main/java/com/jdc/jdcproject/service/impl/UsersServiceImpl.java @@ -56,6 +56,7 @@ public class UsersServiceImpl extends ServiceImpl implements // 校验密码 if (!MD5.encrypt(password).equals(users.getPassword())) { + // if (passwprd.equals(users.getPassword())) { // 密码错误,增加错误次数 Integer errorCount = users.getErrlogincount() == null ? 0 : users.getErrlogincount(); errorCount++; diff --git a/src/main/java/com/jdc/jdcproject/utils/ConstantUtils.java b/src/main/java/com/jdc/jdcproject/utils/ConstantUtils.java new file mode 100644 index 0000000..4bd214f --- /dev/null +++ b/src/main/java/com/jdc/jdcproject/utils/ConstantUtils.java @@ -0,0 +1,30 @@ +package com.jdc.jdcproject.utils; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.stereotype.Component; + +@Component +public class ConstantUtils implements InitializingBean { + + @Value("${wx.open.app_id}") + private String appId; + + @Value("${wx.open.app_secret}") + private String appSecret; + + @Value("${wx.open.redirect_url}") + private String redirectUrl; + + public static String WX_OPEN_APP_ID; + public static String WX_OPEN_APP_SECRET; + public static String WX_OPEN_REDIRECT_URL; + + @Override + public void afterPropertiesSet() throws Exception { + WX_OPEN_APP_ID = appId; + WX_OPEN_APP_SECRET = appSecret; + WX_OPEN_REDIRECT_URL = redirectUrl; + } +} \ No newline at end of file diff --git a/src/main/java/com/jdc/jdcproject/utils/HttpUtils.java b/src/main/java/com/jdc/jdcproject/utils/HttpUtils.java new file mode 100644 index 0000000..9dda203 --- /dev/null +++ b/src/main/java/com/jdc/jdcproject/utils/HttpUtils.java @@ -0,0 +1,764 @@ +package com.jdc.jdcproject.utils; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.SocketTimeoutException; +import java.net.URLEncoder; +import java.security.GeneralSecurityException; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import java.text.ParseException; +import java.util.*; + +import javax.net.ssl.*; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.http.Consts; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.HttpClient; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.*; +import org.apache.http.client.utils.HttpClientUtils; +import org.apache.http.conn.ClientConnectionManager; +import org.apache.http.conn.ConnectTimeoutException; +import org.apache.http.conn.scheme.Scheme; +import org.apache.http.conn.scheme.SchemeRegistry; +import org.apache.http.conn.ssl.*; +import org.apache.http.conn.ssl.SSLSocketFactory; +import org.apache.http.entity.ByteArrayEntity; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; + + +public class HttpUtils { + + public static final int connTimeout = 10000; + public static final int readTimeout = 10000; + public static final String charset = "UTF-8"; + private static HttpClient client = null; + private String url; + private Map param; + private int statusCode; + private String content; + private String xmlParam; + private boolean isHttps; + + /** + * get + * + * @param host + * @param path + * @param method + * @param headers + * @param querys + * @return + * @throws Exception + */ + public static HttpResponse doGet(String host, String path, String method, + Map headers, + Map querys) + throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpGet request = new HttpGet(buildUrl(host, path, querys)); + for (Map.Entry e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + return httpClient.execute(request); + } + + /** + * post form + * + * @param host + * @param path + * @param method + * @param headers + * @param querys + * @param bodys + * @return + * @throws Exception + */ + public static HttpResponse doPost(String host, String path, String method, + Map headers, + Map querys, + Map bodys) + throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpPost request = new HttpPost(buildUrl(host, path, querys)); + for (Map.Entry e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + if (bodys != null) { + List nameValuePairList = new ArrayList(); + + for (String key : bodys.keySet()) { + nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key))); + } + UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8"); + formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8"); + request.setEntity(formEntity); + } + + return httpClient.execute(request); + } + + /** + * Post String + * + * @param host + * @param path + * @param method + * @param headers + * @param querys + * @param body + * @return + * @throws Exception + */ + public static HttpResponse doPost(String host, String path, String method, + Map headers, + Map querys, + String body) + throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpPost request = new HttpPost(buildUrl(host, path, querys)); + for (Map.Entry e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + if (StringUtils.isNotBlank(body)) { + request.setEntity(new StringEntity(body, "utf-8")); + } + + return httpClient.execute(request); + } + + /** + * Post stream + * + * @param host + * @param path + * @param method + * @param headers + * @param querys + * @param body + * @return + * @throws Exception + */ + public static HttpResponse doPost(String host, String path, String method, + Map headers, + Map querys, + byte[] body) + throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpPost request = new HttpPost(buildUrl(host, path, querys)); + for (Map.Entry e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + if (body != null) { + request.setEntity(new ByteArrayEntity(body)); + } + + return httpClient.execute(request); + } + + /** + * Put String + * + * @param host + * @param path + * @param method + * @param headers + * @param querys + * @param body + * @return + * @throws Exception + */ + public static HttpResponse doPut(String host, String path, String method, + Map headers, + Map querys, + String body) + throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpPut request = new HttpPut(buildUrl(host, path, querys)); + for (Map.Entry e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + if (StringUtils.isNotBlank(body)) { + request.setEntity(new StringEntity(body, "utf-8")); + } + + return httpClient.execute(request); + } + + /** + * Put stream + * + * @param host + * @param path + * @param method + * @param headers + * @param querys + * @param body + * @return + * @throws Exception + */ + public static HttpResponse doPut(String host, String path, String method, + Map headers, + Map querys, + byte[] body) + throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpPut request = new HttpPut(buildUrl(host, path, querys)); + for (Map.Entry e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + if (body != null) { + request.setEntity(new ByteArrayEntity(body)); + } + + return httpClient.execute(request); + } + + /** + * Delete + * + * @param host + * @param path + * @param method + * @param headers + * @param querys + * @return + * @throws Exception + */ + public static HttpResponse doDelete(String host, String path, String method, + Map headers, + Map querys) + throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpDelete request = new HttpDelete(buildUrl(host, path, querys)); + for (Map.Entry e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + return httpClient.execute(request); + } + + private static String buildUrl(String host, String path, Map querys) throws UnsupportedEncodingException { + StringBuilder sbUrl = new StringBuilder(); + sbUrl.append(host); + if (!StringUtils.isBlank(path)) { + sbUrl.append(path); + } + if (null != querys) { + StringBuilder sbQuery = new StringBuilder(); + for (Map.Entry query : querys.entrySet()) { + if (0 < sbQuery.length()) { + sbQuery.append("&"); + } + if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) { + sbQuery.append(query.getValue()); + } + if (!StringUtils.isBlank(query.getKey())) { + sbQuery.append(query.getKey()); + if (!StringUtils.isBlank(query.getValue())) { + sbQuery.append("="); + sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8")); + } + } + } + if (0 < sbQuery.length()) { + sbUrl.append("?").append(sbQuery); + } + } + + return sbUrl.toString(); + } + + private static HttpClient wrapClient(String host) { + HttpClient httpClient = new DefaultHttpClient(); + if (host.startsWith("https://")) { + sslClient(httpClient); + } + + return httpClient; + } + + private static void sslClient(HttpClient httpClient) { + try { + SSLContext ctx = SSLContext.getInstance("TLS"); + X509TrustManager tm = new X509TrustManager() { + public X509Certificate[] getAcceptedIssuers() { + return null; + } + + public void checkClientTrusted(X509Certificate[] xcs, String str) { + + } + + public void checkServerTrusted(X509Certificate[] xcs, String str) { + + } + }; + ctx.init(null, new TrustManager[]{tm}, null); + SSLSocketFactory ssf = new SSLSocketFactory(ctx); + ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + ClientConnectionManager ccm = httpClient.getConnectionManager(); + SchemeRegistry registry = ccm.getSchemeRegistry(); + registry.register(new Scheme("https", 443, ssf)); + } catch (KeyManagementException ex) { + throw new RuntimeException(ex); + } catch (NoSuchAlgorithmException ex) { + throw new RuntimeException(ex); + } + } + /**======================================================================================================================================*/ + /**======================================================================================================================================*/ + /**======================================================================================================================================*/ + /**======================================================================================================================================*/ + /**======================================================================================================================================*/ + /**======================================================================================================================================*/ + static { + PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); + cm.setMaxTotal(128); + cm.setDefaultMaxPerRoute(128); + client = HttpClients.custom().setConnectionManager(cm).build(); + } + + public static String postParameters(String url, String parameterStr) throws ConnectTimeoutException, SocketTimeoutException, Exception { + return post(url, parameterStr, "application/x-www-form-urlencoded", charset, connTimeout, readTimeout); + } + + public static String postParameters(String url, String parameterStr, String charset, Integer connTimeout, Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception { + return post(url, parameterStr, "application/x-www-form-urlencoded", charset, connTimeout, readTimeout); + } + + public static String postParameters(String url, Map params) throws ConnectTimeoutException, + SocketTimeoutException, Exception { + return postForm(url, params, null, connTimeout, readTimeout); + } + + public static String postParameters(String url, Map params, Integer connTimeout, Integer readTimeout) throws ConnectTimeoutException, + SocketTimeoutException, Exception { + return postForm(url, params, null, connTimeout, readTimeout); + } + + public static String get(String url) throws Exception { + return get(url, charset, null, null); + } + + public static String get(String url, String charset) throws Exception { + return get(url, charset, connTimeout, readTimeout); + } + + /** + * 发送一个 Post 请求, 使用指定的字符集编码. + * + * @param url + * @param body RequestBody + * @param mimeType 例如 application/xml "application/x-www-form-urlencoded" a=1&b=2&c=3 + * @param charset 编码 + * @param connTimeout 建立链接超时时间,毫秒. + * @param readTimeout 响应超时时间,毫秒. + * @return ResponseBody, 使用指定的字符集编码. + * @throws ConnectTimeoutException 建立链接超时异常 + * @throws SocketTimeoutException 响应超时 + * @throws Exception + */ + public static String post(String url, String body, String mimeType, String charset, Integer connTimeout, Integer readTimeout) + throws ConnectTimeoutException, SocketTimeoutException, Exception { + HttpClient client = null; + HttpPost post = new HttpPost(url); + String result = ""; + try { + if (StringUtils.isNotBlank(body)) { + HttpEntity entity = new StringEntity(body, ContentType.create(mimeType, charset)); + post.setEntity(entity); + } + // 设置参数 + RequestConfig.Builder customReqConf = RequestConfig.custom(); + if (connTimeout != null) { + customReqConf.setConnectTimeout(connTimeout); + } + if (readTimeout != null) { + customReqConf.setSocketTimeout(readTimeout); + } + post.setConfig(customReqConf.build()); + + HttpResponse res; + if (url.startsWith("https")) { + // 执行 Https 请求. + client = createSSLInsecureClient(); + res = client.execute(post); + } else { + // 执行 Http 请求. + client = HttpUtils.client; + res = client.execute(post); + } + result = IOUtils.toString(res.getEntity().getContent(), charset); + } finally { + post.releaseConnection(); + if (url.startsWith("https") && client != null && client instanceof CloseableHttpClient) { + ((CloseableHttpClient) client).close(); + } + } + return result; + } + + + /** + * 提交form表单 + * + * @param url + * @param params + * @param connTimeout + * @param readTimeout + * @return + * @throws ConnectTimeoutException + * @throws SocketTimeoutException + * @throws Exception + */ + public static String postForm(String url, Map params, Map headers, Integer connTimeout, Integer readTimeout) throws ConnectTimeoutException, + SocketTimeoutException, Exception { + + HttpClient client = null; + HttpPost post = new HttpPost(url); + try { + if (params != null && !params.isEmpty()) { + List formParams = new ArrayList(); + Set> entrySet = params.entrySet(); + for (Map.Entry entry : entrySet) { + formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + } + UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, Consts.UTF_8); + post.setEntity(entity); + } + + if (headers != null && !headers.isEmpty()) { + for (Map.Entry entry : headers.entrySet()) { + post.addHeader(entry.getKey(), entry.getValue()); + } + } + // 设置参数 + RequestConfig.Builder customReqConf = RequestConfig.custom(); + if (connTimeout != null) { + customReqConf.setConnectTimeout(connTimeout); + } + if (readTimeout != null) { + customReqConf.setSocketTimeout(readTimeout); + } + post.setConfig(customReqConf.build()); + HttpResponse res = null; + if (url.startsWith("https")) { + // 执行 Https 请求. + client = createSSLInsecureClient(); + res = client.execute(post); + } else { + // 执行 Http 请求. + client = HttpUtils.client; + res = client.execute(post); + } + return IOUtils.toString(res.getEntity().getContent(), "UTF-8"); + } finally { + post.releaseConnection(); + if (url.startsWith("https") && client != null + && client instanceof CloseableHttpClient) { + ((CloseableHttpClient) client).close(); + } + } + } + + + /** + * 发送一个 GET 请求 + * + * @param url + * @param charset + * @param connTimeout 建立链接超时时间,毫秒. + * @param readTimeout 响应超时时间,毫秒. + * @return + * @throws ConnectTimeoutException 建立链接超时 + * @throws SocketTimeoutException 响应超时 + * @throws Exception + */ + public static String get(String url, String charset, Integer connTimeout, Integer readTimeout) + throws ConnectTimeoutException, SocketTimeoutException, Exception { + + HttpClient client = null; + HttpGet get = new HttpGet(url); + String result = ""; + try { + // 设置参数 + RequestConfig.Builder customReqConf = RequestConfig.custom(); + if (connTimeout != null) { + customReqConf.setConnectTimeout(connTimeout); + } + if (readTimeout != null) { + customReqConf.setSocketTimeout(readTimeout); + } + get.setConfig(customReqConf.build()); + + HttpResponse res = null; + + if (url.startsWith("https")) { + // 执行 Https 请求. + client = createSSLInsecureClient(); + res = client.execute(get); + } else { + // 执行 Http 请求. + client = HttpUtils.client; + res = client.execute(get); + } + + result = IOUtils.toString(res.getEntity().getContent(), charset); + } finally { + get.releaseConnection(); + if (url.startsWith("https") && client != null && client instanceof CloseableHttpClient) { + ((CloseableHttpClient) client).close(); + } + } + return result; + } + + + /** + * 从 response 里获取 charset + * + * @param ressponse + * @return + */ + @SuppressWarnings("unused") + private static String getCharsetFromResponse(HttpResponse ressponse) { + // Content-Type:text/html; charset=GBK + if (ressponse.getEntity() != null && ressponse.getEntity().getContentType() != null && ressponse.getEntity().getContentType().getValue() != null) { + String contentType = ressponse.getEntity().getContentType().getValue(); + if (contentType.contains("charset=")) { + return contentType.substring(contentType.indexOf("charset=") + 8); + } + } + return null; + } + + + /** + * 创建 SSL连接 + * + * @return + * @throws GeneralSecurityException + */ + private static CloseableHttpClient createSSLInsecureClient() throws GeneralSecurityException { + try { + SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { + public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { + return true; + } + }).build(); + + SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new X509HostnameVerifier() { + + @Override + public boolean verify(String arg0, SSLSession arg1) { + return true; + } + + @Override + public void verify(String host, SSLSocket ssl) + throws IOException { + } + + @Override + public void verify(String host, X509Certificate cert) + throws SSLException { + } + + @Override + public void verify(String host, String[] cns, + String[] subjectAlts) throws SSLException { + } + + }); + + return HttpClients.custom().setSSLSocketFactory(sslsf).build(); + + } catch (GeneralSecurityException e) { + throw e; + } + } + + public static void main(String[] args) { + try { + String str = post("https://localhost:443/ssl/test.shtml", "name=12&page=34", "application/x-www-form-urlencoded", "UTF-8", 10000, 10000); + //String str= get("https://localhost:443/ssl/test.shtml?name=12&page=34","GBK"); + /*Map map = new HashMap(); + map.put("name", "111"); + map.put("page", "222"); + String str= postForm("https://localhost:443/ssl/test.shtml",map,null, 10000, 10000);*/ + System.out.println(str); + } catch (ConnectTimeoutException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SocketTimeoutException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + /**======================================================================================================================================*/ + /**======================================================================================================================================*/ + /**======================================================================================================================================*/ + /**======================================================================================================================================*/ + /**======================================================================================================================================*/ + /**======================================================================================================================================*/ + public boolean isHttps() { + return isHttps; + } + + public void setHttps(boolean isHttps) { + this.isHttps = isHttps; + } + + public String getXmlParam() { + return xmlParam; + } + + public void setXmlParam(String xmlParam) { + this.xmlParam = xmlParam; + } + + public HttpUtils(String url, Map param) { + this.url = url; + this.param = param; + } + + public HttpUtils(String url) { + this.url = url; + } + + public void setParameter(Map map) { + param = map; + } + + public void addParameter(String key, String value) { + if (param == null) + param = new HashMap(); + param.put(key, value); + } + + public void post() throws ClientProtocolException, IOException { + HttpPost http = new HttpPost(url); + setEntity(http); + execute(http); + } + + public void put() throws ClientProtocolException, IOException { + HttpPut http = new HttpPut(url); + setEntity(http); + execute(http); + } + + public void get() throws ClientProtocolException, IOException { + if (param != null) { + StringBuilder url = new StringBuilder(this.url); + boolean isFirst = true; + for (String key : param.keySet()) { + if (isFirst) + url.append("?"); + else + url.append("&"); + url.append(key).append("=").append(param.get(key)); + } + this.url = url.toString(); + } + HttpGet http = new HttpGet(url); + execute(http); + } + + /** + * set http post,put param + */ + private void setEntity(HttpEntityEnclosingRequestBase http) { + if (param != null) { + List nvps = new LinkedList(); + for (String key : param.keySet()) + nvps.add(new BasicNameValuePair(key, param.get(key))); // 参数 + http.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // 设置参数 + } + if (xmlParam != null) { + http.setEntity(new StringEntity(xmlParam, Consts.UTF_8)); + } + } + + private void execute(HttpUriRequest http) throws ClientProtocolException, + IOException { + CloseableHttpClient httpClient = null; + try { + if (isHttps) { + SSLContext sslContext = new SSLContextBuilder() + .loadTrustMaterial(null, new TrustStrategy() { + // 信任所有 + public boolean isTrusted(X509Certificate[] chain, + String authType) + throws CertificateException { + return true; + } + }).build(); + SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( + sslContext); + httpClient = HttpClients.custom().setSSLSocketFactory(sslsf) + .build(); + } else { + httpClient = HttpClients.createDefault(); + } + CloseableHttpResponse response = httpClient.execute(http); + try { + if (response != null) { + if (response.getStatusLine() != null) + statusCode = response.getStatusLine().getStatusCode(); + HttpEntity entity = response.getEntity(); + // 响应内容 + content = EntityUtils.toString(entity, Consts.UTF_8); + } + } finally { + response.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + httpClient.close(); + } + } + + public int getStatusCode() { + return statusCode; + } + + public String getContent() throws ParseException, IOException { + return content; + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/MANIFEST.MF b/src/main/resources/META-INF/MANIFEST.MF new file mode 100644 index 0000000..c53078c --- /dev/null +++ b/src/main/resources/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: com.jdc.jdcproject.JdcProjectApplication + diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 82b8539..87e295d 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -18,3 +18,8 @@ server: springdoc: swagger-ui: path: /swagger-ui.html +wx: + open: + app_id: + app_secret: + redirect_url: http://localhost:8000/ diff --git a/src/test/java/com/jdc/jdcproject/EasyexcelReader.java b/src/test/java/com/jdc/jdcproject/EasyexcelReader.java new file mode 100644 index 0000000..5f937c0 --- /dev/null +++ b/src/test/java/com/jdc/jdcproject/EasyexcelReader.java @@ -0,0 +1,16 @@ +package com.jdc.jdcproject; + +import com.alibaba.excel.EasyExcel; + +public class EasyexcelReader { + + public static void main(String[] args) { + String fileName = "D:\\ProgramFiles\\project\\JdcProject\\src\\test\\test.xlsx"; + + EasyExcel.read(fileName, new ExcelListener(data->{ + + }))// 从第3行开始是数据 + .sheet() + .doRead(); + } +} diff --git a/src/test/java/com/jdc/jdcproject/ExcelListener.java b/src/test/java/com/jdc/jdcproject/ExcelListener.java new file mode 100644 index 0000000..ae56f77 --- /dev/null +++ b/src/test/java/com/jdc/jdcproject/ExcelListener.java @@ -0,0 +1,23 @@ +package com.jdc.jdcproject; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; +import java.util.Map; + +public class ExcelListener extends AnalysisEventListener> { + + @Override + public void invoke(Map data, AnalysisContext context) { + System.out.println("读取到数据:" + data); + } + + @Override + public void invokeHeadMap(Map headMap, AnalysisContext context) { + System.out.println("表头:" + headMap); + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + System.out.println("所有数据读取完成"); + } +} diff --git a/src/test/java/com/jdc/jdcproject/MineWorkEntity.java b/src/test/java/com/jdc/jdcproject/MineWorkEntity.java new file mode 100644 index 0000000..7450fe8 --- /dev/null +++ b/src/test/java/com/jdc/jdcproject/MineWorkEntity.java @@ -0,0 +1,41 @@ +package com.jdc.jdcproject; + +import com.alibaba.excel.annotation.ExcelProperty; +import lombok.Data; + +@Data +public class MineWorkEntity { + + @ExcelProperty(value = "日期",index = 0) + private String date; + + @ExcelProperty(value = "白班", index = 1) + private String dayShift; + + @ExcelProperty(value = "大厂要矿时间",index = 2) + private String dayRequestTime; + + @ExcelProperty(value = "下去时间",index = 3) + private String dayDownTime; + + @ExcelProperty(value = "11节列数",index = 4) + private String day11Count; + + @ExcelProperty(value = "夜班",index = 5) + private String nightShift; + + @ExcelProperty(value = "大厂要矿时间",index = 6) + private String nightRequestTime; + + @ExcelProperty(value = "下去时间",index = 7) + private String nightDownTime; + + @ExcelProperty(value = "11节列数",index = 8) + private String night11Count; + + @ExcelProperty(value = "夜班早晨7点以后下",index = 9) + private String nightAfter7; + + @ExcelProperty(value = "白班早晨8点以后回来",index = 10) + private String dayAfter8Return; +} diff --git a/src/test/java/com/jdc/jdcproject/Testexcel.java b/src/test/java/com/jdc/jdcproject/Testexcel.java new file mode 100644 index 0000000..0e29ee0 --- /dev/null +++ b/src/test/java/com/jdc/jdcproject/Testexcel.java @@ -0,0 +1,21 @@ +package com.jdc.jdcproject; + +import com.alibaba.excel.annotation.ExcelProperty; +import lombok.Data; + +@Data +public class Testexcel { + + @ExcelProperty("姓名") + private String name; + + @ExcelProperty("年龄") + private String age; + + @ExcelProperty("性别") + private String sex; + + @ExcelProperty("备注") + private String beizhu; + +} diff --git a/src/test/test.xlsx b/src/test/test.xlsx new file mode 100644 index 0000000..09e76e9 Binary files /dev/null and b/src/test/test.xlsx differ diff --git a/src/test/~$副本2025年4月份电机车运输综合报表.xlsx b/src/test/~$副本2025年4月份电机车运输综合报表.xlsx new file mode 100644 index 0000000..da8a2b1 Binary files /dev/null and b/src/test/~$副本2025年4月份电机车运输综合报表.xlsx differ diff --git a/src/test/副本2025年4月份电机车运输综合报表.xlsx b/src/test/副本2025年4月份电机车运输综合报表.xlsx new file mode 100644 index 0000000..9604515 Binary files /dev/null and b/src/test/副本2025年4月份电机车运输综合报表.xlsx differ