diff --git a/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/model/dto/ApplicationDTO.java b/base-framework-common/src/main/java/com/fuhouyu/framework/function/Callback.java similarity index 63% rename from base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/model/dto/ApplicationDTO.java rename to base-framework-common/src/main/java/com/fuhouyu/framework/function/Callback.java index a37a214..be19863 100644 --- a/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/model/dto/ApplicationDTO.java +++ b/base-framework-common/src/main/java/com/fuhouyu/framework/function/Callback.java @@ -13,31 +13,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -package com.fuhouyu.framework.security.model.dto; - -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; +package com.fuhouyu.framework.function; /** *

- * 应用dto传输对象 + * 回调函数 *

* * @author fuhouyu - * @since 2024/8/15 17:16 + * @since 2024/10/22 21:01 */ -@ToString -@Getter -@Setter -public class ApplicationDTO { - - private String clientId; - - private String clientSecret; - - private Integer accessTokenExpireTime; +@FunctionalInterface +public interface Callback { - private Integer refreshTokenExpireTime; + /** + * 回调函数 + * + * @param t t + */ + void call(T t); } diff --git a/base-framework-database/src/main/java/com/fuhouyu/framework/database/interceptor/CipherFieldQueryInterceptor.java b/base-framework-database/src/main/java/com/fuhouyu/framework/database/interceptor/CipherFieldQueryInterceptor.java index a93af7a..1fdcd73 100644 --- a/base-framework-database/src/main/java/com/fuhouyu/framework/database/interceptor/CipherFieldQueryInterceptor.java +++ b/base-framework-database/src/main/java/com/fuhouyu/framework/database/interceptor/CipherFieldQueryInterceptor.java @@ -45,7 +45,6 @@ public class CipherFieldQueryInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { - // TODO 待实现 return null; } } diff --git a/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/SecurityAutoConfiguration.java b/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/SecurityAutoConfiguration.java index 3e81136..af0c8e6 100644 --- a/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/SecurityAutoConfiguration.java +++ b/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/SecurityAutoConfiguration.java @@ -66,8 +66,8 @@ public TokenStore tokenStore(CacheService cacheService) { * 认证管理器配置这里可以进行除其他登录模式的扩展,需要实现{@link AuthenticationProvider} * * @param authenticationProviders 认证提供者集合 - * @param userDetailsService 用户接口详情 - * @param passwordEncoder 密码认证管理器 + * @param userDetailsService 用户接口详情 + * @param passwordEncoder 密码认证管理器 * @return 认证管理器 */ @Bean("authenticationManager") @@ -94,7 +94,7 @@ public PasswordEncoder passwordEncoder() { /** * dao层实现 * - * @param passwordEncoder 密码管理器 + * @param passwordEncoder 密码管理器 * @param userDetailsService 用户详情接口 * @return dao默认实现 */ diff --git a/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/AbstractApplicationManager.java b/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/AbstractApplicationManager.java deleted file mode 100644 index 2ba852b..0000000 --- a/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/AbstractApplicationManager.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 2024-2024 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * 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 com.fuhouyu.framework.security.core; - -import com.fuhouyu.framework.security.model.dto.ApplicationDTO; -import org.springframework.security.authentication.AuthenticationManager; -import org.springframework.security.authentication.BadCredentialsException; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.AuthenticationException; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; - -import java.util.Objects; - -/** - *

- * 客户端管理抽象类, - * 主要解析应用 - *

- * - * @author fuhouyu - * @since 2024/8/15 17:12 - */ -public abstract class AbstractApplicationManager implements AuthenticationManager { - - @Override - public Authentication authenticate(Authentication authentication) throws AuthenticationException { - ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); - if (Objects.isNull(requestAttributes)) { - return null; - } - String applicationId = String.valueOf(authentication.getPrincipal()); - ApplicationDTO application = this.queryApplication(applicationId); - if (Objects.isNull(application)) { - throw new BadCredentialsException(String.format("%s 当前应用不存在,禁止登录", applicationId)); - } - - // 客户端密钥不匹配 - if (!Objects.equals(authentication.getCredentials(), - application.getClientSecret())) { - throw new BadCredentialsException(String.format("%s 无效的应用,禁止登录", applicationId)); - } - if (authentication instanceof UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken) { - usernamePasswordAuthenticationToken.setDetails(application); - } - SecurityContextHolder.getContext().setAuthentication(authentication); - return authentication; - } - - /** - * 通过applicationId 查询出相应的应用信息 - * - * @param applicationId 应用id - * @return 应用dto对象 - */ - public abstract ApplicationDTO queryApplication(String applicationId); -} diff --git a/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/GrantTypeAuthenticationTokenEnum.java b/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/GrantTypeAuthenticationTokenEnum.java new file mode 100644 index 0000000..80787b9 --- /dev/null +++ b/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/GrantTypeAuthenticationTokenEnum.java @@ -0,0 +1,62 @@ +/* + * Copyright 2024-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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 com.fuhouyu.framework.security.core; + +import com.fuhouyu.framework.security.core.authentication.wechat.WechatAppletsPlatformProvider; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.springframework.security.authentication.AbstractAuthenticationToken; + +/** + *

+ * 默认的映射枚举类 + *

+ * + * @author fuhouyu + * @since 2024/10/22 21:25 + */ +@RequiredArgsConstructor +@Getter +@SuppressWarnings("unchecked") +public enum GrantTypeAuthenticationTokenEnum implements GrantTypeAuthenticationTokenMapping { + + WECHAT_APPLETS("WECHAT_APPLETS") { + @Override + public Class getAuthenticationTokenClass() { + return (Class) WechatAppletsPlatformProvider.WechatAppletsAuthenticationToken.class; + } + }, + ; + + private final String grantType; + + /** + * 安全获取枚举类 + * + * @param grantType 授权类型 + * @return 枚举类 + */ + public static GrantTypeAuthenticationTokenEnum safeEnumValueOf(String grantType) { + try { + return GrantTypeAuthenticationTokenEnum.valueOf(grantType); + } catch (IllegalArgumentException | NullPointerException e) { + throw new IllegalArgumentException("Invalid value for enum: " + grantType); + } + } + + @Override + public abstract Class getAuthenticationTokenClass(); +} diff --git a/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/GrantTypeAuthenticationTokenMapping.java b/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/GrantTypeAuthenticationTokenMapping.java new file mode 100644 index 0000000..8b661fd --- /dev/null +++ b/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/GrantTypeAuthenticationTokenMapping.java @@ -0,0 +1,58 @@ +/* + * Copyright 2024-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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 com.fuhouyu.framework.security.core; + +import com.fuhouyu.framework.utils.JacksonUtil; +import org.springframework.security.authentication.AbstractAuthenticationToken; + +/** + *

+ * 授权类型和AuthenticationToken映射 + *

+ * + * @author fuhouyu + * @since 2024/10/22 21:18 + */ +public interface GrantTypeAuthenticationTokenMapping { + + /** + * 获取授权类型 + * + * @return 授权类型 + */ + String getGrantType(); + + /** + * 获取授权token类型 + * + * @param 具体的子类 + * @return 授权token类型 + */ + Class getAuthenticationTokenClass(); + + /** + * 加载该类 + * + * @param param 参数映射 + * @return AbstractAuthenticationToken 子类 + */ + default AbstractAuthenticationToken loadAuthenticationToken(Object param) { + Class authenticationTokenClass = this.getAuthenticationTokenClass(); + return JacksonUtil.tryParse(() -> + JacksonUtil.getObjectMapper().convertValue(param, authenticationTokenClass)); + } + +} diff --git a/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/authentication/wechat/WechatAppletsPlatformProvider.java b/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/authentication/wechat/WechatAppletsPlatformProvider.java index da380df..31395ad 100644 --- a/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/authentication/wechat/WechatAppletsPlatformProvider.java +++ b/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/authentication/wechat/WechatAppletsPlatformProvider.java @@ -16,6 +16,8 @@ package com.fuhouyu.framework.security.core.authentication.wechat; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fuhouyu.framework.security.core.AbstractAuthenticationProvider; import com.fuhouyu.framework.security.properties.OpenPlatformAuthProperties; import com.fuhouyu.framework.utils.JacksonUtil; @@ -141,12 +143,15 @@ public static class WechatAppletsAuthenticationToken extends AbstractAuthenticat */ private final String jsCode; + /** * 构造函数 * * @param jsCode 登录时获取的 code,可通过wx.login获取 */ - public WechatAppletsAuthenticationToken(String jsCode) { + @JsonCreator + public WechatAppletsAuthenticationToken( + @JsonProperty("jsCode") String jsCode) { super(List.of()); this.jsCode = jsCode; } diff --git a/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/filter/ApplicationBasicErrorFilter.java b/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/filter/ApplicationBasicErrorFilter.java deleted file mode 100644 index 5da255c..0000000 --- a/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/core/filter/ApplicationBasicErrorFilter.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2024-2024 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * 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 com.fuhouyu.framework.security.core.filter; - -import com.fuhouyu.framework.response.BaseResponse; -import com.fuhouyu.framework.utils.JacksonUtil; -import jakarta.servlet.ServletOutputStream; -import org.springframework.security.authentication.AuthenticationManager; -import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; - -import java.nio.charset.StandardCharsets; - -/** - *

- * 应用基本认证错误的异常过滤器 - *

- * - * @author fuhouyu - * @since 2024/8/15 17:30 - */ -public class ApplicationBasicErrorFilter extends BasicAuthenticationFilter { - - /** - * 初始化基本认证过滤器 - * Basic Base64(clientId:clientSecret). - * 主要为了处理异常错误的友好返回 - * @param authenticationManager 认证管理器 - */ - public ApplicationBasicErrorFilter(AuthenticationManager authenticationManager) { - super(authenticationManager, (request, response, authException) -> { - BaseResponse baseResponse = new BaseResponse<>() { - @Override - public Integer getCode() { - return 401; - } - - @Override - public String getMessage() { - return authException.getMessage(); - } - - @Override - public Boolean getIsSuccess() { - return false; - } - - @Override - public Void getData() { - return null; - } - }; - String body = JacksonUtil.writeValueAsString(baseResponse); - try (ServletOutputStream outputStream = response.getOutputStream()) { - outputStream.write(body.getBytes(StandardCharsets.UTF_8)); - outputStream.flush(); - } - }); - } - -} diff --git a/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/properties/OpenPlatformAuthProperties.java b/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/properties/OpenPlatformAuthProperties.java index 268d5da..a0984c9 100644 --- a/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/properties/OpenPlatformAuthProperties.java +++ b/base-framework-security-starter/src/main/java/com/fuhouyu/framework/security/properties/OpenPlatformAuthProperties.java @@ -17,6 +17,7 @@ package com.fuhouyu.framework.security.properties; import com.fuhouyu.framework.constants.ConfigPropertiesConstant; +import com.fuhouyu.framework.security.core.GrantTypeAuthenticationTokenEnum; import lombok.Getter; import lombok.Setter; import lombok.ToString; @@ -46,19 +47,7 @@ public class OpenPlatformAuthProperties { /** * 客户端相关配置 */ - private Map auth; - - /** - * 平台类型 - */ - public enum OpenPlatformAuthTypeEnum { - /** - * 微信小程序 - */ - WECHAT_APPLET, - - - } + private Map auth; /** * 授权的详情 diff --git a/base-framework-security-starter/src/test/java/com/fuhouyu/framework/security/Sm3PasswordEncoderTest.java b/base-framework-security-starter/src/test/java/com/fuhouyu/framework/security/Sm3PasswordEncoderTest.java index a6650fc..a6e0fc0 100644 --- a/base-framework-security-starter/src/test/java/com/fuhouyu/framework/security/Sm3PasswordEncoderTest.java +++ b/base-framework-security-starter/src/test/java/com/fuhouyu/framework/security/Sm3PasswordEncoderTest.java @@ -34,9 +34,9 @@ * @since 2024/9/7 22:32 */ @SpringBootTest(classes = { - SecurityAutoConfiguration.class, CacheAutoConfiguration.class, CaffeineCacheAutoconfiguration.class, + SecurityAutoConfiguration.class, }) @TestPropertySource(locations = {"classpath:application.yaml"}) class Sm3PasswordEncoderTest { diff --git a/base-framework-security-starter/src/test/java/com/fuhouyu/framework/security/TokenStoreTest.java b/base-framework-security-starter/src/test/java/com/fuhouyu/framework/security/TokenStoreTest.java index a296032..8b610ac 100644 --- a/base-framework-security-starter/src/test/java/com/fuhouyu/framework/security/TokenStoreTest.java +++ b/base-framework-security-starter/src/test/java/com/fuhouyu/framework/security/TokenStoreTest.java @@ -17,6 +17,7 @@ package com.fuhouyu.framework.security; import com.fuhouyu.framework.cache.CacheAutoConfiguration; +import com.fuhouyu.framework.cache.CaffeineCacheAutoconfiguration; import com.fuhouyu.framework.cache.service.CacheService; import com.fuhouyu.framework.security.entity.TokenEntity; import com.fuhouyu.framework.security.token.TokenStore; @@ -48,6 +49,7 @@ */ @SpringBootTest(classes = { CacheAutoConfiguration.class, + CaffeineCacheAutoconfiguration.class, SecurityAutoConfiguration.class, })