概述
提醒:本文章可能需要一定的java基础,所需jar包也需要自己寻找
效果图:
扫描后获取的数据
openid:(微信唯一标识)
headimgurl:头像
nickname:昵称
(其他就不列举啦)
1.注册微信开放平台开发者账号并创建一个或一个以上的网站应用
点击查看进入下图
2.把授权回调域改成自己的
授权回调域:域名
注:花生壳可以免费注册域名,并且映射至外网测试
3.一个类搞定登录,LoginAction
package com.t5.weixin;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
@Controller
@RequestMapping(value = "/WeCat")
public class LoginAction {
@RequestMapping(value = "/login")
public void login(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
System.out.println("================>>");
String url = "https://open.weixin.qq.com/connect/qrconnect?";
url += "appid=你的appid";
url += "&redirect_uri=" + URLEncoder.encode("http://169l5w4498.imwork.net:16282/WeCat/callBackLogin", "UTF-8"); //此处和微信会调用的域名相同
url += "&response_type=code&scope=snsapi_login";
url += "&state=" + request.getSession().getId() + "#wechat_redirect";
//redirect_uri;指定回调路径
try {
response.sendRedirect(url);
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping(value = "/callBackLogin")
public String callBackLogin(HttpServletRequest request, HttpServletResponse response,Model model) {
System.out.println("callBackLogin....");
// return "redirect:../loginSuccess.jsp";
String code = request.getParameter("code");
String state = request.getParameter("state");
System.out.println("code=" + code);
System.out.println("state=" + state);
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?";
url += "appid=你的appid";
url += "&secret=你的secret";
url += "&code=" + code + "&grant_type=authorization_code";
JSONObject jsonObject = this.httpGet(url);
String at = jsonObject.getString("access_token");//获取微信开放平台票据号
String openId = jsonObject.getString("openid");
System.out.println("at="+at);
url="https://api.weixin.qq.com/sns/userinfo?access_token="+at+"&openid="+openId;
jsonObject = this.httpGet(url);//以json对象格式返回用户数据
System.out.println("==============>"+jsonObject);
model.addAttribute("weixin", jsonObject);
//把用户微信信息保存到数据库(判断这个信息是否存在,如果不存在,新增到数据库表(自动创建一个用户),如果已存在,直接登录成功)
return "index";
}
/**
* ����get���� http://www.cnblogs.com/QQParadise/articles/5020215.html
*
* @param url
* @return
*/
public JSONObject httpGet(String url) {
JSONObject jsonResult = null;
try {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String strResult = EntityUtils.toString(response.getEntity(),"UTF-8"); //此处设定编码格式
jsonResult = JSON.parseObject(strResult);
System.out.println("strResult=" + strResult);
} else {
System.out.println("*******************");
}
} catch (IOException e) {
e.printStackTrace();
}
return jsonResult;
}
}
此致,调用login方法即可
最后
以上就是刻苦太阳为你收集整理的java实现微信登录(SSM)的全部内容,希望文章能够帮你解决java实现微信登录(SSM)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复