首先需要
AgentId:xxxxxxxx(应用id)
Secret:xxxxxxxxxxxxxxxxxxxxxxxxx
企业ID:xxxxxxxxxxxxxxxxx
应用地址:http://localhost:8080/_war_exploded/wechat/index
应用地址需要在企业微信应用上配置
切记 应用地址必须是外网可以访问的
企业微信点击后会进入以下请求
/**
* @描述 首页
* @参数 [request, response]
* @返回值 org.springframework.web.servlet.ModelAndView
* @创建人 lhr
* @创建时间 2018/12/25
**/
@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws IOException {
ModelAndView mv = new ModelAndView("wechat/index");
mv.addObject("url", "/wechat/index");
mv = wechatTokenBean.renderPage(request, response, mv);
return mv;
}
获取token secret上面需要提供 corpid为应用id
获取token地址
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=应用id&corpsecret=secret
获取指定url数据
//url路径 param参数
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
把请求到的json转成对象
JSON.parseObject(result, clazz);
在对象中就可以get到token
让请求去验证是否是企业微信进入的该请求 并且需要拿到code
这个code用来获取当前企业微信的用户
url表示 去验证后回调回来的请求路径 在回调回来的请i求路径里会携带code
response.sendRedirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + this.getAppId() + "&redirect_uri=" + uri + "&response_type=code&scope=snsapi_userinfo&state=" +this.accessToken + "#wechat_redirect");
回调回来的请求中可以
获取code
String code = request.getParameter("code");//如果code有值,证明已经授权
//根据code获取用户名
https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=token&code=code
Map<String, String> params = new HashMap<>();
params.put("access_token",accessToken);
// params.put("secret", this.getAppSecret());
params.put("code", code);
// params.put("grant_type", "authorization_code");
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo";
//去该地址获取到用户名
String res = HttpUtils.sendGet(url, params);
//把地址返回的json转成对象 private String errcode;
// private String errmsg;
// private String UserId;
// private String DeviceId;
// private String user_ticket;
// private String expires_in;
QywechatResult userToken = JsonUtils.parseObject(res, QywechatResult.class);
Subject subject = SecurityUtils.getSubject();
subject.login(new UsernamePasswordToken("xxxx#"+userToken.getUserId(),MD5Util.MD5("111111"), false));
然后就可以根据用户名进行验证 然后进入手机端首页