整体效果
环境说明
- pig 2.10
- renren-security 4.0.0
添加依赖和配置
PIG 客户端表 信息客户端
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
<dependency>
<groupId>com.pig4cloud.shiro</groupId>
<artifactId>sso-sdk</artifactId>
<version>0.0.7</version>
</dependency>
增加配置 application.yml
oauth2:
client:
client-id: renren
client-secret: renren
target-uri: http://localhost:${server.port}/renren-security #登录后跳转到首页的地址
logout-uri: http://localhost:${server.port}/renren-security #退出后跳转的地址
sso-server-uri: http://192.168.0.33:3000 #pig认证中心的地址
scope: server
新增配置OAuth2Realm
renren-admin/io.renren.modules.sys.shiro
@Component
public class OAuth2Realm extends UserRealm {
@Autowired
private SysUserDao sysUserDao;
@Autowired
private OAuth2SsoKit auth2SsoKit;
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
OAuth2SsoAuthenticationToken oAuth2SsoAuthenticationToken = (OAuth2SsoAuthenticationToken) token;
Map<String, Object> map = auth2SsoKit.getAccessToken(oAuth2SsoAuthenticationToken.getCode());
String username = (String) map.get(Constant.username);
SysUserEntity sysUser = sysUserDao.selectOne(Wrappers.<SysUserEntity>lambdaQuery().eq(SysUserEntity::getUsername,username));
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(sysUser, sysUser.getPassword(), ByteSource.Util.bytes(sysUser.getSalt()), getName());
oAuth2SsoAuthenticationToken.setUsername(sysUser.getUsername());
oAuth2SsoAuthenticationToken.setPassword(sysUser.getPassword().toCharArray());
return info;
}
@Override
public CredentialsMatcher getCredentialsMatcher() {
return (token, info) -> true;
}
@Override
public boolean supports(AuthenticationToken token) {
return token instanceof OAuth2SsoAuthenticationToken;
}
}
配置 ShiroConfig
前端使用
http://localhost:8080/renren-admin/sso/login
退出逻辑
@ResponseBody
@RequestMapping(value = "logout", method = RequestMethod.GET)
public void logout() {
ssoKit.deleteToken();
ShiroUtils.logout();
ssoKit.logout();
}