springsecurity 企业微信登入的实现示例
作者:川流不息attitude 时间:2023-06-16 16:39:35
背景
后台系统需要接入 企业微信登入,满足企业员工快速登入系统
流程图
简单代码说明
自定义一套 springsecurity 认证逻辑
主要就是 根据code 获取绑定用户信息 然后返回登入 token ,和qq ,微信 等第 3方登入 一个套路
// 自定义 WeChatAuthenticationProvider
public class WeChatAuthenticationProvider implements AuthenticationProvider {
private UserDetailsService userDetailsService;
public WeChatAuthenticationProvider(UserDetailsService userDetailsService){
this.userDetailsService = userDetailsService;
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
WeChatAuthenticationToken authenticationToken = (WeChatAuthenticationToken) authentication;
String userId = (String) authenticationToken.getPrincipal();
UserDetails userDetails = userDetailsService.loadUserByUsername(userId);
// 此时鉴权成功后,应当重新 new 一个拥有鉴权的 authenticationResult 返回
BrowserAuthenticationToken authenticationResult = new BrowserAuthenticationToken(userDetails, userDetails.getAuthorities());
authenticationResult.setDetails(authenticationToken.getDetails());
return authenticationResult;
}
@Override
public boolean supports(Class<?> authentication) {
// 判断 authentication 是不是 SmsCodeAuthenticationToken 的子类或子接口
return WeChatAuthenticationToken.class.isAssignableFrom(authentication);
}
public UserDetailsService getUserDetailsService() {
return userDetailsService;
}
public void setUserDetailsService(UserDetailsService userDetailsService) {
this.userDetailsService = userDetailsService;
}
}
// 重写 UserDetailsService
@Override
public UserDetails loadUserByUsername(String code) throws UsernameNotFoundException {
String weChatUserId = weChatService.getWeChatUserId(code);
LambdaQueryWrapper<SysUserWechat> lambda = new QueryWrapper<SysUserWechat>().lambda();
lambda.eq(SysUserWechat::getDeleted, DataStatusEnum.NORMAL.getCode());
lambda.eq(SysUserWechat::getWechatId,weChatUserId);
List<SysUserWechat> sysUserWechats = sysUserWechatService.list(lambda);
if(CollectionUtils.isEmpty(sysUserWechats)){
throw new ServiceException(GOOGLE_AUTHENTICATOR_601001.getMsg(),GOOGLE_AUTHENTICATOR_601001.getCode());
}
SysUserWechat sysUserWechat = sysUserWechats.get(0);
Long sysUserId = sysUserWechat.getSysUserId();
SysUser sysUser = userService.selectUserById(sysUserId);
if (StringUtils.isNull(sysUser)) {
throw new ServiceException(GOOGLE_AUTHENTICATOR_601001.getMsg(),GOOGLE_AUTHENTICATOR_501001.getCode());
}
if (UserStatus.DELETED.getCode().equals(sysUser.getDelFlag())) {
throw new ServiceException(GOOGLE_AUTHENTICATOR_601001.getMsg(),GOOGLE_AUTHENTICATOR_501001.getCode());
}
if (UserStatus.DISABLE.getCode().equals(sysUser.getStatus())) {
throw new ServiceException(GOOGLE_AUTHENTICATOR_601001.getMsg(),GOOGLE_AUTHENTICATOR_501001.getCode());
}
return createLoginUser(sysUser);
}
来源:https://www.jianshu.com/p/95b2bceeb530
标签:spring,security,企业微信登入
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中一个for语句导致无穷大死循环的例子
2022-12-17 08:13:33
Android开发之关于项目
2023-05-08 23:38:24
![](https://img.aspxhome.com/file/2023/7/138087_0s.png)
java模拟http的Get/Post请求,并设置ip与port代理的方法
2021-11-25 11:30:57
Java 容器类源码详解 Set
2022-03-21 18:08:40
Android4.4 WebAPI实现拍照上传功能
2023-08-17 00:46:21
Android仿微信菜单(Menu)(使用C#和Java分别实现)
2023-02-13 18:22:11
![](https://img.aspxhome.com/file/2023/3/111673_0s.gif)
Spring cloud alibaba之Ribbon负载均衡实现方案
2021-11-08 20:05:23
![](https://img.aspxhome.com/file/2023/5/83005_0s.png)
Java JDK动态代理(AOP)用法及实现原理详解
2021-11-14 16:45:21
![](https://img.aspxhome.com/file/2023/2/69292_0s.png)
C#实现利用Windows API读写INI文件的方法
2023-03-16 01:23:06
SpringCloud:feign对象传参和普通传参及遇到的坑解决
2023-02-17 11:18:58
SpringBoot + FFmpeg实现一个简单的M3U8切片转码系统
2023-11-24 19:52:38
![](https://img.aspxhome.com/file/2023/1/132131_0s.png)
启用springboot security后登录web页面需要用户名和密码的解决方法
2022-01-29 06:12:07
![](https://img.aspxhome.com/file/2023/9/92709_0s.png)
c#基于NVelocity实现代码生成
2023-06-03 14:46:18
![](https://img.aspxhome.com/file/2023/8/113828_0s.png)
C# 中使用正则表达式匹配字符的含义
2023-11-19 02:59:38
Android热修复及插件化原理示例详解
2023-03-07 01:37:52
![](https://img.aspxhome.com/file/2023/6/137176_0s.jpg)
C#中把任意类型的泛型集合转换成SQLXML数据格式的实例
2022-12-24 08:16:21
Java Runnable线程传参,实现让run访问参数
2023-09-04 21:36:42
Java中工具Jstack的使用实例
2023-07-26 15:18:02
C# 对PDF文档加密、解密(基于Spire.Cloud.SDK for .NET)
2021-11-23 05:37:26
![](https://img.aspxhome.com/file/2023/4/100174_0s.png)
使用C#实现阿拉伯数字到大写中文的转换
2023-03-03 16:34:16
![](https://img.aspxhome.com/file/2023/3/122653_0s.jpg)