当前位置: 首页 > news >正文

若依系统(Security)增加微信小程序登录(自定义登录)

若依系统(分离版后端)自带的账号验证是基于 

UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);

验证,然后在系统中controller或service类中 SecurityUtils 工具类中直接可获取用户或用户ID等

SecurityUtils.getLoginUser().getUser();

现在想实现,微信小程序通过code等登录后返回和之前一样的token后原逻辑中的SecurityUtils一样可以获取到,则需要自定义扩展登录类。废话不多说,上代码:

类似于UsernamePasswordAuthenticationToken类,自定义AuthCodeAuthenticationToken.java

在这个位置:

AuthCodeAuthenticationToken.java:

package com.ruoyi.framework;import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;import java.util.Collection;public class AuthCodeAuthenticationToken extends AbstractAuthenticationToken {private final Object principal;public AuthCodeAuthenticationToken(Object principal) {super(null);this.principal= principal;}public AuthCodeAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities) {super(authorities);this.principal= principal;super.setAuthenticated(true);}@Overridepublic Object getCredentials() {return null;}@Overridepublic Object getPrincipal() {return this.principal;}@Overridepublic void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {if (isAuthenticated) {throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");}super.setAuthenticated(false);}
}
AuthCodeAuthenticationProvider.java
package com.ruoyi.framework;import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;public class AuthCodeAuthenticationProvider implements AuthenticationProvider {private UserDetailsService userDetailsService;public AuthCodeAuthenticationProvider(UserDetailsService userDetailsService) {setUserDetailsService(userDetailsService);}@Overridepublic Authentication authenticate(Authentication authentication) throws AuthenticationException {AuthCodeAuthenticationToken authCodeAuthenticationToken = (AuthCodeAuthenticationToken) authentication;String telephone = (String) authentication.getPrincipal();UserDetails userDetails = userDetailsService.loadUserByUsername(telephone);AuthCodeAuthenticationToken authenticationResult = new AuthCodeAuthenticationToken(userDetails, userDetails.getAuthorities());authenticationResult.setDetails(authentication.getDetails());return authenticationResult;}@Overridepublic boolean supports(Class<?> authentication) {return AuthCodeAuthenticationToken.class.isAssignableFrom(authentication);}public UserDetailsService getUserDetailsService() {return userDetailsService;}public void setUserDetailsService(UserDetailsService userDetailsService) {this.userDetailsService = userDetailsService;}
}
在这里还要重写UserDetailsServiceByAuthCodeImpl.java原逻辑是调用的UserDetailsServiceImpl.java中的loadUserByUsername方法。
package com.ruoyi.framework.web.service;import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;@Slf4j
@Service("userDetailsByAuthCode")
public class UserDetailsServiceByAuthCodeImpl implements UserDetailsService {@Autowiredprivate ISysUserService userService;@Autowiredprivate SysPermissionService permissionService;@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{SysUser user = userService.selectUserByUserName(username);if (StringUtils.isNull(user)){log.info("登录用户:{} 不存在.", username);throw new ServiceException(MessageUtils.message("user.not.exists"));}else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())){log.info("登录用户:{} 已被删除.", username);throw new ServiceException(MessageUtils.message("user.password.delete"));}else if (UserStatus.DISABLE.getCode().equals(user.getStatus())){log.info("登录用户:{} 已被停用.", username);throw new ServiceException(MessageUtils.message("user.blocked"));}return createLoginUser(user);}public UserDetails createLoginUser(SysUser user){return new LoginUser(user.getUserId(), user.getDeptId(), user, permissionService.getMenuPermission(user));}
}

在SecurityConfig.java中配置自定义的登录逻辑(顶部注入,下面增加配置)

配置中api开头的接口,该放开(登录获取token的接口)的放开:

温馨提示:一定要区分原来的UserDetailsService 使用注解@Qualifier("userDetailsByAuthCode")区分,原来的也要加上,不然启动时候注入重复,原来的serviceImpl实现类也要加上,如下:

