扩展使用 OAuth2协议的账户 |
SuperMap iServer 提供了 OAuth2Client 接口,用于遵循 OAuth2 协议的第三方登录方式扩展,这种第三方登录方式有 QQ、新浪微博、人人网等,其中 QQ 和新浪微博为 SuperMap iServer 内置的两种遵循 OAuth2 协议的第三方登录方式。
com.supermap.services.security.OAuth2Client 接口有如下方法:
该方法用于获取 OAuth 跳转 URI 。
该方法用于获取访问 Token 。
该方法用于获取用户 ID 。
该方法用于获取用户信息。
我们通过实现一个简单的扩展,来说明遵循 OAuth2 协议的第三方登录方式的扩展流程,以扩展“人人网账户登录”方式为例。
实现了 RenRenLoginExtended 类,继承自 OAuth2Client 接口 ,代码如下所示:
package com.supermap.services.rest.resources.impl;
import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import com.supermap.services.security.OAuth2Client;
import com.supermap.services.security.OAuthUserInfo;
import org.apache.commons.io.IOUtils;
import java.net.URISyntaxException;
import org.json.JSONException;
import org.json.JSONObject;
public class RenRenLoginExtended implements OAuth2Client {
// 人人的 OAuth2认证根地址
public static final String OAUTH_URL_RENREN = System.getProperty("OAUTH_RENREN", "https://graph.renren.com");
private static final String GET_CODE_URI_RENREN = OAUTH_URL_RENREN
+ "/oauth/authorize?client_id=%s&response_type=code&redirect_uri=%s&state=%s&display=page";
private static final String GET_TOKEN_BY_CODE_RENREN = OAUTH_URL_RENREN + "/oauth/token?client_id=%s&client_secret=%s&grant_type=authorization_code&code=%s&redirect_uri=%s";
private static final String useridUrl = "https://api.renren.com/v2/user/get?access_token=%s";
private static final String userInfoUrl = "https://api.renren.com/v2/user/get?access_token=%s&client_id=%s&userID=%s";
//获取 OAuth2跳转 URI
public String getRedirectURI(String clientID, String state, String redirectUri) {
return String.format(GET_CODE_URI_RENREN, clientID, redirectUri, state);
}
//获取访问 Token
public String getAccesstoken(String clientID, String clientSecret, String code, String redirectUri) throws IOException {
String tokenurl = String.format(GET_TOKEN_BY_CODE_RENREN, clientID, clientSecret,code,redirectUri);
try {
String tokenResult = IOUtils.toString(new URI(tokenurl), "utf-8");
JSONObject tokenObj = new JSONObject(tokenResult);
return tokenObj.getString("access_token");
} catch (JSONException e) {
throw new IOException(e);
}catch (URISyntaxException e) {
throw new IOException(e);
}
}
// 获取用户 ID
public String getUserID(String accesstoken) throws IOException {
String useridfourl = String.format(useridUrl, URLEncoder.encode(accesstoken, "utf-8"));
try {
String useridresult = IOUtils.toString(new URI(useridfourl), "utf-8");
JSONObject useridObj = new JSONObject(useridresult);
return useridObj.getJSONObject("response").getString("id");
} catch (JSONException e) {
throw new IOException(e);
}catch (URISyntaxException e) {
throw new IOException(e);
}
}
//获取用户信息
public OAuthUserInfo getUserInfo(String token, String clientID, String userID) throws IOException {
String userinfourl = String.format(userInfoUrl, URLEncoder.encode(token, "utf-8"), clientID, userID);
OAuthUserInfo userinforesult = new OAuthUserInfo();
String content = null;
try {
content = IOUtils.toString(new URI(userinfourl), "utf-8");
JSONObject userinfoJson = new JSONObject(content).getJSONObject("response");
userinforesult.figureurl = userinfoJson.getString("avatar");
userinforesult.nickName = userinfoJson.getString("name");
return userinforesult;
} catch (JSONException e) {
throw new IOException(e);
} catch (URISyntaxException e) {
throw new IOException(e);
}
}
}
将编译后包含 RenRenLoginExtended.class 文件的整个 com 目录拷贝到 SuperMap iServer Web 应用下,即 %SuperMap iServer_HOME%\webapps\iserver\WEB-INF\classes 下(先在该目录下新建 classes 文件夹)。
在产品包根目录 %SuperMap iServer_HOME%webapps/iserver/WEB-INF 下新建一个 extendedOAuth.xml 文件,里面的内容如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<extendedOAuthSettings>
<extendedOAuthSetting>
<loginType>RENREN</loginType>
<oAuth2ClientClass>com.supermap.services.rest.resources.impl.RenRenLoginExtended</oAuth2ClientClass>
</extendedOAuthSetting>
</extendedOAuthSettings>
其中<extendedOAuthSettings>为遵循 OAuth2 协议的第三方登录方式扩展实现配置集合,可以包含多个<extendedOAuthSetting>标签。每个<extendedOAuthSetting>标签对应一种遵循 OAuth2 协议的第三方登陆方式扩展实现配置,<extendedOAuthSetting>标签中的内容对应于 ExtendedOAuthSetting 中的配置项:
在产品包根目录 %SuperMap iServer_HOME%webapps/iserver/WEB-INF 下的 iserver-system.xml 文件的根节点<server>中添加如下内容:
<server>
...
<oAuthConfigs>
<oAuthConfig>
<id>1</id>
<enabled>true</enabled>
<loginType>RENREN</loginType>
<buttonText>人人网账号登陆</buttonText>
<clientSecret>b7544a9cc9524bac9641346f3720384b</clientSecret>
<clientID>fc23a7eefde348cca487c3bab60861be</clientID>
<redirectDomain>iserver.supermap.com</redirectDomain>
<loginIcon>renren.png</loginIcon>
</oAuthConfig>
</oAuthConfigs>
<!--<oAuthMetas>
<oAuthMeta><meta property="qc:admins" content="4323423424235" /></oAuthMeta>
</oAuthMetas>-->
</server>
其中<oAuthConfigs>为遵循 OAuth2 协议的第三方登陆方式 配置项集合,可以包含多个<oAuthConfig>标签。每个<oAuthConfig>标签对应一种登陆方式的配置,<oAuthConfig>中的内容对应于 OAuthConfig 中的配置项:
<oAuthMetas>标签代表的是用于验证网站地址的 meta 信息(将添加到首页 HTML 代码的 HEAD 标签中),扩展“人人网账号登录”方式不需要填写元数据,在次已将该标签注释掉。
当完成以上步骤时,表示已经成功添加了“人人网账号登录“方式。在第三方登陆配置页面(http://iserver.supermap.com:8090/iserver/manager/security/oauthconfig) 可以查看该登陆方式的配置信息,这些信息与<oAuthConfig>标签中的配置项相对应。访问 iPortal、 iServer 或 iEdge 的登陆页面,可以看到登录页面增加了“人人网账号登陆”按钮,点击此按钮可以实现用人人网账号登录 iPortal、 iServer 或 iEdge 的操作。想了解更多遵循 OAuth2 协议的第三方登录方式的使用,详细请参见:第三方登录方式的使用。