diff --git a/continew-starter-auth/continew-starter-auth-justauth/pom.xml b/continew-starter-auth/continew-starter-auth-justauth/pom.xml
index 6554e264..619e538d 100644
--- a/continew-starter-auth/continew-starter-auth-justauth/pom.xml
+++ b/continew-starter-auth/continew-starter-auth-justauth/pom.xml
@@ -26,11 +26,5 @@
+ * 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 + *
+ * http://www.gnu.org/licenses/lgpl.html + *
+ * 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.satoken.autoconfigure; + +import cn.dev33.satoken.dao.SaTokenDao; +import cn.hutool.core.util.ReflectUtil; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.redisson.client.RedisClient; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import top.charles7c.continew.starter.auth.satoken.impl.SaTokenDaoRedisImpl; +import top.charles7c.continew.starter.cache.redisson.autoconfigure.RedissonAutoConfiguration; + +/** + * SaToken 持久层配置 + * + * @author Charles7c + * @since 1.0.0 + */ +@Slf4j +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public abstract class SaTokenDaoConfiguration { + + /** + * 自定义持久层实现类-Redis + */ + @ConditionalOnClass(RedisClient.class) + @ConditionalOnMissingBean(SaTokenDao.class) + @AutoConfigureBefore(RedissonAutoConfiguration.class) + @ConditionalOnProperty(name = "sa-token.extension.dao.type", havingValue = "redis") + static class Redis { + static { + log.debug("[ContiNew Starter] - Auto Configuration 'SaToken-SaTokenDao-Redis' completed initialization."); + } + + @Bean + public SaTokenDao saTokenDao() { + return new SaTokenDaoRedisImpl(); + } + } + + /** + * 自定义持久层实现类-自定义 + */ + @ConditionalOnProperty(name = "sa-token.extension.dao.type", havingValue = "custom") + static class Custom { + static { + log.debug("[ContiNew Starter] - Auto Configuration 'SaToken-SaTokenDao-Custom' completed initialization."); + } + + @Bean + public SaTokenDao saTokenDao(SaTokenExtensionProperties properties) { + return ReflectUtil.newInstance(properties.getDao().getImpl()); + } + } +} \ No newline at end of file diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenExtensionProperties.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenExtensionProperties.java index 312e639f..9c4894a5 100644 --- a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenExtensionProperties.java +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenExtensionProperties.java @@ -16,10 +16,12 @@ package top.charles7c.continew.starter.auth.satoken.autoconfigure; -import cn.dev33.satoken.dao.SaTokenDao; import cn.dev33.satoken.stp.StpInterface; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.NestedConfigurationProperty; +import top.charles7c.continew.starter.auth.satoken.properties.SaTokenDaoProperties; +import top.charles7c.continew.starter.auth.satoken.properties.SaTokenSecurityProperties; /** @@ -37,30 +39,20 @@ public class SaTokenExtensionProperties { */ private boolean enabled = false; - /** - * 自定义缓存实现 - */ - private Class extends SaTokenDao> daoImpl; - /** * 权限认证实现 */ private Class extends StpInterface> permissionImpl; /** - * 安全配置 + * 持久层配置 */ - private SecurityProperties security; + @NestedConfigurationProperty + private SaTokenDaoProperties dao; /** - * 安全配置属性 + * 安全配置 */ - @Data - public static class SecurityProperties { - - /** - * 排除(放行)路径配置 - */ - private String[] excludes = new String[0]; - } + @NestedConfigurationProperty + private SaTokenSecurityProperties security; } diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/enums/SaTokenDaoTypeEnum.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/enums/SaTokenDaoTypeEnum.java new file mode 100644 index 00000000..cb1e998c --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/enums/SaTokenDaoTypeEnum.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *
+ * 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 + *
+ * http://www.gnu.org/licenses/lgpl.html + *
+ * 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.satoken.enums; + +/** + * SaToken 持久层类型枚举 + * + * @author Charles7c + * @since 1.0.0 + */ +public enum SaTokenDaoTypeEnum { + + /** + * Redis + */ + REDIS, + + /** + * 自定义 + */ + CUSTOM +} diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/impl/SaTokenDaoRedisImpl.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/impl/SaTokenDaoRedisImpl.java new file mode 100644 index 00000000..9c49c3e1 --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/impl/SaTokenDaoRedisImpl.java @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *
+ * 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 + *
+ * http://www.gnu.org/licenses/lgpl.html + *
+ * 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.satoken.impl;
+
+import cn.dev33.satoken.dao.SaTokenDao;
+import cn.dev33.satoken.util.SaFoxUtil;
+import top.charles7c.continew.starter.cache.redisson.util.RedisUtils;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * Sa-Token 持久层 Redis 实现(参考:Sa-Token/sa-token-plugin/sa-token-dao-redisx/SaTokenDaoOfRedis.java)
+ *
+ * @author Charles7c
+ * @since 1.0.0
+ */
+public class SaTokenDaoRedisImpl implements SaTokenDao {
+
+ @Override
+ public String get(String key) {
+ return RedisUtils.get(key);
+ }
+
+ @Override
+ public void set(String key, String value, long timeout) {
+ if (timeout == 0 || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) {
+ return;
+ }
+ // 判断是否为永不过期
+ if (timeout == SaTokenDao.NEVER_EXPIRE) {
+ RedisUtils.set(key, value);
+ } else {
+ RedisUtils.set(key, value, Duration.ofSeconds(timeout));
+ }
+ }
+
+ @Override
+ public void update(String key, String value) {
+ long expire = getTimeout(key);
+ // -2:无此键
+ if (expire == SaTokenDao.NOT_VALUE_EXPIRE) {
+ return;
+ }
+ this.set(key, value, expire);
+ }
+
+ @Override
+ public void delete(String key) {
+ RedisUtils.delete(key);
+ }
+
+ @Override
+ public long getTimeout(String key) {
+ long timeout = RedisUtils.getTimeToLive(key);
+ return timeout < 0 ? timeout : timeout / 1000;
+ }
+
+ @Override
+ public void updateTimeout(String key, long timeout) {
+ // 判断是否想要设置为永久
+ if (timeout == SaTokenDao.NEVER_EXPIRE) {
+ long expire = getTimeout(key);
+ if (expire == SaTokenDao.NEVER_EXPIRE) {
+ // 如果其已经被设置为永久,则不作任何处理
+ } else {
+ // 如果尚未被设置为永久,那么再次 set 一次
+ this.set(key, this.get(key), timeout);
+ }
+ return;
+ }
+ RedisUtils.expire(key, Duration.ofSeconds(timeout));
+ }
+
+ @Override
+ public Object getObject(String key) {
+ return RedisUtils.get(key);
+ }
+
+ @Override
+ public void setObject(String key, Object object, long timeout) {
+ if (0 == timeout || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) {
+ return;
+ }
+ // 判断是否为永不过期
+ if (timeout == SaTokenDao.NEVER_EXPIRE) {
+ RedisUtils.set(key, object);
+ } else {
+ RedisUtils.set(key, object, Duration.ofSeconds(timeout));
+ }
+ }
+
+ @Override
+ public void updateObject(String key, Object object) {
+ long expire = getObjectTimeout(key);
+ // -2:无此键
+ if (expire == SaTokenDao.NOT_VALUE_EXPIRE) {
+ return;
+ }
+ this.setObject(key, object, expire);
+ }
+
+ @Override
+ public void deleteObject(String key) {
+ RedisUtils.delete(key);
+ }
+
+ @Override
+ public long getObjectTimeout(String key) {
+ long timeout = RedisUtils.getTimeToLive(key);
+ return timeout < 0 ? timeout : timeout / 1000;
+ }
+
+ @Override
+ public void updateObjectTimeout(String key, long timeout) {
+ // 判断是否想要设置为永久
+ if (timeout == SaTokenDao.NEVER_EXPIRE) {
+ long expire = getObjectTimeout(key);
+ if (expire == SaTokenDao.NEVER_EXPIRE) {
+ // 如果其已经被设置为永久,则不作任何处理
+ } else {
+ // 如果尚未被设置为永久,那么再次 set 一次
+ this.setObject(key, this.getObject(key), timeout);
+ }
+ return;
+ }
+ RedisUtils.expire(key, Duration.ofSeconds(timeout));
+ }
+
+ @Override
+ public List
+ * 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
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * 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.satoken.properties;
+
+import cn.dev33.satoken.dao.SaTokenDao;
+import lombok.Data;
+import top.charles7c.continew.starter.auth.satoken.enums.SaTokenDaoTypeEnum;
+
+/**
+ * SaToken 持久层配置属性
+ *
+ * @author Charles7c
+ * @since 1.0.0
+ */
+@Data
+public class SaTokenDaoProperties {
+
+ /**
+ * 持久层类型
+ */
+ private SaTokenDaoTypeEnum type;
+
+ /**
+ * 自定义持久层实现类(当 type 为 CUSTOM 时必填)
+ */
+ private Class extends SaTokenDao> impl;
+}
diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/properties/SaTokenSecurityProperties.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/properties/SaTokenSecurityProperties.java
new file mode 100644
index 00000000..4e7002b9
--- /dev/null
+++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/properties/SaTokenSecurityProperties.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
+ *
+ * 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
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * 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.satoken.properties;
+
+import lombok.Data;
+
+/**
+ * SaToken 安全配置属性
+ *
+ * @author Charles7c
+ * @since 1.0.0
+ */
+@Data
+public class SaTokenSecurityProperties {
+
+ /**
+ * 排除(放行)路径配置
+ */
+ private String[] excludes = new String[0];
+}
\ No newline at end of file
diff --git a/continew-starter-auth/pom.xml b/continew-starter-auth/pom.xml
index 23de50c7..7735e69c 100644
--- a/continew-starter-auth/pom.xml
+++ b/continew-starter-auth/pom.xml
@@ -21,10 +21,10 @@