diff --git a/continew-starter-auth/continew-starter-auth-satoken/pom.xml b/continew-starter-auth/continew-starter-auth-satoken/pom.xml new file mode 100644 index 00000000..82f4211b --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-satoken/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + top.charles7c.continew + continew-starter-auth + ${revision} + + + continew-starter-auth-satoken + jar + + ${project.artifactId} + ContiNew Starter 认证模块 - SaToken + + + + + cn.dev33 + sa-token-spring-boot3-starter + + + + + cn.dev33 + sa-token-jwt + + + \ 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/SaTokenAutoConfiguration.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenAutoConfiguration.java new file mode 100644 index 00000000..efadeeb9 --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenAutoConfiguration.java @@ -0,0 +1,89 @@ +/* + * 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.autoconfigure; + +import cn.dev33.satoken.dao.SaTokenDao; +import cn.dev33.satoken.interceptor.SaInterceptor; +import cn.dev33.satoken.jwt.StpLogicJwtForSimple; +import cn.dev33.satoken.stp.StpInterface; +import cn.dev33.satoken.stp.StpLogic; +import cn.dev33.satoken.stp.StpUtil; +import cn.hutool.core.util.ReflectUtil; +import jakarta.annotation.PostConstruct; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +/** + * Sa-Token 自动配置 + * + * @author Charles7c + * @since 1.0.0 + */ +@Slf4j +@AutoConfiguration +@RequiredArgsConstructor +@EnableConfigurationProperties(SaTokenExtensionProperties.class) +@ConditionalOnProperty(prefix = "sa-token.extension", name = "enabled", havingValue = "true") +public class SaTokenAutoConfiguration implements WebMvcConfigurer { + + private final SaTokenExtensionProperties properties; + + @Override + public void addInterceptors(InterceptorRegistry registry) { + // 注册 Sa-Token 拦截器,校验规则为 StpUtil.checkLogin() 登录校验 + registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin())).addPathPatterns("/**") + .excludePathPatterns(properties.getSecurity().getExcludes()); + } + + /** + * 整合 JWT(简单模式) + */ + @Bean + public StpLogic stpLogic() { + return new StpLogicJwtForSimple(); + } + + /** + * 自定义缓存实现 + */ + @Bean + @ConditionalOnMissingBean + public SaTokenDao saTokenDao() { + return ReflectUtil.newInstance(properties.getDaoImpl()); + } + + /** + * 权限认证实现 + */ + @Bean + @ConditionalOnMissingBean + public StpInterface stpInterface() { + return ReflectUtil.newInstance(properties.getPermissionImpl()); + } + + @PostConstruct + public void postConstruct() { + log.info("[ContiNew Starter] - Auto Configuration 'SaToken' completed initialization."); + } +} 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 new file mode 100644 index 00000000..312e639f --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenExtensionProperties.java @@ -0,0 +1,66 @@ +/* + * 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.autoconfigure; + +import cn.dev33.satoken.dao.SaTokenDao; +import cn.dev33.satoken.stp.StpInterface; +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + + +/** + * SaToken 扩展配置属性 + * + * @author Charles7c + * @since 1.0.0 + */ +@Data +@ConfigurationProperties(prefix = "sa-token.extension") +public class SaTokenExtensionProperties { + + /** + * 是否启用扩展 + */ + private boolean enabled = false; + + /** + * 自定义缓存实现 + */ + private Class daoImpl; + + /** + * 权限认证实现 + */ + private Class permissionImpl; + + /** + * 安全配置 + */ + private SecurityProperties security; + + /** + * 安全配置属性 + */ + @Data + public static class SecurityProperties { + + /** + * 排除(放行)路径配置 + */ + private String[] excludes = new String[0]; + } +} diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-auth/continew-starter-auth-satoken/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..667befd1 --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +top.charles7c.continew.starter.auth.satoken.autoconfigure.SaTokenAutoConfiguration \ No newline at end of file diff --git a/continew-starter-auth/pom.xml b/continew-starter-auth/pom.xml new file mode 100644 index 00000000..8e6bcab3 --- /dev/null +++ b/continew-starter-auth/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + top.charles7c.continew + continew-starter + ${revision} + + + continew-starter-auth + pom + + ${project.artifactId} + ContiNew Starter 认证模块 + + + continew-starter-auth-satoken + + + + + + top.charles7c.continew + continew-starter-core + + + \ No newline at end of file diff --git a/continew-starter-dependencies/pom.xml b/continew-starter-dependencies/pom.xml index a3aaa543..32e89579 100644 --- a/continew-starter-dependencies/pom.xml +++ b/continew-starter-dependencies/pom.xml @@ -55,6 +55,7 @@ 1.0.0-SNAPSHOT + 1.37.0 3.5.4.1 4.2.0 3.9.1 @@ -66,6 +67,26 @@ + + + cn.dev33 + sa-token-spring-boot3-starter + ${sa-token.version} + + + + + cn.dev33 + sa-token-jwt + ${sa-token.version} + + + cn.hutool + hutool-all + + + + com.baomidou @@ -118,6 +139,13 @@ + + + top.charles7c.continew + continew-starter-auth-satoken + ${revision} + + top.charles7c.continew diff --git a/pom.xml b/pom.xml index d0175e3b..ff254f20 100644 --- a/pom.xml +++ b/pom.xml @@ -74,6 +74,7 @@ continew-starter-captcha continew-starter-cache continew-starter-data + continew-starter-auth