我爱java系列---【springboot整合微信扫码登陆】
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>wxlogin-web</artifactId>
<description>二维码登录功能demo</description>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- 日志 -->
<slf4j.version>1.7.25</slf4j.version>
<log4j2.version>2.7</log4j2.version>
<!-- 异步日志 -->
<disruptor.version>3.2.0</disruptor.version>
<!--fastjson工具类 -->
<fastjson.version>1.2.45</fastjson.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- Log4j2 异步支持 -->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>${disruptor.version}</version>
</dependency>
<!-- Apache Commons Logging 1.x接口的日志输出转向log4j2输出 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
</dependency>
<!-- 集成测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
2.前端代码login.html
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8"/>
<title>登录</title>
</head>
<body>
<!--登录框-->
<div >
<form class="login-form">
<!--二维码登录-->
<div id="weixin"></div>
</form>
</div>
<!--微信官方提供的生成二维码的js-->
<script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>
</body>
<script>
var obj = new WxLogin({
self_redirect:true,
id:"weixin",
appid: "wx7287a60bb700fd21", #微信开放平台中申请的appid
scope: "snsapi_login", #固定参数,可以参照微信开放平台中的文档
redirect_uri: "http://www.txjava.cn/login" #注意:后台在host文件中添加:“127.0.0.1 www.txjava.cn”,后台springboot项目的端口号为80
});
</script>
</html>
3.LoginController.java
@Controller
@RequestMapping("/login")
@Slf4j
public class LoginController {
@Autowired
private RestTemplate restTemplate;
@GetMapping
public void login(HttpServletRequest request, HttpServletResponse response) {
//获取到code值
String code = request.getParameter("code");
//判断
if (code == null) {
throw new RuntimeException("用户禁止授权");
}
//获取到了code值,回调没有问题
//定义地址
String token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx7287a60bb700fd21&secret=1ef8755f92bebae8ad7bab432ba29cbf&code="
+ code + "&grant_type=authorization_code";
//发送请求
String json = restTemplate.getForObject(token_url, String.class);
WxToken token = parseObject(json, WxToken.class);
//获取到接口调用凭证
//获取个人信息
String user_url = "https://api.weixin.qq.com/sns/userinfo?access_token="+token.getAccess_token()+"&openid="+token.getOpenid();
String jsonStr1 = restTemplate.getForObject(user_url, String.class);
JSONObject jsonStr = (JSONObject) JSON.parse(jsonStr1);
//用户名
String nickname = (String) jsonStr.get("nickname");
//用户头像地址
String headimgurl = (String) jsonStr.get("headimgurl");
request.setAttribute("nickname",nickname);
request.setAttribute("headimgurl",headimgurl);
log.info("微信返回的用户对象:jsonStr1:{}",jsonStr);
try {
request.getRequestDispatcher("/main.html").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("微信扫描登陆异常");
}
}
}
4.WxToken.java
public class WxToken implements Serializable {
private String access_token;
private String expires_in;
private String refresh_token;
private String openid;
private String scope;
private String unionid;
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public String getExpires_in() {
return expires_in;
}
public void setExpires_in(String expires_in) {
this.expires_in = expires_in;
}
public String getRefresh_token() {
return refresh_token;
}
public void setRefresh_token(String refresh_token) {
this.refresh_token = refresh_token;
}
public String getOpenid() {
return openid;
}
public void setOpenid(String openid) {
this.openid = openid;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public String getUnionid() {
return unionid;
}
public void setUnionid(String unionid) {
this.unionid = unionid;
}
}
5.application.properties
spring.application.name=wxlogin
server.port=80