首页 技术 正文
技术 2022年11月20日
0 收藏 999 点赞 2,853 浏览 3336 个字

关于snsapi_base网页授权的说明

以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(往往是业务页面)微信打开链接(https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx48414ee14f7d7158
&redirect_uri=http://test.cn/testWx//servlet/Oauth2Servlet&response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect)
即可看到效果,效果截图如下:
只打印了 openid。

微信公众平台–网页授权获取用户基本信息(snsapi_base方式)

具体代码如下:(代码参考博客:http://www.cnblogs.com/zyw-205520/p/3581088.html) 
Oauth2Servlet.java
package com.payroll.wx.servlet;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONObject;import com.payroll.wx.util.CommendDef;
import com.payroll.wx.util.HttpsGetUtil;public class CopyOfOauth2Servlet extends HttpServlet { /**
*
*/
private static final long serialVersionUID = 1L; /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//静默授权
String get_access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
+ "appid="
+ CommendDef.AppId
+ "&secret="
+ CommendDef.AppSecret
+ "&code=CODE&grant_type=authorization_code"; // 将请求、响应的编码均设置为UTF-8(防止中文乱码)
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String code = request.getParameter("code"); System.out.println("******************code=" + code); get_access_token_url = get_access_token_url.replace("CODE", code); String json = HttpsGetUtil.doHttpsGetJson(get_access_token_url); JSONObject jsonObject = JSONObject.fromObject(json);
String openid = jsonObject.getString("openid"); response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method \n");
out.println("openid:" + openid + "\n\n");
out.println(">");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}}

HttpsGetUtil.java
package com.payroll.wx.util;import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;public class HttpsGetUtil
{ public static String doHttpsGetJson(String Url)
{
String message = "";
try
{
System.out.println("doHttpsGetJson");//TODO:dd
URL urlGet = new URL(Url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
http.setRequestMethod("GET"); //必须是get方式请求 24
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒28
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒29 30
http.connect();
InputStream is =http.getInputStream();
int size =is.available();
byte[] jsonBytes =new byte[size];
is.read(jsonBytes);
message=new String(jsonBytes,"UTF-8");
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return message;
}
}

 

    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898