SysLoginService.java中
public String wxLogin(String userName){// 用户验证Authentication authentication = null;try{AuthCodeAuthenticationToken authCodeAuthenticationToken = new AuthCodeAuthenticationToken(userName);AuthenticationContextHolder.setContext(authCodeAuthenticationToken);// 该方法会去调用UserDetailsServiceImpl.loadUserByUsernameauthentication = authenticationManager.authenticate(authCodeAuthenticationToken);}catch (Exception e){if (e instanceof BadCredentialsException){AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));throw new UserPasswordNotMatchException();}else{AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGIN_FAIL, e.getMessage()));throw new ServiceException(e.getMessage());}}finally{AuthenticationContextHolder.clearContext();}AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));LoginUser loginUser = (LoginUser) authentication.getPrincipal();recordLoginInfo(loginUser.getUserId());// 生成tokenreturn tokenService.createToken(loginUser);}

接口调用中可以如下调用了:

相关文章:

若依系统(Security)增加微信小程序登录(自定义登录)

若依系统(分离版后端)自带的账号验证是基于 UsernamePasswordAuthenticationToken authenticationToken new UsernamePasswordAuthenticationToken(username, password); 验证&#xff0c;然后在系统中controller或service类中 SecurityUtils 工具类中直接可获取用户或用户…...

道可云人工智能元宇宙每日资讯|2024互联网岳麓峰会在长沙召开

道可云元宇宙每日简报&#xff08;2024年9月10日&#xff09;讯&#xff0c;今日元宇宙新鲜事有&#xff1a; 2024互联网岳麓峰会在长沙召开 9月9日&#xff0c;2024互联网岳麓峰会在长沙召开&#xff0c;湖南省副省长曹志强在峰会表示&#xff0c;今年上半年湖南省人工智能产…...

MySQL JDBC URL各参数详解

jdbc:mysql://localhost:3306/test?userroot&password123456&useUnicodetrue&characterEncodinggbk &autoReconnecttrue&failOverReadOnlyfalse&serverTimezoneUTC&drivercom.mysql.cj.jdbc.Driver 参数名称参数说明缺省值user指定用于连接数据库…...

celery control.shutdown

Celery 提供了 control 模块&#xff0c;允许你发送控制命令给正在运行的 worker。其中 shutdown 命令可以用来关闭一个或多个 worker。下面是如何使用 control.shutdown 来关闭 worker 的详细说明。 使用 control.shutdown 1. 导入必要的模块 首先&#xff0c;你需要导入 C…...

数据库设计与软件工程阶段的对应关系

数据库设计的很多阶段确实可以与软件工程的各阶段对应起来&#xff0c;这体现了数据库设计作为软件工程中一个核心组成部分的紧密关联性。 1. 需求分析阶段 数据库设计&#xff1a;需求分析是数据库设计的初始阶段&#xff0c;主要任务是收集和分析用户的需求&#xff0c;包括…...

基于ASP+ACCESS的教师信息管理系统

摘要 随着我国社会主义市场经济的发展和改革开放的不断深入&#xff0c;计算机的应用已遍及国民经济的各个领域&#xff0c;计算机来到我们的工作和生活中&#xff0c;改变着我们和周围的一切。在以前&#xff0c;学校用手工处理教师档案以及工资发放等繁多的工作和数据时&…...

【智能体】浅谈大模型之AI Agent

随着ChatGPT推出插件和函数调用功能&#xff0c;构建以LLM&#xff08;大语言模型&#xff09;为核心控制器的AI Agent愈发成为一个拥有无限可能的概念。 AI Agent是一种超越简单文本生成的人工智能系统。它使用大型语言模型&#xff08;LLM&#xff09;作为其核心计算引擎&am…...

大疆 嵌入式 笔记 面试题目汇总大全[嵌入式找工作必看] 比较有难度适合进阶收藏学习

24届大疆车载嵌入式系统安全面试经验 投递岗位&#xff1a;&#xff08;大疆车载&#xff09;嵌入式系统安全 投递时间&#xff1a;大疆大概在7月-8月月初开秋招岗位。8月月中开始笔试&#xff0c;8月月末开始面试。&#xff08;理论上9月&#xff0c;10月开奖。&#xff09;…...

线程池以及详解使用@Async注解异步处理方法

目录 一.什么是线程池&#xff1a; 二.使用线程池的好处&#xff1a; 三.线程池的使用场景&#xff1a; 四.使用线程池来提高Springboot项目的并发处理能力&#xff1a; 1.在application.yml配置文件中配置&#xff1a; 2.定义配置类来接受配置文件内的属性值&#xff1a…...

css鼠标移动过去变成手的图标

在css中定义 cursor:pointer;直接在html中指定 <div class"mt-2 mt-md-2 mt-lg-1 fs-md-1 fs-lg-2 " style"cursor:pointer;"></div>...

uniapp 懒加载、预加载、缓存机制深度解析

uniapp 懒加载、预加载、缓存机制深度解析 文章目录 uniapp 懒加载、预加载、缓存机制深度解析一、为什么要使用uniapp的懒加载、预加载和缓存机制二、如何使用uniapp的懒加载、预加载和缓存机制1. 懒加载2. 预加载3. 缓存机制 四、扩展与高级技巧1. 结合懒加载和预加载优化页面…...

《OpenCV计算机视觉》—— 图像形态学(腐蚀、膨胀等)

文章目录 一、图像形态学基本概念二、基本运算1.简单介绍2.代码实现 三、高级运算1.简单介绍2.代码实现 一、图像形态学基本概念 图像形态学是图像处理科学的一个独立分支&#xff0c;它基于集合论和数学形态学的理论&#xff0c;专门用于分析和处理图像中的形状和结构。图像形…...

【Rust光年纪】海洋学研究的利器:Rust语言海洋学计算库详解

探索Rust语言下的海洋学计算库&#xff1a;功能对比与选择指南 前言 随着科学技术的不断发展&#xff0c;海洋学领域对于计算和数据处理的需求也日益增长。在Rust语言中&#xff0c;出现了一系列专注于海洋学计算和数据处理的库&#xff0c;它们为海洋学工作者提供了强大的工…...

Word文档的读入【2】

现在&#xff0c;乔老师已经了解了Word文档的基本结构。 下面&#xff0c;我们通过观察一份答题卡来思考一下每条信息的具体位置。这样&#xff0c;在后面几天的学习和操作中&#xff0c;我们就能更快、更准确地读取到答题卡中的信息。 这份答题卡是由一个表格和一些段落组成。…...

报名开启 | 游戏开发缺队友?首期繁星招聘会来袭!

**点击蓝链领取游戏开发教程 ** EE GAMES 创作者社区是专注于链接每一位游戏创作者&#xff0c;提供社区交流、团队匹配、经验共享、成果展示、知识整合、最新活动资讯等全方位服务的游戏领域垂类社区。 这里不仅仅是一个游戏创作的互助平台&#xff0c;更是每一位游戏创作者…...

无法加载源https://api.nuget.org/v3/index.json的服务索引

我是用的visual studio2022 17.11.2版本&#xff0c;在运行.net c#项目的时候显示“无法加载源https://api.nuget.org/v3/index.json的服务索引”&#xff0c;从网上找了一堆方法全部没用&#xff0c;最后用这个方法解决了。亲测有效家人们 关闭VS&#xff0c;删除C:\Users\xx…...

C#--CM+Fody+HCWPF开发组合

CM&#xff1a;Caliburn.Micro(简称CM)一经推出便备受推崇&#xff0c;作为一款MVVM开发模式的经典框架&#xff0c;越来越多的受到wpf开发者的青睐.我们看一下官方的描述&#xff1a;Caliburn是一个为Xaml平台设计的小型但功能强大的框架。Micro实现了各种UI模式&#xff0c;用…...

力扣474-一和零(Java详细题解)

题目链接&#xff1a;474. 一和零 - 力扣&#xff08;LeetCode&#xff09; 前情提要&#xff1a; 因为本人最近都来刷dp类的题目所以该题就默认用dp方法来做。 最近刚学完01背包&#xff0c;所以现在的题解都是以01背包问题为基础再来写的。 如果大家不懂01背包的话&#…...

【话题】量子计算:前沿技术与应用前景深度解析

引言 在当今信息时代&#xff0c;计算能力已成为推动科技进步和社会发展的重要驱动力。随着摩尔定律逐渐接近其物理极限&#xff0c;传统计算机硬件的性能提升面临前所未有的挑战。在此背景下&#xff0c;量子计算作为一种革命性的计算范式&#xff0c;凭借其独特的量子力学属性…...

第11章 32位x86处理器编程架构

第11章 32位x86处理器编程架构 IA-32(INTEL Architecture, 32-bit)&#xff1a;INTEL 32位处理器架构简称IA-3&#xff0c;以8086处理器为基础发展起来的。该章重点介绍了IA-32处理器的工作方式和相关技术。 IA-32架构的基本执行环境 寄存器的扩展 32位处理器通用寄存器&am…...

从零开始玩转translategemma-27b-it:Ollama环境搭建与提示词详解

从零开始玩转translategemma-27b-it&#xff1a;Ollama环境搭建与提示词详解 1. 环境准备与快速部署 想要体验强大的图文翻译能力&#xff0c;首先需要搭建好运行环境。translategemma-27b-it是一个基于Ollama部署的翻译模型&#xff0c;支持文本和图片的翻译功能。 1.1 系统…...

伯克利Octo机器人框架实战:5步搞定跨平台任务迁移(附代码)

伯克利Octo机器人框架实战&#xff1a;5步搞定跨平台任务迁移&#xff08;附代码&#xff09; 在机器人开发领域&#xff0c;硬件平台的多样性一直是阻碍算法快速部署的主要瓶颈。想象一下&#xff0c;你花费数月为WidowX机械臂开发的抓取算法&#xff0c;当实验室新购入UR5工业…...

告别插件!保姆级教程:用Nginx反向代理搞定海康威视Web无插件视频预览

海康威视Web无插件视频预览的Nginx反向代理实战指南 引言 在现代安防监控系统集成中&#xff0c;海康威视设备因其稳定性和广泛兼容性成为行业首选。然而&#xff0c;传统Web集成方案往往依赖浏览器插件&#xff0c;这不仅增加了部署复杂度&#xff0c;也带来了安全风险。随着H…...

Windows 11 + CUDA 12.1 保姆级教程:手把手搞定Detectron2环境搭建(含Git加速与权限避坑)

Windows 11 CUDA 12.1 终极指南&#xff1a;零障碍搭建Detectron2开发环境 RTX 40系显卡用户注意了&#xff01;如果你正在Windows 11上尝试搭建Detectron2开发环境&#xff0c;却苦于找不到针对CUDA 12.1的完整解决方案&#xff0c;这篇指南将为你扫清所有障碍。不同于网上那…...

Apache Flink Agents 0.2.1版本发布,亮点几何?

Apache Flink社区宣布发布 Apache Flink Agents 0.2 系列的首个缺陷修复版本 0.2.1&#xff0c;包含3项缺陷和漏洞修复及小幅改进&#xff0c;还基于此构建了演示项目。版本发布情况Apache Flink社区很高兴地推出了 Apache Flink Agents 0.2.1 版本。此版本是 0.2 系列的首个缺…...

Conda环境管理全攻略:从零配置到VSCode无缝衔接(附清华镜像加速)

Conda环境管理全攻略&#xff1a;从零配置到VSCode无缝衔接&#xff08;附清华镜像加速&#xff09; 在数据科学和机器学习领域&#xff0c;Python环境的配置与管理往往是项目开始的第一步&#xff0c;也是最容易让初学者感到困惑的环节。不同项目可能需要不同版本的Python解释…...

Vue 中的 deep、v-deep 和 >>> 有什么区别?什么时候该用

点赞 收藏 学会&#x1f923;&#x1f923;&#x1f923; “你用 Element Plus 写了个按钮&#xff0c;想改下 hover 颜色&#xff0c;结果死活不生效&#xff01;最后查了半天&#xff0c;发现得加个 :deep() 才行” 其实&#xff0c;这是 Vue 中一个非常常见的坑&#xf…...

nlp_structbert_sentence-similarity_chinese-large 赋能智能客服:基于Vue前端的问题相似度匹配实践

nlp_structbert_sentence-similarity_chinese-large 赋能智能客服&#xff1a;基于Vue前端的问题相似度匹配实践 你有没有遇到过这种情况&#xff1f;在某个网站的客服对话框里&#xff0c;输入一个问题&#xff0c;等了半天&#xff0c;要么是机器人答非所问&#xff0c;要么…...

BabyOS:MCU裸机开发的轻量级框架解析

1. BabyOS&#xff1a;专为MCU裸机开发设计的轻量级框架 在嵌入式开发领域&#xff0c;重复造轮子一直是困扰工程师的痛点。每次新项目启动&#xff0c;我们总需要重新调试那些基础功能模块——从串口通信到Flash操作&#xff0c;从定时器管理到协议栈实现。BabyOS的出现&#…...

“人工智能+”政策下,企业AI转型的机遇与路径

在“人工智能”政策的大力推动下&#xff0c;企业引入AI项目与产品正成为提升竞争力、实现转型提效的关键举措。对于山东地区&#xff0c;尤其是威海地区的企业而言&#xff0c;把握这一趋势&#xff0c;积极探索AI技术的应用&#xff0c;无疑是顺应时代发展的明智选择。企业引…...