SpringBoot使用WxJava SDK集成网页授权(OAuth2)

描述

微信官方文档 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842

WxJava SDK github地址 https://github.com/Wechat-Group/WxJava

授权步骤

1. 用户同意授权,获得code

2. 通过code换取网页授权access_token

3. 刷新access_token(如果需要,过期需刷新)

4. 拉取用户信息

详细可参考文档,这里采用SDK方式集成

编译环境:IDEA

前提准备

若无公众号,可使用微信测试号,较低代价实现网页授权

申请地址: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421137522

扫码后可获得appID和appsecret

后扫码关注公众号获得用户权限

最重要的是修改登录的网页账号

由于是测试号,支持ip:端口的形式,若是正式环境必须使用域名,没有的话可以使用内网穿透,将公网域名映射到localhost:8080

我使用的是natapp https://natapp.cn/

流程

1. Maven依赖

//仅包含网页授权的依赖
<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-mp</artifactId>
    <version>2.7.0</version>
</dependency>
//不用写get和set方法的辅助依赖,可以根据需要选择不添加,那就要手动添加get()和set()方法
//若仅添加依赖还是报错,需要在File-settings-Plugins中搜索添加,也可百度详细安装方式
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

2. 创建appid和appsecret的配置文件 WechatAccountConfig

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Created by Sakura
 */
@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {

    /**
     * 公众平台id
     */
    private String mpAppId;

    /**
     * 公众平台密钥
     */
    private String mpAppSecret;

    /**
     * 公众平台回调地址
     */
    private String wechatMpAuthorize;
}

resource的application中配置,地址要填测试号中网页账号中地址,否则会报错

为sdk的WxMpService创建配置文件 WechatMpConfig

import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * Created by Sakura
 */
@Component
public class WechatMpConfig {

    @Autowired
    private WechatAccountConfig accountConfig;

    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(accountConfig.getMpAppId());
        wxMpConfigStorage.setSecret(accountConfig.getMpAppSecret());
        return wxMpConfigStorage;
    }
}

3. 创建登陆授权controller WechatController

package com.imooc.cotroller;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import java.net.URLEncoder;

/**
 * Created by Sakura
 */
@Controller
@RequestMapping("/wechat")
public class WechatController {

    @Autowired
    private WxMpService wxMpService;

    @Autowired
    private WechatAccountConfig accountConfig;

    @GetMapping("/authorize")
    public String authorize(@RequestParam("returnUrl") String returnUrl) throws Exception {
//        //1. 配置
//        //2. 调用方法
        String url = accountConfig.wechatMpAuthorize + "/wechat/userInfo"; //回调地址
        String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_BASE, URLEncoder.encode(returnUrl,"gbk"));
        return "redirect:" + redirectUrl;
    }

    @GetMapping("/userInfo")
    public String userInfo(@RequestParam("code") String code,
                         @RequestParam("state") String returnUrl) {
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();
        try {
            wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }

        String openId = wxMpOAuth2AccessToken.getOpenId();

        return "redirect:" + returnUrl + "?openid=" + openId;
    }
}

具体流程是,前端携带着获得登录信息后跳转的地址returnUrl访问localhost:8080/wechat/authorize接口,调用oauth2buildAuthorizationUrl方法,获得code并把returnUrl作为附加信息state(这里state是附加字段,可以存自定义参数),最终生成的字符串格式为

http://自己的wechatMpAuthorize回调地址?code=....&state=...

后通过redirect访问localhost:8080/wechat/userInfo接口,拿到code和returnUrl(state中的值)后,调用oauth2getAccessToken方法获得信息列表,在列表中拿到openId,这就是我们的微信号,相当于网页授权成功,然后redirect到returnUrl附带openId参数。

其中在调用oauth2buildAuthorizationUrl方法时,第二个参数若设置为 

WxConsts.OAUTH2_SCOPE_BASE

则不弹出授权页面,直接跳转,只能获取用户openid

若设置为

WxConsts.OAUTH2_SCOPE_USER_INFO

则会弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Spring Boot集成OAuth2可以实现用户身份验证和授权功能。OAuth2是一种用于授权的开放标准,允许用户授权第三方应用访问他们在其他服务提供商上的资源,而无需将用户名和密码提供给第三方应用。 要在Spring Boot应用程序中实现OAuth2,可以使用Spring Security和Spring Security OAuth2模块。下面是一个简单的步骤指南: 1. 添加依赖:在Maven或Gradle中添加Spring Security和Spring Security OAuth2的相关依赖。 2. 配置认证服务器:创建一个认证服务器配置类,继承自`AuthorizationServerConfigurerAdapter`,并重写`configure`方法来配置OAuth2的相关信息,如授权类型、客户端信息、访问令牌的有效期等。 3. 配置资源服务器:创建一个资源服务器配置类,继承自`ResourceServerConfigurerAdapter`,并重写`configure`方法来配置受保护的资源路径和访问规则。 4. 创建用户认证服务:创建一个实现了`UserDetailsService`接口的自定义用户认证服务类,用于从数据库或其他数据源中获取用户信息。 5. 配置Spring Security:创建一个继承自`WebSecurityConfigurerAdapter`的配置类,通过重写`configure`方法来配置Spring Security的行为,例如允许所有用户访问某些路径或强制用户进行身份验证。 6. 配置安全规则:在应用程序的配置文件(如application.properties)中,配置安全规则,如禁用CSRF保护、允许访问的路径等。 7. 创建登录页面:根据需要创建一个登录页面,用于用户进行身份验证。 以上是一个基本的步骤指南,具体实现可能会根据你的应用程序需求而有所不同。你可以参考Spring官方文档和示例代码来更详细地了解和实现Spring Boot集成OAuth2的过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值