JustAuth整合第三方登錄組件樣例
一、官網(wǎng)
整合平臺(tái):
- QQ登錄
- 新浪微博登錄
- 百度登錄
- Gitee登錄
- Github登錄
- 開源中國登錄
- StackOverflow登錄
- Coding(騰訊云)登錄
- 程序員客棧登錄
- CSDN登錄
- Google登錄
- Facebook登錄
- 釘釘?shù)卿?/li>
- 阿里云登錄
- 支付寶登錄
- 華為登錄
- 飛書登錄
- 微信開放平臺(tái)登錄
- 企業(yè)微信掃碼登錄
- 企業(yè)微信網(wǎng)頁登錄
- 抖音登錄
- 京東登錄
二、樣例-微信開放平臺(tái)登錄
2.1 引入依賴
<dependency> <groupId>me.zhyd.oauth</groupId> <artifactId>JustAuth</artifactId> <version>${latest.version}</version> </dependency>
2.2 創(chuàng)建Request
AuthRequest authRequest = new AuthWeChatRequest(AuthConfig.builder() .clientId("Client ID") .clientSecret("Client Secret") .redirectUri("https://www.zhyd.me/oauth/callback/wechat") .build());
2.3 生成授權(quán)地址
我們可以直接使用以下方式生成第三方平臺(tái)的授權(quán)鏈接
String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
這個(gè)鏈接我們可以直接后臺(tái)重定向跳轉(zhuǎn),也可以返回到前端后,前端控制跳轉(zhuǎn)。前端控制的好處就是,可以將第三方的授權(quán)頁嵌入到iframe中,適配網(wǎng)站設(shè)計(jì)。
完整代碼如下
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.request.AuthWeChatOpenRequest; import me.zhyd.oauth.model.AuthCallback; import me.zhyd.oauth.request.AuthRequest; import me.zhyd.oauth.utils.AuthStateUtils; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @RestController @RequestMapping("/oauth") public class RestAuthController { @RequestMapping("/render") public void renderAuth(HttpServletResponse response) throws IOException { AuthRequest authRequest = getAuthRequest(); response.sendRedirect(authRequest.authorize(AuthStateUtils.createState())); } @RequestMapping("/callback") public Object login(AuthCallback callback) { AuthRequest authRequest = getAuthRequest(); return authRequest.login(callback); } private AuthRequest getAuthRequest() { return new AuthWeChatOpenRequest(AuthConfig.builder() .clientId("Client ID") .clientSecret("Client Secret") .redirectUri("https://www.zhyd.me/oauth/callback/wechat") .build()); } }
三、集群環(huán)境問題
3.1 JustAuth默認(rèn)使用Map做為緩存
JustAuth默認(rèn)使用ConcurrentHashMap做為緩存,在單機(jī)環(huán)境下無問題,但在集群環(huán)境下(分布式多實(shí)例部署的應(yīng)用)就會(huì)出現(xiàn)問題。
JustAuth部分源碼
public class AuthWeChatOpenRequest extends AuthDefaultRequest { public AuthWeChatOpenRequest(AuthConfig config) { super(config, AuthDefaultSource.WECHAT_OPEN); } public AuthWeChatOpenRequest(AuthConfig config, AuthStateCache authStateCache) { super(config, AuthDefaultSource.WECHAT_OPEN, authStateCache); } ..... } public class AuthDefaultCache implements AuthCache { /** * state cache */ private static Map<String, CacheState> stateCache = new ConcurrentHashMap<>(); private final ReentrantReadWriteLock cacheLock = new ReentrantReadWriteLock(true); private final Lock writeLock = cacheLock.writeLock(); private final Lock readLock = cacheLock.readLock(); public AuthDefaultCache() { if (AuthCacheConfig.schedulePrune) { this.schedulePrune(AuthCacheConfig.timeout); } } }
3.2 自定義使用Reids做緩存解決集群問題
3.2.1 實(shí)現(xiàn)AuthStateCache接口
import lombok.extern.slf4j.Slf4j; import me.zhyd.oauth.cache.AuthCacheConfig; import me.zhyd.oauth.cache.AuthStateCache; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.concurrent.TimeUnit; /** * 自定義AuthState Rds 緩存 * @author zak **/ @Slf4j @Component public class AuthStateRedisCache implements AuthStateCache { @Autowired private StringRedisTemplate stringRedisTemplate; /** * 存入緩存,默認(rèn)3分鐘 * * @param key 緩存key * @param value 緩存內(nèi)容 */ @Override public void cache(String key, String value) { log.info("RDS, 存入緩存,默認(rèn)3分鐘, key: {}, value: {}", key, value); stringRedisTemplate.opsForValue().set(key, value, AuthCacheConfig.timeout, TimeUnit.MILLISECONDS); } /** * 存入緩存 * * @param key 緩存key * @param value 緩存內(nèi)容 * @param timeout 指定緩存過期時(shí)間(毫秒) */ @Override public void cache(String key, String value, long timeout) { log.info("RDS, 存入緩存, key: {}, value: {}, timeout: {}", key, value, timeout); stringRedisTemplate.opsForValue().set(key, value, timeout, TimeUnit.MILLISECONDS); } /** * 獲取緩存內(nèi)容 * * @param key 緩存key * @return 緩存內(nèi)容 */ @Override public String get(String key) { return stringRedisTemplate.opsForValue().get(key); } /** * 是否存在key,如果對應(yīng)key的value值已過期,也返回false * * @param key 緩存key * @return true:存在key,并且value沒過期;false:key不存在或者已過期 */ @Override public boolean containsKey(String key) { return stringRedisTemplate.hasKey(key); } }
3.2.2 使用自定義緩存
@Resource private AuthStateRedisCache stateRedisCache; AuthRequest authRequest = new AuthWeChatOpenRequest(AuthConfig.builder() .clientId(wechatAppId) .clientSecret(wechatAppSecret) .redirectUri(StrUtil.isNotBlank(redirectUri) ? redirectUri : wechatRedirectUri) .build(), stateRedisCache);
以上就是JustAuth整合第三方登錄組件樣例的詳細(xì)內(nèi)容,更多關(guān)于JustAuth整合第三方登錄組件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java實(shí)現(xiàn)批量查找與替換Excel文本的思路詳解
在 Java 中,可以通過find和replace的方法來查找和替換單元格的數(shù)據(jù),下面小編將以Excel文件為例為大家介紹如何實(shí)現(xiàn)Excel文件內(nèi)容的批量替換,感興趣的朋友跟隨小編一起看看吧2023-10-10form-data與x-www-form-urlencoded的區(qū)別以及知識(shí)延伸
這篇文章主要給大家介紹了關(guān)于form-data與x-www-form-urlencoded的區(qū)別以及知識(shí)延伸,form-data和x-www-form-urlencoded都是HTTP請求中用于傳輸表單數(shù)據(jù)的編碼格式,需要的朋友可以參考下2023-11-11鴻蒙HarmonyOS App開發(fā)造輪子之自定義圓形圖片組件的實(shí)例代碼
這篇文章主要介紹了鴻蒙HarmonyOS App開發(fā)造輪子之自定義圓形圖片組件,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01使用HttpServletResponse對象獲取請求行信息
這篇文章主要介紹了使用HttpServletResponse對象獲取請求行信息,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Spring Cloud使用Feign實(shí)現(xiàn)Form表單提交的示例
本篇文章主要介紹了Spring Cloud使用Feign實(shí)現(xiàn)Form表單提交的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03SpringBoot2.3整合redis緩存自定義序列化的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot2.3整合redis緩存自定義序列化的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08