JustAuth整合第三方登錄組件樣例
一、官網(wǎng)
整合平臺:
- QQ登錄
- 新浪微博登錄
- 百度登錄
- Gitee登錄
- Github登錄
- 開源中國登錄
- StackOverflow登錄
- Coding(騰訊云)登錄
- 程序員客棧登錄
- CSDN登錄
- Google登錄
- Facebook登錄
- 釘釘?shù)卿?/li>
- 阿里云登錄
- 支付寶登錄
- 華為登錄
- 飛書登錄
- 微信開放平臺登錄
- 企業(yè)微信掃碼登錄
- 企業(yè)微信網(wǎng)頁登錄
- 抖音登錄
- 京東登錄
二、樣例-微信開放平臺登錄
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)地址
我們可以直接使用以下方式生成第三方平臺的授權(quán)鏈接
String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
這個鏈接我們可以直接后臺重定向跳轉(zhuǎn),也可以返回到前端后,前端控制跳轉(zhuǎn)。前端控制的好處就是,可以將第三方的授權(quán)頁嵌入到iframe中,適配網(wǎng)站設(shè)計。
完整代碼如下
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做為緩存,在單機環(huán)境下無問題,但在集群環(huán)境下(分布式多實例部署的應(yīng)用)就會出現(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 實現(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 指定緩存過期時間(毫秒)
*/
@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)文章
Mybatis多數(shù)據(jù)源切換實現(xiàn)代碼
這篇文章主要介紹了Mybatis多數(shù)據(jù)源切換實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10
解決@Api注解不展示controller內(nèi)容的問題
這篇文章主要介紹了解決@Api注解不展示controller內(nèi)容的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。2022-01-01
Java多線程案例實戰(zhàn)之定時器的實現(xiàn)
在Java中可以使用多線程和定時器來實現(xiàn)定時任務(wù),下面這篇文章主要給大家介紹了關(guān)于Java多線程案例之定時器實現(xiàn)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01

