首頁 > 軟體

springsecurity 企業微信登入的實現範例

2022-04-15 13:01:09

背景

後臺系統需要接入 企業微信登入,滿足企業員工快速登入系統

流程圖

簡單程式碼說明

自定義一套 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);
    }

到此這篇關於springsecurity 企業微信登入的實現範例的文章就介紹到這了,更多相關springsecurity 企業微信登入內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com