SpringBoot + thymeleaf实现微信登录

首先新建一个springboot项目,规划一下业务流程


首先用户点击使用微信登录——弹出二维码给用户——用户拿出手机进行扫描——用户点击确定——进入后台验证——成功后回调用户信息给页面。流程走完,(相关代码已经放在gitee了),这里介绍的不详细——主要是作者懒,不想把每个类都拿出来体谅一下嘿嘿嘿。gitee仓库如下——


https://gitee.com/chen-houde/csktwxlogin.git


导入依赖,——内网穿透(不设置内网穿透,实现不了微信回调返回用户信息)


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>


        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.6</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.40</version>
        </dependency>

 内网穿透需要去修改相关的网关设置,用管理员才可以编辑,



 


 内网穿透后既可以开始写代码了


添加thymeleaf配置,新建两个页面一个用于弹出微信登录按钮,一个是登录成功显示用户信息,


项目结构如下。


 


index页面


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>

<a href="/wxLogin">
微信登录
</a>

</h1>
</body>
</html>

 登录成功main页面


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>主页</title>
</head>
<body>
<div style="padding: 20px">
<p>微信唯一标识OpenId:&nbsp;<font th:text="${userInfo.openid}"></font></p>
<p>昵称:&nbsp;<font th:text="${userInfo.nickname}"></font></p>
<p>性别:&nbsp;<font th:text="${userInfo.sexStr}"></font></p>
<p><img th:src="${userInfo.headimgurl}"/></p>
</div>
ooxx
</body>
</html>

 

 相关代码已经放在gitee上面了。感兴趣的可以拉下来自行测试,

https://gitee.com/chen-houde/csktwxlogin.git


下面是测试效果,点击微信登录,跳转页面,弹出二维码,登录成功后现实手游信息




 

 希望能帮到各位小伙伴

猜你喜欢

转载自blog.csdn.net/weixin_69218754/article/details/131509778