feat: 重构微信小程序认证及优化代码生成模板

- 将微信小程序认证相关类重命名(WechatMini -> WxMa)
- 新增 WxMaAuthenticationToken 和 WxMaAuthenticationProvider
- 调整代码生成前端模板样式与导入路径
This commit is contained in:
Ray.Hao
2026-03-30 07:48:38 +08:00
parent 9cd3ff88f8
commit 10eb81ccd1
9 changed files with 58 additions and 52 deletions

View File

@@ -9,7 +9,7 @@ import com.youlai.boot.framework.security.filter.TokenAuthenticationFilter;
import com.youlai.boot.framework.security.handler.MyAccessDeniedHandler; import com.youlai.boot.framework.security.handler.MyAccessDeniedHandler;
import com.youlai.boot.framework.security.handler.MyAuthenticationEntryPoint; import com.youlai.boot.framework.security.handler.MyAuthenticationEntryPoint;
import com.youlai.boot.framework.security.provider.SmsAuthenticationProvider; import com.youlai.boot.framework.security.provider.SmsAuthenticationProvider;
import com.youlai.boot.framework.security.provider.WechatMiniAuthenticationProvider; import com.youlai.boot.framework.security.provider.WxMaAuthenticationProvider;
import com.youlai.boot.framework.security.token.TokenManager; import com.youlai.boot.framework.security.token.TokenManager;
import com.youlai.boot.framework.security.service.SysUserDetailsService; import com.youlai.boot.framework.security.service.SysUserDetailsService;
import com.youlai.boot.system.service.ConfigService; import com.youlai.boot.system.service.ConfigService;
@@ -132,11 +132,11 @@ public class SecurityConfig {
* 微信小程序认证 Provider * 微信小程序认证 Provider
*/ */
@Bean @Bean
public WechatMiniAuthenticationProvider wechatMiniAuthenticationProvider( public WxMaAuthenticationProvider wechatMiniAuthenticationProvider(
WxMaService wxMaService, WxMaService wxMaService,
SysUserDetailsService sysUserDetailsService SysUserDetailsService sysUserDetailsService
) { ) {
return new WechatMiniAuthenticationProvider(wxMaService, sysUserDetailsService); return new WxMaAuthenticationProvider(wxMaService, sysUserDetailsService);
} }
/** /**
@@ -146,12 +146,12 @@ public class SecurityConfig {
public AuthenticationManager authenticationManager( public AuthenticationManager authenticationManager(
DaoAuthenticationProvider daoAuthenticationProvider, DaoAuthenticationProvider daoAuthenticationProvider,
SmsAuthenticationProvider smsAuthenticationProvider, SmsAuthenticationProvider smsAuthenticationProvider,
WechatMiniAuthenticationProvider wechatMiniAuthenticationProvider WxMaAuthenticationProvider wxMaAuthenticationProvider
) { ) {
return new ProviderManager( return new ProviderManager(
daoAuthenticationProvider, daoAuthenticationProvider,
smsAuthenticationProvider, smsAuthenticationProvider,
wechatMiniAuthenticationProvider wxMaAuthenticationProvider
); );
} }

View File

@@ -10,7 +10,7 @@ import java.util.Collection;
/** /**
* 微信小程序认证 Token * 微信小程序认证 Token
*/ */
public class WechatMiniAuthenticationToken extends AbstractAuthenticationToken { public class WxMaAuthenticationToken extends AbstractAuthenticationToken {
@Serial @Serial
private static final long serialVersionUID = 622L; private static final long serialVersionUID = 622L;
@@ -34,7 +34,7 @@ public class WechatMiniAuthenticationToken extends AbstractAuthenticationToken {
* *
* @param code 微信小程序code * @param code 微信小程序code
*/ */
public WechatMiniAuthenticationToken(String code) { public WxMaAuthenticationToken(String code) {
super(AuthorityUtils.NO_AUTHORITIES); super(AuthorityUtils.NO_AUTHORITIES);
this.principal = code; this.principal = code;
this.credentials = null; this.credentials = null;
@@ -47,7 +47,7 @@ public class WechatMiniAuthenticationToken extends AbstractAuthenticationToken {
* @param principal 用户详情SysUserDetails * @param principal 用户详情SysUserDetails
* @param authorities 授权信息 * @param authorities 授权信息
*/ */
public WechatMiniAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities) { public WxMaAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities) {
super(authorities); super(authorities);
this.principal = principal; this.principal = principal;
this.credentials = null; this.credentials = null;
@@ -57,8 +57,8 @@ public class WechatMiniAuthenticationToken extends AbstractAuthenticationToken {
/** /**
* 创建已认证的 Token静态工厂方法 * 创建已认证的 Token静态工厂方法
*/ */
public static WechatMiniAuthenticationToken authenticated(Object principal, Collection<? extends GrantedAuthority> authorities) { public static WxMaAuthenticationToken authenticated(Object principal, Collection<? extends GrantedAuthority> authorities) {
return new WechatMiniAuthenticationToken(principal, authorities); return new WxMaAuthenticationToken(principal, authorities);
} }
@Override @Override

View File

@@ -6,7 +6,7 @@ import cn.hutool.core.util.ObjectUtil;
import com.youlai.boot.framework.security.exception.NeedBindMobileException; import com.youlai.boot.framework.security.exception.NeedBindMobileException;
import com.youlai.boot.framework.security.model.SysUserDetails; import com.youlai.boot.framework.security.model.SysUserDetails;
import com.youlai.boot.framework.security.model.UserAuthInfo; import com.youlai.boot.framework.security.model.UserAuthInfo;
import com.youlai.boot.framework.security.model.WechatMiniAuthenticationToken; import com.youlai.boot.framework.security.model.WxMaAuthenticationToken;
import com.youlai.boot.framework.security.service.SysUserDetailsService; import com.youlai.boot.framework.security.service.SysUserDetailsService;
import com.youlai.boot.system.model.entity.UserSocial; import com.youlai.boot.system.model.entity.UserSocial;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@@ -23,7 +23,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
*/ */
@Slf4j @Slf4j
@RequiredArgsConstructor @RequiredArgsConstructor
public class WechatMiniAuthenticationProvider implements AuthenticationProvider { public class WxMaAuthenticationProvider implements AuthenticationProvider {
private final WxMaService wxMaService; private final WxMaService wxMaService;
private final SysUserDetailsService sysUserDetailsService; private final SysUserDetailsService sysUserDetailsService;
@@ -76,7 +76,7 @@ public class WechatMiniAuthenticationProvider implements AuthenticationProvider
log.info("微信小程序登录成功username={}, openid={}", userAuthInfo.getUsername(), openid); log.info("微信小程序登录成功username={}, openid={}", userAuthInfo.getUsername(), openid);
return WechatMiniAuthenticationToken.authenticated(userDetails, userDetails.getAuthorities()); return WxMaAuthenticationToken.authenticated(userDetails, userDetails.getAuthorities());
} catch (WxErrorException e) { } catch (WxErrorException e) {
log.error("微信小程序登录失败调用微信接口异常code={}", code, e); log.error("微信小程序登录失败调用微信接口异常code={}", code, e);
@@ -86,7 +86,7 @@ public class WechatMiniAuthenticationProvider implements AuthenticationProvider
@Override @Override
public boolean supports(Class<?> authentication) { public boolean supports(Class<?> authentication) {
return WechatMiniAuthenticationToken.class.isAssignableFrom(authentication); return WxMaAuthenticationToken.class.isAssignableFrom(authentication);
} }
} }

View File

@@ -1,5 +1,5 @@
<template> <template>
<div class="app-container h-full flex flex-1 flex-col"> <div class="page-container h-full flex flex-1 flex-col">
<!-- 搜索 --> <!-- 搜索 -->
<PageSearch <PageSearch
ref="searchRef" ref="searchRef"

View File

@@ -1,6 +1,6 @@
<template> <template>
<div class="app-container"> <div class="page-container">
<div class="filter-section"> <el-card class="page-search" shadow="never">
<el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="auto"> <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="auto">
#foreach($fieldConfig in $fieldConfigs) #foreach($fieldConfig in $fieldConfigs)
#if($fieldConfig.isShowInQuery == 1) #if($fieldConfig.isShowInQuery == 1)
@@ -88,16 +88,16 @@
</el-form-item> </el-form-item>
#end #end
#end #end
<el-form-item class="search-buttons"> <el-form-item>
<el-button type="primary" icon="search" @click="handleQuery">搜索</el-button> <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
<el-button icon="refresh" @click="handleResetQuery">重置</el-button> <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
</el-form-item> </el-form-item>
</el-form> </el-form>
</div> </el-card>
<el-card shadow="hover" class="table-section"> <el-card class="page-content" shadow="never">
<div class="table-section__toolbar"> <div class="page-toolbar">
<div class="table-section__toolbar--actions"> <div class="page-toolbar__left">
<el-button <el-button
v-hasPerm="['${moduleName}:${entityKebab}:create']" v-hasPerm="['${moduleName}:${entityKebab}:create']"
type="success" type="success"
@@ -121,7 +121,6 @@
border border
stripe stripe
highlight-current-row highlight-current-row
class="table-section__content"
row-key="id" row-key="id"
@selection-change="handleSelectionChange" @selection-change="handleSelectionChange"
> >
@@ -171,13 +170,15 @@
</el-table-column> </el-table-column>
</el-table> </el-table>
<pagination <div class="page-pagination">
v-if="total > 0" <pagination
v-model:total="total" v-if="total > 0"
v-model:page="queryParams.pageNum" v-model:total="total"
v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum"
@pagination="fetchList" v-model:limit="queryParams.pageSize"
/> @pagination="fetchList"
/>
</div>
</el-card> </el-card>
<!-- $!{businessName}表单弹窗 --> <!-- $!{businessName}表单弹窗 -->

View File

@@ -2,7 +2,7 @@
* ${entityName} $!{businessName}类型定义 * ${entityName} $!{businessName}类型定义
*/ */
import type { BaseQueryParams } from "./common"; import type { BaseQueryParams } from "@/api/common";
/** $!{businessName}查询参数 */ /** $!{businessName}查询参数 */
export interface ${entityName}QueryParams extends BaseQueryParams { export interface ${entityName}QueryParams extends BaseQueryParams {

View File

@@ -1,5 +1,6 @@
import request from "@/utils/request"; import request from "@/utils/request";
import type { ${entityName}Form, ${entityName}QueryParams, ${entityName}Item } from "@/types/api"; import type { ${entityName}Form, ${entityName}QueryParams, ${entityName}Item } from "./types";
import type { PageResult } from "@/api/common";
const ${entityUpperSnake}_BASE_URL = "/api/v1/${entityKebab}"; const ${entityUpperSnake}_BASE_URL = "/api/v1/${entityKebab}";
@@ -66,3 +67,6 @@ const ${entityName}API = {
} }
export default ${entityName}API; export default ${entityName}API;
// 重导出类型
export * from "./types";

View File

@@ -1,5 +1,5 @@
<template> <template>
<div class="app-container h-full flex flex-1 flex-col"> <div class="page-container h-full flex flex-1 flex-col">
<!-- 搜索 --> <!-- 搜索 -->
<PageSearch <PageSearch
ref="searchRef" ref="searchRef"
@@ -64,7 +64,7 @@
defineOptions({ name: "$entityName" }); defineOptions({ name: "$entityName" });
import ${entityName}API from "@/api/${moduleName}/${entityKebab}"; import ${entityName}API from "@/api/${moduleName}/${entityKebab}";
import type { ${entityName}Form, ${entityName}QueryParams } from "@/types/api"; import type { ${entityName}Form, ${entityName}QueryParams } from "@/api/${moduleName}/${entityKebab}";
import type { IObject, IModalConfig, IContentConfig, ISearchConfig } from "@/components/CURD/types"; import type { IObject, IModalConfig, IContentConfig, ISearchConfig } from "@/components/CURD/types";
import usePage from "@/components/CURD/usePage"; import usePage from "@/components/CURD/usePage";
@@ -151,8 +151,8 @@ const contentConfig: IContentConfig<${entityName}QueryParams> = reactive({
// 数据解析函数 // 数据解析函数
parseData(res: any) { parseData(res: any) {
return { return {
total: res?.page?.total ?? 0, total: res?.total ?? 0,
list: res?.data ?? [], list: res?.list ?? [],
}; };
}, },
// 分页配置 // 分页配置

View File

@@ -1,6 +1,6 @@
<template> <template>
<div class="app-container"> <div class="page-container">
<div class="filter-section"> <el-card class="page-search" shadow="never">
<el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="auto"> <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="auto">
#foreach($fieldConfig in $fieldConfigs) #foreach($fieldConfig in $fieldConfigs)
#if($fieldConfig.isShowInQuery == 1) #if($fieldConfig.isShowInQuery == 1)
@@ -88,16 +88,16 @@
</el-form-item> </el-form-item>
#end #end
#end #end
<el-form-item class="search-buttons"> <el-form-item>
<el-button type="primary" icon="search" @click="handleQuery">搜索</el-button> <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
<el-button icon="refresh" @click="handleResetQuery">重置</el-button> <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
</el-form-item> </el-form-item>
</el-form> </el-form>
</div> </el-card>
<el-card shadow="hover" class="table-section"> <el-card class="page-content" shadow="never">
<div class="table-section__toolbar"> <div class="page-toolbar">
<div class="table-section__toolbar--actions"> <div class="page-toolbar__left">
<el-button <el-button
v-hasPerm="['${moduleName}:${entityKebab}:create']" v-hasPerm="['${moduleName}:${entityKebab}:create']"
type="success" type="success"
@@ -121,7 +121,6 @@
border border
stripe stripe
highlight-current-row highlight-current-row
class="table-section__content"
row-key="id" row-key="id"
@selection-change="handleSelectionChange" @selection-change="handleSelectionChange"
> >
@@ -171,13 +170,15 @@
</el-table-column> </el-table-column>
</el-table> </el-table>
<pagination <div class="page-pagination">
v-if="total > 0" <pagination
v-model:total="total" v-if="total > 0"
v-model:page="queryParams.pageNum" v-model:total="total"
v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum"
@pagination="fetchList" v-model:limit="queryParams.pageSize"
/> @pagination="fetchList"
/>
</div>
</el-card> </el-card>
<!-- $!{businessName}表单弹窗 --> <!-- $!{businessName}表单弹窗 -->
@@ -277,7 +278,7 @@
import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from "element-plus"; import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from "element-plus";
import { useTableSelection } from "@/composables"; import { useTableSelection } from "@/composables";
import ${entityName}API from "@/api/${moduleName}/${entityKebab}"; import ${entityName}API from "@/api/${moduleName}/${entityKebab}";
import type { ${entityName}Item, ${entityName}Form, ${entityName}QueryParams } from "@/types/api"; import type { ${entityName}Item, ${entityName}Form, ${entityName}QueryParams } from "@/api/${moduleName}/${entityKebab}";
defineOptions({ defineOptions({
name: "${entityName}", name: "${entityName}",