原问题: AppletShiroRealm
中的 getAppletUser
方法返回null,导致无法正确获取用户信息。
修复方案:
getAppletUser
方法,支持从数据库和Redis缓存获取用户信息原问题: Token验证缺少黑名单检查和刷新机制。
修复方案:
原问题: 小程序登录没有正确集成到Shiro认证体系。
修复方案:
AppletShiroConfig
配置类AppletShiroRealm
的认证和授权逻辑原问题: 控制器缺少参数验证和错误处理。
修复方案:
创建了 AppletUserUtil
工具类,提供以下功能:
getCurrentAppletUser()
: 获取当前登录的小程序用户getCurrentAppletUserId()
: 获取当前用户IDgetCurrentAppletUserOpenid()
: 获取当前用户openidisAppletUserLoggedIn()
: 检查用户是否已登录hasPermission()
: 检查用户权限hasRole()
: 检查用户角色applet_user
applet:user:*
@GetMapping("/getCurrentUser")
public Result<AppletUser> getCurrentUser() {
AppletUser currentUser = AppletUserUtil.getCurrentAppletUser();
if (currentUser == null) {
return Result.error("用户未登录");
}
return Result.OK("获取成功", currentUser);
}
@PostMapping("/sensitiveOperation")
public Result<String> sensitiveOperation() {
if (!AppletUserUtil.hasPermission("applet:user:edit")) {
return Result.error("权限不足");
}
// 执行敏感操作
return Result.OK("操作成功");
}
@GetMapping("/adminOnly")
public Result<String> adminOnly() {
if (!AppletUserUtil.hasRole("applet_admin")) {
return Result.error("需要管理员权限");
}
return Result.OK("管理员操作成功");
}
确保 applet_user
表已创建,包含以下字段:
id
: 主键openid
: 微信openid(唯一索引)phone
: 手机号(唯一索引)name
: 用户昵称avatar
: 头像bmi
: 体总指数fat
: 脂肪create_time
: 创建时间update_time
: 更新时间确保Redis服务正常运行,用于缓存:
在 application.yml
中配置:
wechat:
mpAppId: your_applet_appid
mpAppSecret: your_applet_secret
确保JWT工具类能正确读取配置:
jeecg:
jwt:
secret: your_jwt_secret_key
expire: 604800