https://github.com/xuxiaowei-com-cn/spring-boot-starter-wechat-miniprogram
支持 OAuth 2.1 JWT 授权的微信小程序开发组件
https://github.com/xuxiaowei-com-cn/spring-boot-starter-wechat-miniprogram
Last synced: 14 days ago
JSON representation
支持 OAuth 2.1 JWT 授权的微信小程序开发组件
- Host: GitHub
- URL: https://github.com/xuxiaowei-com-cn/spring-boot-starter-wechat-miniprogram
- Owner: xuxiaowei-com-cn
- License: apache-2.0
- Created: 2022-06-29T08:44:29.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-08-28T01:59:40.000Z (8 months ago)
- Last Synced: 2024-08-28T02:45:45.678Z (8 months ago)
- Language: Java
- Homepage:
- Size: 437 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
为简化开发工作、提高生产率、解决常见问题而生## 分支
- main
- 支持 JDK 8、11
- next
- 支持 JDK 17## [使用文档](https://www.yuque.com/xuxiaowei-com-cn/xuxiaowei-cloud/third-party-login)
## 其他 [Spring Boot Starter 项目](https://gitee.com/xuxiaowei-com-cn/spring-boot-starter)
## 发布
- [快照版](https://s01.oss.sonatype.org/content/repositories/snapshots/cn/com/xuxiaowei/boot/spring-boot-starter-wechat-miniprogram)
- [发布版](https://s01.oss.sonatype.org/content/repositories/releases/cn/com/xuxiaowei/boot/spring-boot-starter-wechat-miniprogram)## [更新日志](CHANGELOG.md)
## [代码格式规范 spring-javaformat](https://github.com/spring-io/spring-javaformat)
- IntelliJ IDEA
-
项目添加 [io.spring.javaformat:spring-javaformat-maven-plugin](https://repo1.maven.org/maven2/io/spring/javaformat/spring-javaformat-maven-plugin)
依赖之后,可安装自动格式化插件(无需运行下列命令):[spring-javaformat-intellij-idea-plugin](https://repo1.maven.org/maven2/io/spring/javaformat/spring-javaformat-intellij-idea-plugin)```shell
./mvnw spring-javaformat:apply
```
或
```shell
mvn spring-javaformat:apply
```## 使用方法
1. 依赖引入
```xml
org.springframework.security
spring-security-oauth2-authorization-server
0.4.2
cn.com.xuxiaowei.boot
spring-boot-starter-wechat-miniprogram
0.0.2-alpha.1
```
2. 项目配置
```java
package cloud.xuxiaowei.passport.configuration;// import cloud.xuxiaowei.passport.handler.AccessTokenAuthenticationFailureHandlerImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2WeChatMiniProgramAuthenticationProvider;
import org.springframework.security.oauth2.server.authorization.config.annotation.web.configurers.OAuth2AuthorizationServerConfigurer;
import org.springframework.security.oauth2.server.authorization.web.authentication.*;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.RequestMatcher;import java.util.Arrays;
/**
* Spring Security 配置
*
* 详细使用说明参见:
*
* Gitee
*
* Github
*
* @author xuxiaowei
* @since 0.0.1
*/
@Configuration
public class WebSecurityConfigurerAdapterConfiguration {@Bean
@Order(-1)
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {// 此段代码来自:OAuth2AuthorizationServerConfiguration#applyDefaultSecurity(HttpSecurity)
// @formatter:off
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
RequestMatcher endpointsMatcher = authorizationServerConfigurer
.getEndpointsMatcher();http
.requestMatcher(endpointsMatcher)
.authorizeRequests(authorizeRequests ->
authorizeRequests.anyRequest().authenticated()
)
.csrf(csrf -> csrf.ignoringRequestMatchers(endpointsMatcher))
.apply(authorizationServerConfigurer);
// @formatter:on// 自定义客户授权
authorizationServerConfigurer.tokenEndpoint(tokenEndpointCustomizer -> tokenEndpointCustomizer
.accessTokenRequestConverter(new DelegatingAuthenticationConverter(Arrays.asList(
// 新增:微信 OAuth2 用于验证授权授予的 {@link
// OAuth2WeChatMiniProgramAuthenticationToken}
new OAuth2WeChatMiniProgramAuthenticationConverter(),
// 默认值:OAuth2 授权码认证转换器
new OAuth2AuthorizationCodeAuthenticationConverter(),
// 默认值:OAuth2 刷新令牌认证转换器
new OAuth2RefreshTokenAuthenticationConverter(),
// 默认值:OAuth2 客户端凭据身份验证转换器
new OAuth2ClientCredentialsAuthenticationConverter())))
// 用于处理失败的身份验证尝试的策略。
// .errorResponseHandler(new AccessTokenAuthenticationFailureHandlerImpl())
);// 微信小程序 OAuth2 身份验证提供程序
new OAuth2WeChatMiniProgramAuthenticationProvider(http);return http.build();
}}
``````yaml
# 微信小程序配置
wechat:
mini:
program:
# 默认微信小程序的权限
default-role: wechat_miniprogram
# 小程序账户列表
list:
- appid: ${wx_miniapp_appid:}
secret: ${wx_miniapp_secret:}
```3. 微信小程序可使用下列URL获取授权Token
- grant_type
- 必须使用 `wechat_miniprogram`
- client_id
- OAuth 2 客户ID
- client_secret
- OAuth 2 客户秘钥
- appid(可缺省)
- 小程序appid
- 从 0.0.1-alpha.2 开始,参数缺省 `appid` 时,可以从请求头中的 `Referer` 中自动截取
- code
- 微信登录授权码```
/oauth2/token?grant_type=wechat_miniprogram&client_id={CLIENT_ID}&client_secret={CLIENT_SECRET}&appid={APPID}&code={CODE}
```## 批量添加远端仓库地址
点击展开
git remote add gitee https://gitee.com/xuxiaowei-com-cn/spring-boot-starter-wechat-miniprogram.gitgit remote add gitlab https://gitlab.com/xuxiaowei-com-cn/spring-boot-starter-wechat-miniprogram.git
git remote add framagit https://framagit.org/xuxiaowei-com-cn/spring-boot-starter-wechat-miniprogram.git
git remote add github https://github.com/xuxiaowei-com-cn/spring-boot-starter-wechat-miniprogram.git
git remote add gitcode https://gitcode.net/xuxiaowei-com-cn/spring-boot-starter-wechat-miniprogram.git
git remote add gitlink https://gitlink.org.cn/xuxiaowei-com-cn/spring-boot-starter-wechat-miniprogram.git
## 参考文档
- [登录 - code2Session](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html)
## 鸣谢
1. 感谢 [](https://www.jetbrains.com/)
提供开发工具 [](https://www.jetbrains.com/idea) 的免费授权## Stargazers over time
[](https://starchart.cc/xuxiaowei-com-cn/spring-boot-starter-wechat-miniprogram)