feat: 新增 JustAuth 自动配置(认证模块)

This commit is contained in:
2023-11-25 18:16:06 +08:00
parent b147f6189b
commit 23e5d79615
6 changed files with 208 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-auth</artifactId>
<version>${revision}</version>
</parent>
<artifactId>continew-starter-auth-justauth</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>ContiNew Starter 认证模块 - JustAuth</description>
<dependencies>
<!-- Just Auth开箱即用的整合第三方登录的开源组件脱离繁琐的第三方登录 SDK让登录变得 So easy! -->
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
</dependency>
<dependency>
<groupId>com.xkcoding.justauth</groupId>
<artifactId>justauth-spring-boot-starter</artifactId>
</dependency>
<!-- 缓存模块 - Redisson -->
<dependency>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-cache-redisson</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,51 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.continew.starter.auth.justauth.autoconfigure;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.cache.AuthStateCache;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import top.charles7c.continew.starter.auth.justauth.impl.JustAuthStateCacheRedisImpl;
/**
* JustAuth 自动配置
*
* @author Charles7c
* @since 1.0.0
*/
@Slf4j
@AutoConfiguration(after = com.xkcoding.justauth.autoconfigure.JustAuthAutoConfiguration.class)
@ConditionalOnProperty(prefix = "justauth", value = "enabled", havingValue = "true", matchIfMissing = true)
public class JustAuthAutoConfiguration {
/**
* 自定义 State 缓存实现
*/
@Bean
@ConditionalOnProperty(prefix = "justauth.cache", value = "type", havingValue = "custom")
public AuthStateCache authStateCache() {
return new JustAuthStateCacheRedisImpl();
}
@PostConstruct
public void postConstruct() {
log.info("[ContiNew Starter] - Auto Configuration 'JustAuth' completed initialization.");
}
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.continew.starter.auth.justauth.impl;
import me.zhyd.oauth.cache.AuthStateCache;
import top.charles7c.continew.starter.cache.redisson.util.RedisUtils;
import java.time.Duration;
/**
* Just Auth 自定义 State 缓存实现Redis
*
* @author Charles7c
* @since 1.0.0
*/
public class JustAuthStateCacheRedisImpl implements AuthStateCache {
private static final String KEY_PREFIX = "SOCIAL_AUTH_STATE";
/**
* 存入缓存
*
* @param key
* key
* @param value
* 内容
*/
@Override
public void cache(String key, String value) {
// 参考:在 JustAuth 中,内置了一个基于 map 的 state 缓存器,默认缓存有效期为 3 分钟
RedisUtils.set(RedisUtils.formatKey(KEY_PREFIX, key), value,
Duration.ofMinutes(3));
}
/**
* 存入缓存
*
* @param key
* key
* @param value
* 内容
* @param timeout
* 缓存过期时间(毫秒)
*/
@Override
public void cache(String key, String value, long timeout) {
RedisUtils.set(RedisUtils.formatKey(KEY_PREFIX, key), value,
Duration.ofMillis(timeout));
}
/**
* 获取缓存内容
*
* @param key
* key
* @return 内容
*/
@Override
public String get(String key) {
return RedisUtils.get(RedisUtils.formatKey(KEY_PREFIX, key));
}
/**
* 是否存在 key如果对应 key 的 value 值已过期,也返回 false
*
* @param key
* key
* @return true存在 key并且 value 没过期falsekey 不存在或者已过期
*/
@Override
public boolean containsKey(String key) {
return RedisUtils.hasKey(RedisUtils.formatKey(KEY_PREFIX, key));
}
}

View File

@@ -0,0 +1,2 @@
top.charles7c.continew.starter.auth.justauth.autoconfigure.JustAuthAutoConfiguration
com.xkcoding.justauth.autoconfigure.JustAuthAutoConfiguration

View File

@@ -17,6 +17,7 @@
<modules>
<module>continew-starter-auth-satoken</module>
<module>continew-starter-auth-justauth</module>
</modules>
<dependencies>