Springboot項(xiàng)目中實(shí)現(xiàn)微信小程序登錄案例(最新推薦)
接入微信登錄可以通過微信開放平臺(tái)的授權(quán)登錄功能實(shí)現(xiàn),如何實(shí)現(xiàn)微信小程序使用微信登錄案例
注冊(cè)微信開放平臺(tái)賬號(hào)并創(chuàng)建應(yīng)用:
首先注冊(cè)微信開放平臺(tái)賬號(hào),并在開發(fā)者中心創(chuàng)建一個(gè)應(yīng)用,獲取到AppID和AppSecret。
配置Spring Boot項(xiàng)目:
在application.properties或application.yml中配置微信登錄的相關(guān)信息:
wechat.appId=your_app_id wechat.appSecret=your_app_secret wechat.redirectUrl=http://localhost:8080/login/wechat/callback
創(chuàng)建微信登錄控制器:
@PostMapping("/weixin-mini-app-login")
@Operation(summary = "微信小程序的一鍵登錄")
@PermitAll
public CommonResult<AppAuthLoginRespVO> weixinMiniAppLogin(@RequestBody @Valid AppAuthWeixinMiniAppLoginReqVO reqVO) {
return success(authService.weixinMiniAppLogin(reqVO));
}
@Schema(description = "用戶 APP - 微信小程序手機(jī)登錄 Request VO")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AppAuthWeixinMiniAppLoginReqVO {
@Schema(description = "手機(jī) code,小程序通過 wx.getPhoneNumber 方法獲得", requiredMode = Schema.RequiredMode.REQUIRED, example = "hello")
@NotEmpty(message = "手機(jī) code 不能為空")
private String phoneCode;
@Schema(description = "登錄 code,小程序通過 wx.login 方法獲得", requiredMode = Schema.RequiredMode.REQUIRED, example = "word")
@NotEmpty(message = "登錄 code 不能為空")
private String loginCode;
@Schema(description = "state", requiredMode = Schema.RequiredMode.REQUIRED, example = "9b2ffbc1-7425-4155-9894-9d5c08541d62")
@NotEmpty(message = "state 不能為空")
private String state;
}public interface MemberAuthService {
/**
* 手機(jī) + 密碼登錄
*
* @param reqVO 登錄信息
* @return 登錄結(jié)果
*/
AppAuthLoginRespVO login(@Valid AppAuthLoginReqVO reqVO);
/**
* 基于 token 退出登錄
*
* @param token token
*/
void logout(String token);
/**
* 手機(jī) + 驗(yàn)證碼登陸
*
* @param reqVO 登陸信息
* @return 登錄結(jié)果
*/
AppAuthLoginRespVO smsLogin(@Valid AppAuthSmsLoginReqVO reqVO);
/**
* 社交登錄,使用 code 授權(quán)碼
*
* @param reqVO 登錄信息
* @return 登錄結(jié)果
*/
AppAuthLoginRespVO socialLogin(@Valid AppAuthSocialLoginReqVO reqVO);
/**
* 微信小程序的一鍵登錄
*
* @param reqVO 登錄信息
* @return 登錄結(jié)果
*/
AppAuthLoginRespVO weixinMiniAppLogin(AppAuthWeixinMiniAppLoginReqVO reqVO);
/** @Override
public AppAuthLoginRespVO weixinMiniAppLogin(AppAuthWeixinMiniAppLoginReqVO reqVO) {
// 獲得對(duì)應(yīng)的手機(jī)號(hào)信息
SocialWxPhoneNumberInfoRespDTO phoneNumberInfo = socialClientApi.getWxMaPhoneNumberInfo(
UserTypeEnum.COOPERATIVE.getValue(), reqVO.getPhoneCode());
Assert.notNull(phoneNumberInfo, "獲得手機(jī)信息失敗,結(jié)果為空");
// 獲得獲得注冊(cè)用戶
MemberUserDO user = userService.createUserIfAbsent(phoneNumberInfo.getPurePhoneNumber(),
getClientIP(), TerminalEnum.WECHAT_MINI_PROGRAM.getTerminal());
Assert.notNull(user, "獲取用戶失敗,結(jié)果為空");
// 綁定社交用戶
String openid = socialUserApi.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(),
SocialTypeEnum.WECHAT_MINI_APP.getType(), reqVO.getLoginCode(), reqVO.getState()));
// 創(chuàng)建 Token 令牌,記錄登錄日志
return createTokenAfterLoginSuccess(user, user.getMobile(), LoginLogTypeEnum.LOGIN_SOCIAL, openid);
} private AppAuthLoginRespVO createTokenAfterLoginSuccess(MemberUserDO user, String mobile,
LoginLogTypeEnum logType, String openid) {
// 插入登陸日志
createLoginLog(user.getId(), mobile, logType, LoginResultEnum.SUCCESS);
// 創(chuàng)建 Token 令牌
OAuth2AccessTokenRespDTO accessTokenRespDTO = oauth2TokenApi.createAccessToken(new OAuth2AccessTokenCreateReqDTO()
.setUserId(user.getId()).setUserType(getUserType().getValue())
.setClientId(OAuth2ClientConstants.CLIENT_ID_DEFAULT));
// 構(gòu)建返回結(jié)果
return AuthConvert.INSTANCE.convert(accessTokenRespDTO, openid);
} @Override
public OAuth2AccessTokenRespDTO createAccessToken(OAuth2AccessTokenCreateReqDTO reqDTO) {
OAuth2AccessTokenDO accessTokenDO = oauth2TokenService.createAccessToken(
reqDTO.getUserId(), reqDTO.getUserType(), reqDTO.getClientId(), reqDTO.getTenantId(), reqDTO.getScopes());
return BeanUtils.toBean(accessTokenDO, OAuth2AccessTokenRespDTO.class);
}public interface OAuth2TokenService {
/**
* 創(chuàng)建訪問令牌
* 注意:該流程中,會(huì)包含創(chuàng)建刷新令牌的創(chuàng)建
*
* 參考 DefaultTokenServices 的 createAccessToken 方法
*
* @param userId 用戶編號(hào)
* @param userType 用戶類型
* @param clientId 客戶端編號(hào)
* @param scopes 授權(quán)范圍
* @return 訪問令牌的信息
*/
OAuth2AccessTokenDO createAccessToken(Long userId, Integer userType, String clientId, List<String> scopes);
OAuth2AccessTokenDO createAccessToken(Long userId, Integer userType, String clientId, Long tenantId, List<String> scopes); @Override
@Transactional(rollbackFor = Exception.class)
public OAuth2AccessTokenDO createAccessToken(Long userId, Integer userType, String clientId, Long tenantId, List<String> scopes) {
OAuth2ClientDO clientDO = oauth2ClientService.validOAuthClientFromCache(clientId);
// 創(chuàng)建刷新令牌
OAuth2RefreshTokenDO refreshTokenDO = createOAuth2RefreshToken(userId, userType, tenantId, clientDO, scopes);
// 創(chuàng)建訪問令牌
return createOAuth2AccessToken(refreshTokenDO, clientDO);
} private OAuth2RefreshTokenDO createOAuth2RefreshToken(Long userId, Integer userType, Long tenantId, OAuth2ClientDO clientDO, List<String> scopes) {
OAuth2RefreshTokenDO refreshToken = new OAuth2RefreshTokenDO().setRefreshToken(generateRefreshToken())
.setUserId(userId).setUserType(userType)
.setClientId(clientDO.getClientId()).setScopes(scopes)
.setExpiresTime(LocalDateTime.now().plusSeconds(clientDO.getRefreshTokenValiditySeconds()));
refreshToken.setTenantId(tenantId);
oauth2RefreshTokenMapper.insert(refreshToken);
return refreshToken;
}@KeySequence("system_oauth2_access_token_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 數(shù)據(jù)庫的主鍵自增。如果是 MySQL 等數(shù)據(jù)庫,可不寫。
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
public class OAuth2RefreshTokenDO extends TenantBaseDO {
/**
* 編號(hào),數(shù)據(jù)庫字典
*/
private Long id;
/**
* 刷新令牌
*/
private String refreshToken;
/**
* 用戶編號(hào)
*/
private Long userId;
/**
* 用戶類型
*
* 枚舉 {@link UserTypeEnum}
*/
private Integer userType;
/**
* 客戶端編號(hào)
*
* 關(guān)聯(lián) {@link OAuth2ClientDO#getId()}
*/
private String clientId;
/**
* 授權(quán)范圍
*/
@TableField(typeHandler = JacksonTypeHandler.class)
private List<String> scopes;
/**
* 過期時(shí)間
*/
private LocalDateTime expiresTime;
}public class OAuth2AccessTokenDO extends TenantBaseDO {
/**
* 編號(hào),數(shù)據(jù)庫遞增
*/
@TableId
private Long id;
/**
* 訪問令牌
*/
private String accessToken;
/**
* 刷新令牌
*/
private String refreshToken;
/**
* 用戶編號(hào)
*/
private Long userId;
/**
* 用戶類型
*
* 枚舉 {@link UserTypeEnum}
*/
private Integer userType;
/**
* 用戶信息
*/
@TableField(typeHandler = JacksonTypeHandler.class)
private Map<String, String> userInfo;
/**
* 客戶端編號(hào)
*
* 關(guān)聯(lián) {@link OAuth2ClientDO#getId()}
*/
private String clientId;
/**
* 授權(quán)范圍
*/
@TableField(typeHandler = JacksonTypeHandler.class)
private List<String> scopes;
/**
* 過期時(shí)間
*/
private LocalDateTime expiresTime;
}到此這篇關(guān)于Springboot項(xiàng)目中實(shí)現(xiàn)微信小程序登錄案例的文章就介紹到這了,更多相關(guān)Springboot微信小程序登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合Mybatis-Plus實(shí)現(xiàn)微信注冊(cè)登錄的示例代碼
- 微信小程序使用uni-app和springboot實(shí)現(xiàn)一鍵登錄功能(JWT鑒權(quán))
- springboot實(shí)現(xiàn)微信掃碼登錄的項(xiàng)目實(shí)踐
- springboot+jwt+微信小程序授權(quán)登錄獲取token的方法實(shí)例
- 詳解SpringBoot如何實(shí)現(xiàn)整合微信登錄
- 一篇文章帶你入門Springboot整合微信登錄與微信支付(附源碼)
- springboot 微信授權(quán)網(wǎng)頁登錄操作流程
- SpringBoot實(shí)現(xiàn)微信掃碼登錄的示例代碼
相關(guān)文章
spring boot優(yōu)雅集成redisson詳解
這篇文章主要為大家介紹了spring boot優(yōu)雅集成redisson詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
eclipse項(xiàng)目在IDEA中打開并運(yùn)行的詳細(xì)圖文教程
這篇文章主要給大家介紹了關(guān)于eclipse項(xiàng)目在IDEA中打開并運(yùn)行的詳細(xì)圖文教程,至從使用IDEA開發(fā)工具以來,不少次有使用IDEA運(yùn)行Eclipse項(xiàng)目或非Maven項(xiàng)目,所以這里給大家總結(jié)下,需要的朋友可以參考下2023-09-09
Java實(shí)現(xiàn)使用Websocket發(fā)送消息詳細(xì)代碼舉例
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)使用Websocket發(fā)送消息的相關(guān)資料,WebSocket是一種協(xié)議,用于在Web應(yīng)用程序和服務(wù)器之間建立實(shí)時(shí)、雙向的通信連接,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05
在Ubuntu上部署SpringBoot應(yīng)用的操作步驟
隨著云計(jì)算和容器化技術(shù)的普及,Linux 服務(wù)器已成為部署 Web 應(yīng)用程序的主流平臺(tái)之一,Java 作為一種跨平臺(tái)的編程語言,具有廣泛的應(yīng)用場景,本文將詳細(xì)介紹如何在 Ubuntu 服務(wù)器上部署 Java 應(yīng)用,需要的朋友可以參考下2025-01-01
Java畢業(yè)設(shè)計(jì)之多用戶宿舍管理系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了基于Java實(shí)現(xiàn)的多用戶宿舍管理系統(tǒng),本文采用了jsp、servlet、jdbc等技術(shù),文中示例代碼講解詳細(xì),需要的可以參考一下2022-02-02
SpringBoot--- SpringSecurity進(jìn)行注銷權(quán)限控制的配置方法
這篇文章主要介紹了SpringBoot--- SpringSecurity進(jìn)行注銷,權(quán)限控制,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08

