好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

背景

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

流程图

简单代码说明

自定义一套 springsecurity 认证逻辑

主要就是 根据code 获取绑定用户信息 然后返回登入 token ,和qq ,微信 等第 3方登入 一个套路

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

// 自定义 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 企业微信登入内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://www.jianshu.com/p/95b2bceeb530

查看更多关于springsecurity 企业微信登入的实现示例的详细内容...

  阅读:20次