diff --git a/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/constants/AuthConstants.java b/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/constants/AuthConstants.java index 810485e00..eda60baa4 100644 --- a/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/constants/AuthConstants.java +++ b/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/constants/AuthConstants.java @@ -43,4 +43,9 @@ public interface AuthConstants { */ String DEFAULT_PASSWORD = "123456"; + /** + * auth模块,jwt的失效时间,默认7天 + */ + Long DEFAULT_AUTH_JWT_TIMEOUT_SECONDS = 3600L * 24 * 7; + } diff --git a/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/expander/AuthConfigExpander.java b/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/expander/AuthConfigExpander.java index 79a8575c7..3f7cc22aa 100644 --- a/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/expander/AuthConfigExpander.java +++ b/kernel-d-auth/auth-api/src/main/java/cn/stylefeng/roses/kernel/auth/api/expander/AuthConfigExpander.java @@ -1,5 +1,6 @@ package cn.stylefeng.roses.kernel.auth.api.expander; +import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.StrUtil; import cn.stylefeng.roses.kernel.config.api.context.ConfigContext; @@ -31,12 +32,43 @@ public class AuthConfigExpander { } } + /** + * 用于auth校验的jwt的秘钥 + * + * @author fengshuonan + * @date 2021/1/2 18:52 + */ + public static String getAuthJwtSecret() { + String sysJwtSecret = ConfigContext.me().getConfigValueNullable("SYS_AUTH_JWT_SECRET", String.class); + + // 没配置就返回一个随机密码 + if (sysJwtSecret == null) { + return RandomUtil.randomString(20); + } else { + return sysJwtSecret; + } + } + + /** + * 用于auth模块权限校验的jwt失效时间 + *

+ * 这个时间也是“记住我”功能的过期时间,默认为7天 + *

+ * 如果登录的时候开启了“记住我”,则用户7天内免登录 + * + * @author fengshuonan + * @date 2021/1/2 18:53 + */ + public static Long getAuthJwtTimeoutSeconds() { + return ConfigContext.me().getSysConfigValueWithDefault("SYS_AUTH_JWT_TIMEOUT_SECONDS", Long.class, DEFAULT_AUTH_JWT_TIMEOUT_SECONDS); + } + /** * 获取session过期时间,默认3600秒 *

* 在这个时段内不操作,会将用户踢下线,从新登陆 *

- * 关于记住我功能,如果开启了记住我功能,这个参数 + * 如果开启了记住我功能,在session过期后会从新创建session * * @author fengshuonan * @date 2020/10/20 9:32 diff --git a/kernel-d-auth/auth-spring-boot-starter/pom.xml b/kernel-d-auth/auth-spring-boot-starter/pom.xml index aae5c9c3c..548e46f49 100644 --- a/kernel-d-auth/auth-spring-boot-starter/pom.xml +++ b/kernel-d-auth/auth-spring-boot-starter/pom.xml @@ -31,6 +31,13 @@ 1.0.0 + + + cn.stylefeng.roses + jwt-sdk + 1.0.0 + + diff --git a/kernel-d-auth/auth-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/auth/starter/GunsAuthAutoConfiguration.java b/kernel-d-auth/auth-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/auth/starter/GunsAuthAutoConfiguration.java index ead90ec28..43365ebe8 100644 --- a/kernel-d-auth/auth-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/auth/starter/GunsAuthAutoConfiguration.java +++ b/kernel-d-auth/auth-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/auth/starter/GunsAuthAutoConfiguration.java @@ -15,6 +15,9 @@ import cn.stylefeng.roses.kernel.auth.session.cache.logintoken.MemoryLoginTokenC import cn.stylefeng.roses.kernel.auth.session.cache.loginuser.MemoryLoginUserCache; import cn.stylefeng.roses.kernel.auth.session.cookie.DefaultSessionCookieCreator; import cn.stylefeng.roses.kernel.cache.api.constants.CacheConstants; +import cn.stylefeng.roses.kernel.jwt.JwtTokenOperator; +import cn.stylefeng.roses.kernel.jwt.api.JwtApi; +import cn.stylefeng.roses.kernel.jwt.api.pojo.config.JwtConfig; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -31,6 +34,25 @@ import java.util.Set; @Configuration public class GunsAuthAutoConfiguration { + /** + * jwt操作工具类的配置 + * + * @author fengshuonan + * @date 2020/12/1 14:40 + */ + @Bean + @ConditionalOnMissingBean(SessionManagerApi.class) + public JwtApi jwtApi() { + + JwtConfig jwtConfig = new JwtConfig(); + + // 从系统配置表中读取配置 + jwtConfig.setJwtSecret(AuthConfigExpander.getAuthJwtSecret()); + jwtConfig.setExpiredSeconds(AuthConfigExpander.getAuthJwtTimeoutSeconds()); + + return new JwtTokenOperator(jwtConfig); + } + /** * Bcrypt方式的密码加密 * @@ -38,6 +60,7 @@ public class GunsAuthAutoConfiguration { * @date 2020/12/21 17:45 */ @Bean + @ConditionalOnMissingBean(SessionManagerApi.class) public PasswordStoredEncryptApi passwordStoredEncryptApi() { return new BcryptPasswordStoredEncrypt(); } @@ -49,6 +72,7 @@ public class GunsAuthAutoConfiguration { * @date 2020/12/21 17:45 */ @Bean + @ConditionalOnMissingBean(SessionManagerApi.class) public PasswordTransferEncryptApi passwordTransferEncryptApi() { String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCytSVn3ff7eBJckAFYwgJjqE9Zq2uAL4g+hkfQqGALdT8NJKALFxNzeSD/xTBLAJrtALWbN1dvyktoVNPAuuzCZO1BxYZNaAU3IKFaj73OSPzca5SGY0ibMw0KvEPkC3sZQeqBqx+VqYAqan90BeG/r9p36Eb0wrshj5XmsFeo6QIDAQAB"; String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALK1JWfd9/t4ElyQAVjCAmOoT1mra4AviD6GR9CoYAt1Pw0koAsXE3N5IP/FMEsAmu0AtZs3V2/KS2hU08C67MJk7UHFhk1oBTcgoVqPvc5I/NxrlIZjSJszDQq8Q+QLexlB6oGrH5WpgCpqf3QF4b+v2nfoRvTCuyGPleawV6jpAgMBAAECgYBS9fUfetQcUWl0vwVhBu/FA+WSYxnMsEQ3gm7kVsX/i7Zxi4cgnt3QxXKkSg5ZQzaov6OPIuncY7UOAhMrbZtq/Hh8atdTVC/Ng/X9Bomodplq/+KTe/vIfWW5rlQAnMNFVaidxhCVRlRHNusapmj2vYwsiyI9kXUJNHryxtFC4QJBANtQuh3dtd79t3MVaC3TUD/EsGBe9TB3Eykbgv0muannC2Oq8Ci4vIp0NSA+FNCoB8ctgfKJUdBS8RLVnYyu3RcCQQDQmY+AuAXEpO9SgcYeRnQSOU2OSuC1wLt1MRVpPTdvE3bfRnkVxMrK0n3YilQWkQzfkERSG4kRFLIw605xPWn/AkEAiw3vQ9p8Yyu5MiXDjTKrchMyxZfPnHATXQANmJcCJ0DQDtymMxuWp66wtIXIStgPPnGTMAVzM0Qzh/6bS0Tf9wJAWj+1yFjVlghNyoJ+9qZAnYnRNhjLM5dZAxDjVI65pwLi0SKqTHLB0hJThBYE32aODUNba7KiEJPFrEiBvZh2fQJARbboHuHT0PqD1UTJGgodHlaw48kreBU+twext/9/jIqvwmFF88BmQgssHGW/tn4E6Qy3+rCCNWreEReY0gATYw=="; @@ -62,6 +86,7 @@ public class GunsAuthAutoConfiguration { * @date 2020/12/27 15:48 */ @Bean + @ConditionalOnMissingBean(SessionManagerApi.class) public SessionCookieCreator sessionCookieCreator() { return new DefaultSessionCookieCreator(); }