在《Github第三方登錄--通用化的第三方登陸實現(xiàn)》中我們實現(xiàn)了一個通用化的第三方登錄框架,其中包括OAUth的基本流程以及最簡單的用戶注冊。但是不同的第三方" />
時間:2023-05-20 17:48:01 | 來源:網(wǎng)站運營
時間:2023-05-20 17:48:01 來源:網(wǎng)站運營
基于Spring的新浪微博第三方登錄實現(xiàn):public class SinaWeiboOAuthService extends OAuthServiceDeractor { private static final String PROTECTED_RESOURCE_URL = "https://api.weibo.com/oauth2/get_token_info"; public SinaWeiboOAuthService(OAuthService oAuthService) { super(oAuthService, OAuthTypes.SINA_WEIBO); } @Override public OAuthUser getOAuthUser(Token accessToken) { OAuthRequest request = new OAuthRequest(Verb.POST, PROTECTED_RESOURCE_URL); this.signRequest(accessToken, request); Response response = request.send(); OAuthUser oAuthUser = new OAuthUser(); oAuthUser.setoAuthType(getoAuthType()); oAuthUser.setoAuthId(JSONPath.eval(JSON.parse(response.getBody()), "$.uid").toString()); oAuthUser.setUser(new User()); return oAuthUser; }}
SinaWeiboOAuthService需要完成獲取用戶的相關(guān)信息,并將其轉(zhuǎn)換成為OAuthUser對象,關(guān)于如何獲取新浪微博的用戶信息以及其相關(guān)參數(shù),可以參看新浪微博的官方文檔。@Configurationpublic class OAuthConfig { private static final String CALLBACK_URL = "http://tianmaying.com/oauth/%s/callback"; @Value("${oAuth.sina.appKey}") String sinaAppKey; @Value("${oAuth.sina.appSecret}") String sinaAppSecret; @Bean public OAuthServiceDeractor getSinaOAuthService(){ return new SinaWeiboOAuthService(new ServiceBuilder() .provider(SinaWeiboApi20.class) .apiKey(sinaAppKey) .apiSecret(sinaAppSecret) .callback(String.format(CALLBACK_URL, OAuthTypes.SINA_WEIBO)) .build()); }}
127.0.0.1 tianmaying.com
關(guān)鍵詞:實現(xiàn)
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。