java实现微信二维码登录功能

实现流程:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <title>登录</title>

    <style type="text/css">
        #text{
            margin: 0 auto;
            width: 100px;
            text-align: center;
        }
        #logo{
            margin: 30px auto;
            width: 100px;
        }
    </style>

</head>
<body>
<div id="text"><h1>登录</h1></div>
<div id="logo"><a href="weixinLogin"><img src="/weixinLogo.jpg" width="100px"></a></div>
</body>
</html>

请求该页面的controller

@RequestMapping("/loginPage")
    public String loginPage(){
        return "login/loginPage";
    }

效果就这样:

2.点击图标发起请求

@RequestMapping("/weixinLogin")
    public String weixinLogin(){
        return "login/weixinLogin";
    }

该请求直接去到一个jsp页面

3.当你扫描二维码后,自动调用你指定的回调函数

然后把用户信息放到model,返回登录成功的页面

这个controller的方法里面用到httpClient和fastjson,放上maven

<dependency>
     <groupId>org.apache.httpcomponents</groupId>
     <artifactId>httpclient</artifactId>
     <version>4.5.2</version>
</dependency>
<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.2.47</version>
</dependency>

到这里基本上就完成了,回调函数的功能在代码注释上非常详细,不做多说了,登录成功返回的页面,自己根据需求编写,用户信息已经放在了model里,不知道返回用户信息的属性的,打印在控制台一看便知。

经验分享 程序员 微信小程序 职场和发展