1.微信登录线上操作
- 注册微信开放者平台
- 进行企业认证
- 创建网页应用
- 查看APPID
2.导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.50</version>
</dependency>
3.创建微信登陆配置文件
wechat.openUrl=https://open.weixin.qq.com/connect/qrconnect
wechat.appid=此处填写APPID
wechat.redirectUri=http://localhost:8080/wechat/callback
wechat.responseType=code
wechat.scope=snsapi_login
wechat.state=STATE#wechat_redirect
wechat.accessTokenUrl=https://api.weixin.qq.com/sns/oauth2/access_token
wechat.secret=此处填写SECRET
wechat.grantType=authorization_code
wechat.userInfoUrl=https://api.weixin.qq.com/sns/userinfo
4.创建微信配置文件对象类
package com.example.wxlogin.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "wechat")
@PropertySource("classpath:wxlogin.properties")
public class WechatConfig {
private String appid;
private String redirectUri;
private String responseType;
private String scope;
private String state;
private String openUrl;
private String accessTokenUrl;
private String secret;
private String grantType;
private String userInfoUrl;
}
5.创建URL工具类
package com.example.wxlogin.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class UrlUtils {
public static String loadURL(String urlStr) {
try {
URL url = new URL(urlStr);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
String responseStr = ConvertToString(inputStream);
return responseStr;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private static String ConvertToString(InputStream inputStream) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder result = new StringBuilder();
String line = null;
try {
while ((line = bufferedReader.readLine()) != null) {
result.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStreamReader.close();
inputStream.close();
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result.toString();
}
}
6.添加微信登录控制器方法
package com.example.wxlogin.controller;
import com.alibaba.fastjson.JSONObject;
import com.example.wxlogin.config.WechatConfig;
import com.example.wxlogin.utils.UrlUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
@RestController
public class TestController {
@Resource
private WechatConfig wechatConfig;
@RequestMapping("/wechat/login")
public void wechat(HttpServletResponse response) throws Exception {
StringBuffer wxsb = new StringBuffer(wechatConfig.getOpenUrl());
wxsb.append("?appid=" + wechatConfig.getAppid());
wxsb.append("&redirect_uri=" + wechatConfig.getRedirectUri());
wxsb.append("&response_type=" + wechatConfig.getResponseType());
wxsb.append("&scope=" + wechatConfig.getScope());
wxsb.append("&state=" + wechatConfig.getState());
response.sendRedirect(wxsb.toString());
}
@RequestMapping("/wechat/callback")
public Object callback(String code) throws Exception {
StringBuffer wxsb = new StringBuffer(wechatConfig.getAccessTokenUrl());
wxsb.append("?appid=" + wechatConfig.getAppid());
wxsb.append("&secret=" + wechatConfig.getSecret());
wxsb.append("&code=" + code);
wxsb.append("&grant_type=" + wechatConfig.getGrantType());
String resp = UrlUtils.loadURL(wxsb.toString());
JSONObject jsonObject = JSONObject.parseObject(resp);
String accessToken = jsonObject.getString("access_token");
String openId = jsonObject.getString("openid");
StringBuffer usb = new StringBuffer(wechatConfig.getUserInfoUrl());
usb.append("?access_token=" + accessToken);
usb.append("&openid=" + openId);
String userInfo = UrlUtils.loadURL(usb.toString());
JSONObject userObject = JSONObject.parseObject(userInfo);
return userObject;
}
}