Commit f70b2b3e by 黄森林

微信登录修改

parent 687f2bee
...@@ -3,10 +3,16 @@ package com.winsun.utils; ...@@ -3,10 +3,16 @@ package com.winsun.utils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
...@@ -15,7 +21,13 @@ import org.apache.http.util.EntityUtils; ...@@ -15,7 +21,13 @@ import org.apache.http.util.EntityUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.net.ssl.SSLContext;
import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;
import java.io.IOException; import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
/** /**
* HTTP请求封装 * HTTP请求封装
...@@ -171,6 +183,108 @@ public class HttpHelper { ...@@ -171,6 +183,108 @@ public class HttpHelper {
return null; return null;
} }
// 新方法 走代理
public static String sendGet(String url) {
String string = "";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
try {
HttpHost proxy = new HttpHost("172.18.101.170", 3128);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(200000).setConnectTimeout(200000).setProxy(proxy).build();
httpPost.setConfig(requestConfig);
CloseableHttpResponse result = httpClient.execute(httpPost);
string = EntityUtils.toString(result.getEntity(), "utf-8"); ;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
return string;
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
// 走代理
public static String sendPost(String url, String param) {
String string = "";
CloseableHttpClient httpClient = createSSLClientDefault();
HttpPost httpPost = new HttpPost(url);
StringEntity s = new StringEntity(param, "UTF-8"); // 中文乱码在此解决
s.setContentType("application/json");
httpPost.setEntity(s);
HttpResponse res;
try {
HttpHost proxy = new HttpHost("172.18.101.170", 3128);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(200000).setConnectTimeout(200000).setProxy(proxy).build();
httpPost.setConfig(requestConfig);
CloseableHttpResponse result = httpClient.execute(httpPost);
string = EntityUtils.toString(result.getEntity(), "utf-8");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}finally {
try {
httpClient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return string;
}
public static CloseableHttpClient createSSLClientDefault() {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(
null, new TrustStrategy() {
// 信任所有
public boolean isTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
return true;
}
@Override
public boolean isTrusted(
java.security.cert.X509Certificate[] arg0,
String arg1)
throws java.security.cert.CertificateException {
// TODO Auto-generated method stub
return false;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslContext);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
}
return HttpClients.createDefault();
}
} }
...@@ -43,6 +43,8 @@ import org.springframework.util.Assert; ...@@ -43,6 +43,8 @@ import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
...@@ -94,6 +96,11 @@ public class LoginPwdController extends BaseController { ...@@ -94,6 +96,11 @@ public class LoginPwdController extends BaseController {
LoginPwdController.stringRedisTemplate = stringRedisTemplate; LoginPwdController.stringRedisTemplate = stringRedisTemplate;
} }
@RequestMapping("/MP_verify_oYaGTxvtIJXLOkXB.txt")
public void auth(HttpServletResponse response) throws IOException {
response.getWriter().print("oYaGTxvtIJXLOkXB");
}
/** /**
* 微信授权 * 微信授权
* *
...@@ -106,13 +113,15 @@ public class LoginPwdController extends BaseController { ...@@ -106,13 +113,15 @@ public class LoginPwdController extends BaseController {
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?"; String url = "https://open.weixin.qq.com/connect/oauth2/authorize?";
url += "appid="; url += "appid=";
url += APPID; url += APPID;
url += "&redirect_uri=" + URLEncoder.encode("http://hhrcode.winsun-aly.com/#/login?state="+state, "UTF-8");//此处和微信会调用的域名相同 url += "&redirect_uri=" + URLEncoder.encode("http://hhrcode.winsun-aly.com/#/login", "UTF-8");//此处和微信会调用的域名相同
url += "&response_type=code&scope=snsapi_userinfo&state=STATE"; url += "&response_type=code&scope=snsapi_userinfo&state="+state;
url += "#wechat_redirect"; url += "#wechat_redirect";
log.info(url); log.info(url);
try { try {
response.sendRedirect(url); RequestDispatcher rd = request.getRequestDispatcher(url);
} catch (IOException e) { rd.forward(request,response);
/* response.sendRedirect(url);*/
} catch (Exception e) {
log.error("微信授权异常", e.getMessage()); log.error("微信授权异常", e.getMessage());
} }
} }
...@@ -131,10 +140,10 @@ public class LoginPwdController extends BaseController { ...@@ -131,10 +140,10 @@ public class LoginPwdController extends BaseController {
url += APPID; url += APPID;
url += "&redirect_uri=" + URLEncoder.encode("http://167460x6b0.51mypc.cn/ciop/forgerpw/callBackLogin", "UTF-8");//此处和微信会调用的域名相同 url += "&redirect_uri=" + URLEncoder.encode("http://167460x6b0.51mypc.cn/ciop/forgerpw/callBackLogin", "UTF-8");//此处和微信会调用的域名相同
url += "&response_type=code&scope=snsapi_userinfo"; url += "&response_type=code&scope=snsapi_userinfo";
url += "&state=#wechat_redirect"; url += "&state="+"http%3A%2F%2Flocalhost%3A3048%2F%23%2Fsupervisor%3Fid%3D7678%26sign%3Df543efe0512735b8b559b7058a6799ea"+"#wechat_redirect";
try { try {
response.sendRedirect(url); response.sendRedirect(url);
} catch (IOException e) { } catch (Exception e) {
log.error("微信授权异常", e.getMessage()); log.error("微信授权异常", e.getMessage());
} }
} }
...@@ -164,16 +173,17 @@ public class LoginPwdController extends BaseController { ...@@ -164,16 +173,17 @@ public class LoginPwdController extends BaseController {
url += "&secret="; url += "&secret=";
url += APPSECRET; url += APPSECRET;
url += "&code=" + code + "&grant_type=authorization_code"; url += "&code=" + code + "&grant_type=authorization_code";
JSONObject jsonObject = null; String jsonObject = null;
try { try {
jsonObject = HttpHelper.doGet(url); jsonObject = HttpHelper.sendGet(url);
} catch (Exception e) { } catch (Exception e) {
log.error("微信登录获取用户信息失败", e.getMessage()); log.error("微信登录获取用户信息失败", e.getMessage());
} }
if (jsonObject == null) { if (jsonObject == null) {
return ResponseData.error("微信授权失效,请重新授权!"); return ResponseData.error("微信授权失效,请重新授权!");
} }
String openId = jsonObject.getString("openid"); Map<String,Object> map = JSON.parseObject(jsonObject, Map.class);
String openId = map.get("openid").toString();
Wrapper<HhrUser> wrapper = new EntityWrapper<>(); Wrapper<HhrUser> wrapper = new EntityWrapper<>();
wrapper.eq("open_id", openId); wrapper.eq("open_id", openId);
List<HhrUser> hhrUsers = hhrUserMapper.selectList(wrapper); List<HhrUser> hhrUsers = hhrUserMapper.selectList(wrapper);
...@@ -223,16 +233,17 @@ public class LoginPwdController extends BaseController { ...@@ -223,16 +233,17 @@ public class LoginPwdController extends BaseController {
url += "&secret="; url += "&secret=";
url += APPSECRET; url += APPSECRET;
url += "&code=" + code + "&grant_type=authorization_code"; url += "&code=" + code + "&grant_type=authorization_code";
JSONObject jsonObject = null; String jsonObject = null;
try { try {
jsonObject = HttpHelper.doGet(url); jsonObject = HttpHelper.sendGet(url);
} catch (Exception e) { } catch (Exception e) {
log.error("微信登录获取用户信息失败", e.getMessage()); log.error("微信登录获取用户信息失败", e.getMessage());
} }
if (jsonObject == null) { if (jsonObject == null) {
return ResponseData.error("微信授权失效,请重新授权!"); return ResponseData.error("微信授权失效,请重新授权!");
} }
String openId = jsonObject.getString("openid"); Map<String,Object> map = JSON.parseObject(jsonObject, Map.class);
String openId = map.get("openid").toString();
SysUser sysUser = users.get(0); SysUser sysUser = users.get(0);
Wrapper<HhrUser> wrapper = new EntityWrapper<>(); Wrapper<HhrUser> wrapper = new EntityWrapper<>();
wrapper.eq("id", sysUser.getId()); wrapper.eq("id", sysUser.getId());
......
...@@ -8,11 +8,9 @@ import org.springframework.stereotype.Controller; ...@@ -8,11 +8,9 @@ import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@Controller @Controller
public class WeixinAuthController { public class WeixinAuthController {
@RequestMapping("/MP_verify_oYaGTxvtIJXLOkXB.txt")
public void auth(HttpServletResponse response) throws IOException {
response.getWriter().print("oYaGTxvtIJXLOkXB");
}
} }
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