整体效果

renren-sso.mp4 (3.35MB)

环境说明

  • pig 2.10
  • renren-security 4.0.0

添加依赖和配置

  • PIG 客户端表 信息客户端

    1. INSERT INTO `pig`.`sys_oauth_client_details` (`client_id`,`resource_ids`,`client_secret`,`scope`,`authorized_grant_types`,`web_server_redirect_uri`,`authorities`,`access_token_validity`,`refresh_token_validity`,`additional_information`,`autoapprove`) VALUES ('renren',NULL,'renren','server','refresh_token,authorization_code','http://localhost:8080/renren-admin/sso/login',NULL,43200,2592001,NULL,'true');
  • renren-admin/pom.xm

  1. <dependency>
  2. <groupId>com.pig4cloud.shiro</groupId>
  3. <artifactId>sso-sdk</artifactId>
  4. <version>0.0.7</version>
  5. </dependency>
  • 增加配置 application.yml

    1. oauth2:
    2. client:
    3. client-id: renren
    4. client-secret: renren
    5. target-uri: http://localhost:${server.port}/renren-security #登录后跳转到首页的地址
    6. logout-uri: http://localhost:${server.port}/renren-security #退出后跳转的地址
    7. sso-server-uri: http://192.168.0.33:3000 #pig认证中心的地址
    8. scope: server

    新增配置OAuth2Realm

  • renren-admin/io.renren.modules.sys.shiro

    1. @Component
    2. public class OAuth2Realm extends UserRealm {
    3. @Autowired
    4. private SysUserDao sysUserDao;
    5. @Autowired
    6. private OAuth2SsoKit auth2SsoKit;
    7. @Override
    8. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    9. OAuth2SsoAuthenticationToken oAuth2SsoAuthenticationToken = (OAuth2SsoAuthenticationToken) token;
    10. Map<String, Object> map = auth2SsoKit.getAccessToken(oAuth2SsoAuthenticationToken.getCode());
    11. String username = (String) map.get(Constant.username);
    12. SysUserEntity sysUser = sysUserDao.selectOne(Wrappers.<SysUserEntity>lambdaQuery().eq(SysUserEntity::getUsername,username));
    13. SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(sysUser, sysUser.getPassword(), ByteSource.Util.bytes(sysUser.getSalt()), getName());
    14. oAuth2SsoAuthenticationToken.setUsername(sysUser.getUsername());
    15. oAuth2SsoAuthenticationToken.setPassword(sysUser.getPassword().toCharArray());
    16. return info;
    17. }
    18. @Override
    19. public CredentialsMatcher getCredentialsMatcher() {
    20. return (token, info) -> true;
    21. }
    22. @Override
    23. public boolean supports(AuthenticationToken token) {
    24. return token instanceof OAuth2SsoAuthenticationToken;
    25. }
    26. }
  • 配置 ShiroConfig

image.png
image.png

前端使用

  1. http://localhost:8080/renren-admin/sso/login

退出逻辑

  1. @ResponseBody
  2. @RequestMapping(value = "logout", method = RequestMethod.GET)
  3. public void logout() {
  4. ssoKit.deleteToken();
  5. ShiroUtils.logout();
  6. ssoKit.logout();
  7. }

image.png

❤ 问题咨询

手势点击蓝字求关注简约风动态引导关注__2022-09-07+23_18_38.gif