mirror of
				https://github.com/continew-org/continew-starter.git
				synced 2025-10-26 19:00:53 +08:00 
			
		
		
		
	feat: 新增 SaToken 自动配置(认证模块)
This commit is contained in:
		
							
								
								
									
										31
									
								
								continew-starter-auth/continew-starter-auth-satoken/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								continew-starter-auth/continew-starter-auth-satoken/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | |||||||
|  | <?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-satoken</artifactId> | ||||||
|  |     <packaging>jar</packaging> | ||||||
|  |  | ||||||
|  |     <name>${project.artifactId}</name> | ||||||
|  |     <description>ContiNew Starter 认证模块 - SaToken</description> | ||||||
|  |  | ||||||
|  |     <dependencies> | ||||||
|  |         <!-- Sa-Token(轻量级 Java 权限认证框架,让鉴权变得简单、优雅) --> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>cn.dev33</groupId> | ||||||
|  |             <artifactId>sa-token-spring-boot3-starter</artifactId> | ||||||
|  |         </dependency> | ||||||
|  |  | ||||||
|  |         <!-- Sa-Token 整合 JWT --> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>cn.dev33</groupId> | ||||||
|  |             <artifactId>sa-token-jwt</artifactId> | ||||||
|  |         </dependency> | ||||||
|  |     </dependencies> | ||||||
|  | </project> | ||||||
| @@ -0,0 +1,89 @@ | |||||||
|  | /* | ||||||
|  |  * 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.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."); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -0,0 +1,66 @@ | |||||||
|  | /* | ||||||
|  |  * 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.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<? extends SaTokenDao> daoImpl; | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 权限认证实现 | ||||||
|  |      */ | ||||||
|  |     private Class<? extends StpInterface> permissionImpl; | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 安全配置 | ||||||
|  |      */ | ||||||
|  |     private SecurityProperties security; | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 安全配置属性 | ||||||
|  |      */ | ||||||
|  |     @Data | ||||||
|  |     public static class SecurityProperties { | ||||||
|  |  | ||||||
|  |         /** | ||||||
|  |          * 排除(放行)路径配置 | ||||||
|  |          */ | ||||||
|  |         private String[] excludes = new String[0]; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -0,0 +1 @@ | |||||||
|  | top.charles7c.continew.starter.auth.satoken.autoconfigure.SaTokenAutoConfiguration | ||||||
							
								
								
									
										29
									
								
								continew-starter-auth/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								continew-starter-auth/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | |||||||
|  | <?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</artifactId> | ||||||
|  |         <version>${revision}</version> | ||||||
|  |     </parent> | ||||||
|  |  | ||||||
|  |     <artifactId>continew-starter-auth</artifactId> | ||||||
|  |     <packaging>pom</packaging> | ||||||
|  |  | ||||||
|  |     <name>${project.artifactId}</name> | ||||||
|  |     <description>ContiNew Starter 认证模块</description> | ||||||
|  |  | ||||||
|  |     <modules> | ||||||
|  |         <module>continew-starter-auth-satoken</module> | ||||||
|  |     </modules> | ||||||
|  |  | ||||||
|  |     <dependencies> | ||||||
|  |         <!-- 核心模块 --> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>top.charles7c.continew</groupId> | ||||||
|  |             <artifactId>continew-starter-core</artifactId> | ||||||
|  |         </dependency> | ||||||
|  |     </dependencies> | ||||||
|  | </project> | ||||||
| @@ -55,6 +55,7 @@ | |||||||
|  |  | ||||||
|     <properties> |     <properties> | ||||||
|         <revision>1.0.0-SNAPSHOT</revision> |         <revision>1.0.0-SNAPSHOT</revision> | ||||||
|  |         <sa-token.version>1.37.0</sa-token.version> | ||||||
|         <mybatis-plus.version>3.5.4.1</mybatis-plus.version> |         <mybatis-plus.version>3.5.4.1</mybatis-plus.version> | ||||||
|         <dynamic-datasource.version>4.2.0</dynamic-datasource.version> |         <dynamic-datasource.version>4.2.0</dynamic-datasource.version> | ||||||
|         <p6spy.version>3.9.1</p6spy.version> |         <p6spy.version>3.9.1</p6spy.version> | ||||||
| @@ -66,6 +67,26 @@ | |||||||
|  |  | ||||||
|     <dependencyManagement> |     <dependencyManagement> | ||||||
|         <dependencies> |         <dependencies> | ||||||
|  |             <!-- Sa-Token(轻量级 Java 权限认证框架,让鉴权变得简单、优雅) --> | ||||||
|  |             <dependency> | ||||||
|  |                 <groupId>cn.dev33</groupId> | ||||||
|  |                 <artifactId>sa-token-spring-boot3-starter</artifactId> | ||||||
|  |                 <version>${sa-token.version}</version> | ||||||
|  |             </dependency> | ||||||
|  |  | ||||||
|  |             <!-- Sa-Token 整合 JWT --> | ||||||
|  |             <dependency> | ||||||
|  |                 <groupId>cn.dev33</groupId> | ||||||
|  |                 <artifactId>sa-token-jwt</artifactId> | ||||||
|  |                 <version>${sa-token.version}</version> | ||||||
|  |                 <exclusions> | ||||||
|  |                     <exclusion> | ||||||
|  |                         <groupId>cn.hutool</groupId> | ||||||
|  |                         <artifactId>hutool-all</artifactId> | ||||||
|  |                     </exclusion> | ||||||
|  |                 </exclusions> | ||||||
|  |             </dependency> | ||||||
|  |  | ||||||
|             <!-- MyBatis Plus(MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,简化开发、提高效率) --> |             <!-- MyBatis Plus(MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,简化开发、提高效率) --> | ||||||
|             <dependency> |             <dependency> | ||||||
|                 <groupId>com.baomidou</groupId> |                 <groupId>com.baomidou</groupId> | ||||||
| @@ -118,6 +139,13 @@ | |||||||
|             </dependency> |             </dependency> | ||||||
|  |  | ||||||
|             <!-- ContiNew Starter 依赖 --> |             <!-- ContiNew Starter 依赖 --> | ||||||
|  |             <!-- 认证模块 - SaToken --> | ||||||
|  |             <dependency> | ||||||
|  |                 <groupId>top.charles7c.continew</groupId> | ||||||
|  |                 <artifactId>continew-starter-auth-satoken</artifactId> | ||||||
|  |                 <version>${revision}</version> | ||||||
|  |             </dependency> | ||||||
|  |  | ||||||
|             <!-- 数据访问模块 - MyBatis Plus --> |             <!-- 数据访问模块 - MyBatis Plus --> | ||||||
|             <dependency> |             <dependency> | ||||||
|                 <groupId>top.charles7c.continew</groupId> |                 <groupId>top.charles7c.continew</groupId> | ||||||
|   | |||||||
							
								
								
									
										1
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								pom.xml
									
									
									
									
									
								
							| @@ -74,6 +74,7 @@ | |||||||
|         <module>continew-starter-captcha</module> |         <module>continew-starter-captcha</module> | ||||||
|         <module>continew-starter-cache</module> |         <module>continew-starter-cache</module> | ||||||
|         <module>continew-starter-data</module> |         <module>continew-starter-data</module> | ||||||
|  |         <module>continew-starter-auth</module> | ||||||
|     </modules> |     </modules> | ||||||
|  |  | ||||||
|     <dependencies> |     <dependencies> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user