路条编程 2023-11-15 20:38 发表于河北

springSecurity 中集成微信登录 springboot security 微信登录_登录页面

这一系列课程将包含Spring Boot 许多关键的技术和工具,包括 Mybatis-Plus、Redis、Mongodb、MinIO、Kafka、MySQL、消息队列(MQ)、OAuth2 等相关内容。

使用Spring Boot结合JustAuth实现支付宝、微信、微博扫码登录功能

在使用Spring Boot结合JustAuth实现支付宝、微信、微博扫码登录功能之前,需要先确保已经配置好Spring Boot项目,并且添加了JustAuth的依赖。你可以在项目的pom.xml文件中添加如下依赖:

<dependency>
    <groupId>me.zhyd.oauth</groupId>
    <artifactId>JustAuth</artifactId>
    <version>1.15.10</version> <!-- 请根据最新版本进行替换 -->
</dependency>

接下来,可以参考下面的示例代码实现支付宝、微信、微博扫码登录功能。在这个例子中,我使用了Spring Boot的Web模块,可以根据自己的项目需求进行适当的调整。

import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.request.AuthAlipayRequest;
import me.zhyd.oauth.request.AuthWeChatRequest;
import me.zhyd.oauth.request.AuthWeiboRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/oauth")
public class OAuthController {

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

    @GetMapping("/callback")
    public String loginCallback(HttpServletRequest request, Model model) {
        String source = request.getParameter("source");
        AuthCallback authCallback = AuthCallback.builder()
                .code(request.getParameter("code"))
                .state(request.getParameter("state"))
                .build();

        AuthResponse response;
        switch (source) {
            case "wechat":
                response = getWechatAuthResponse(authCallback);
                break;
            case "alipay":
                response = getAlipayAuthResponse(authCallback);
                break;
            case "weibo":
                response = getWeiboAuthResponse(authCallback);
                break;
            default:
                return "redirect:/oauth/login";
        }

        model.addAttribute("response", response);
        return "callback";
    }

    private AuthResponse getWechatAuthResponse(AuthCallback callback) {
        return new AuthWeChatRequest().login(callback);
    }

    private AuthResponse getAlipayAuthResponse(AuthCallback callback) {
        return new AuthAlipayRequest().login(callback);
    }

    private AuthResponse getWeiboAuthResponse(AuthCallback callback) {
        return new AuthWeiboRequest().login(callback);
    }
}

在上述代码中,我们创建了一个OAuthController类,该类包含了登录和回调的处理方法。在login方法中,我们返回登录页面的视图,而在loginCallback方法中,根据不同的授权源(wechat、alipay、weibo),调用不同的JustAuth请求进行登录,并将结果传递给回调页面。

springSecurity 中集成微信登录 springboot security 微信登录_微信_02

接着,创建两个页面,一个是登录页面(login.html),另一个是回调页面(callback.html):

src/main/resources/templates/login.html:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
</head>
<body>
    <h2>登录页面</h2>
    <a href="/oauth/login?source=wechat">使用微信登录</a><br>
    <a href="/oauth/login?source=alipay">使用支付宝登录</a><br>
    <a href="/oauth/login?source=weibo">使用微博登录</a>
</body>
</html>

src/main/resources/templates/callback.html:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>回调页面</title>
</head>
<body>
    <h2>回调页面</h2>
    <p>响应结果: ${response}</p>
</body>
</html>

在这个简单的示例中,我们可以通过访问/oauth/login来进入登录页面,选择不同的授权源进行登录。登录成功后将跳转到/oauth/callback页面,展示登录结果。

示例中完整代码,可以从下面网址获取:

https://gitee.com/jlearning/wechatdemo.git

https://github.com/icoderoad/wxdemo.git

今天就讲到这里