springsecurity 企业微信登入的实现示例(springsecurity oauth2)

网友投稿 619 2022-08-04


springsecurity 企业微信登入的实现示例(springsecurity oauth2)

背景

后台系统需要接入 企业微信登入,满足企业员工快速登入系统

流程图

简单代码说明

自定义一套 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 = (WeChatAuthenthttp://icationToken) 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 lambda = new QueryWrapper().lambda();

lahttp://mbda.eq(SysUserWechat::getDeleted, DataStatusEnum.NORMAL.getCode());

lambda.eq(SysUserWechat::getWechatId,weChatUserId);

List 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.getCodefgzMvv());

}

if (UserStatus.DISABLE.getCode().equals(sysUser.getStatus())) {

throw new ServiceException(GOOGLE_AUTHENTICATOR_601001.getMsg(),GOOGLE_AUTHENTICATOR_501001.getCode());

}

return createLoginUser(sysUser);

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:MyBatis 和 jeesite多表查询示例详解
下一篇:深入解析spring AOP原理及源码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~