diff --git a/Cpop-Common/src/main/java/com/cpop/common/constant/Constants.java b/Cpop-Common/src/main/java/com/cpop/common/constant/Constants.java index 2b97c40..6617db5 100644 --- a/Cpop-Common/src/main/java/com/cpop/common/constant/Constants.java +++ b/Cpop-Common/src/main/java/com/cpop/common/constant/Constants.java @@ -174,6 +174,11 @@ public interface Constants { */ String SUPER_ADMIN = "Cpop"; + /** + * 超级管理员ID + */ + String SUPER_ADMIN_ID = "1"; + /** * 超级管理员 */ diff --git a/Cpop-Core/src/main/java/com/cpop/core/abstracts/AbstractLoginInfoBuild.java b/Cpop-Core/src/main/java/com/cpop/core/abstracts/AbstractLoginInfoBuild.java deleted file mode 100644 index 5a98869..0000000 --- a/Cpop-Core/src/main/java/com/cpop/core/abstracts/AbstractLoginInfoBuild.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.cpop.core.abstracts; - -import com.cpop.core.base.entity.LoginUser; -import com.cpop.core.base.enums.UserType; -import com.cpop.core.base.table.SysUser; -import com.cpop.core.gateway.miniProgram.MiniUserLoginInfoBuild; -import com.cpop.core.gateway.sys.SysLoginInfoBuild; - -import java.util.Map; -import java.util.Set; - -/** - * @author DB - * @createTime 2023/10/19 11:26 - * @description - */ -public abstract class AbstractLoginInfoBuild { - - /** - * @descriptions 构建用户信息 - * @author DB - * @date 2023/10/19 12:50 - * @param userType 用户类型 - * @return: com.cpop.core.abstracts.AbstractLoginInfoBuild - */ - public static AbstractLoginInfoBuild getInstance(UserType userType){ - switch (userType){ - //系统用户 - case OAM_USER: - case MALL_USER: - return new SysLoginInfoBuild(); - //小程序用户 - case MINI_USER: - return new MiniUserLoginInfoBuild(); - default: - return new SysLoginInfoBuild(); - } - } - - /** - * 构建用户 - */ - public abstract LoginUser buildLoginUser(SysUser user, Map credentials); - -} diff --git a/Cpop-Core/src/main/java/com/cpop/core/annontation/StringArrayConvert.java b/Cpop-Core/src/main/java/com/cpop/core/annontation/StringArrayConvert.java index f9fd009..9ca0900 100644 --- a/Cpop-Core/src/main/java/com/cpop/core/annontation/StringArrayConvert.java +++ b/Cpop-Core/src/main/java/com/cpop/core/annontation/StringArrayConvert.java @@ -3,8 +3,8 @@ package com.cpop.core.annontation; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; -import com.cpop.core.strategy.ArrayToStringDeserializer; -import com.cpop.core.strategy.StringToArraySerializer; +import com.cpop.core.strategy.json.ArrayToStringDeserializer; +import com.cpop.core.strategy.json.StringToArraySerializer; import java.lang.annotation.*; diff --git a/Cpop-Core/src/main/java/com/cpop/core/base/entity/loginInfo/MiniUserLoginInfo.java b/Cpop-Core/src/main/java/com/cpop/core/base/entity/loginInfo/MiniUserLoginInfo.java index 352d6e9..c58acc4 100644 --- a/Cpop-Core/src/main/java/com/cpop/core/base/entity/loginInfo/MiniUserLoginInfo.java +++ b/Cpop-Core/src/main/java/com/cpop/core/base/entity/loginInfo/MiniUserLoginInfo.java @@ -2,7 +2,6 @@ package com.cpop.core.base.entity.loginInfo; import com.cpop.core.base.enums.SourceType; import com.cpop.core.base.table.SysUser; -import com.cpop.core.gateway.miniProgram.MiniUserLoginInfoBuild; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; diff --git a/Cpop-Core/src/main/java/com/cpop/core/base/enums/UserType.java b/Cpop-Core/src/main/java/com/cpop/core/base/enums/UserType.java index 75bd961..67842e8 100644 --- a/Cpop-Core/src/main/java/com/cpop/core/base/enums/UserType.java +++ b/Cpop-Core/src/main/java/com/cpop/core/base/enums/UserType.java @@ -1,5 +1,10 @@ package com.cpop.core.base.enums; +import com.cpop.core.strategy.login.LoginStrategy; +import com.cpop.core.strategy.login.MallLoginStrategy; +import com.cpop.core.strategy.login.MiniLoginStrategy; +import com.cpop.core.strategy.login.OamLoginStrategy; +import com.cpop.core.utils.SpringUtils; import lombok.Getter; /** @@ -12,15 +17,15 @@ public enum UserType { /** * oam系统员工 */ - OAM_USER(0, "oam:loginUser:"), + OAM_USER(0, "oam:loginUser:", SpringUtils.getBean(OamLoginStrategy.class)), /** * 小程序用户 */ - MINI_USER(1, "mini:loginUser:"), + MINI_USER(1, "mini:loginUser:", SpringUtils.getBean(MiniLoginStrategy.class)), /** * 商城系统员工 */ - MALL_USER(2, "mall:loginUser:"); + MALL_USER(2, "mall:loginUser:",SpringUtils.getBean(MallLoginStrategy.class)); /** * code @@ -32,9 +37,12 @@ public enum UserType { */ private final String key; - UserType(Integer code, String key) { + private final LoginStrategy strategy; + + UserType(Integer code, String key, LoginStrategy strategy) { this.code = code; this.key = key; + this.strategy = strategy; } } diff --git a/Cpop-Core/src/main/java/com/cpop/core/config/SecurityConfig.java b/Cpop-Core/src/main/java/com/cpop/core/config/SecurityConfig.java index 8c0b0ae..302f478 100644 --- a/Cpop-Core/src/main/java/com/cpop/core/config/SecurityConfig.java +++ b/Cpop-Core/src/main/java/com/cpop/core/config/SecurityConfig.java @@ -6,21 +6,17 @@ import com.cpop.core.filter.JwtAuthenticationFilter; import com.cpop.core.filter.RepeatableFilter; import com.cpop.core.gateway.miniProgram.MiniProgramAuthenticationFilter; import com.cpop.core.gateway.miniProgram.MiniProgramAuthenticationProvider; -import com.cpop.core.gateway.oam.CpopUsernamePasswordAuthenticationFilter; import com.cpop.core.gateway.sys.SysAuthenticationFilter; import com.cpop.core.gateway.sys.SysAuthenticationProvider; import com.cpop.core.handler.*; -import com.cpop.core.service.impl.OamStaffDetailsServiceImpl; import com.cpop.core.utils.JwtUtils; import com.cpop.core.utils.PasswordEncoder; -import com.cpop.core.utils.SpringUtils; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.ProviderManager; -import org.springframework.security.authentication.dao.DaoAuthenticationProvider; import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; @@ -119,36 +115,6 @@ public class SecurityConfig implements WebMvcConfigurer { return http.build(); } - /** - * 自定义用户密码过滤器 - * - * @param authenticationManager 认证管理器 - * @return OamUsernamePasswordAuthenticationFilter oma管理器 - */ - @Bean - public CpopUsernamePasswordAuthenticationFilter oamLoginFilter(AuthenticationManager authenticationManager) { - CpopUsernamePasswordAuthenticationFilter loginFilter = new CpopUsernamePasswordAuthenticationFilter(); - loginFilter.setFilterProcessesUrl("/login"); - loginFilter.setAuthenticationManager(authenticationManager); - loginFilter.setAuthenticationSuccessHandler(loginSuccessHandler); - loginFilter.setAuthenticationFailureHandler(loginFailureHandler); - return loginFilter; - } - - /** - * @Description: 默认的认证方式 - * @return DaoAuthenticationProvider - * @author DB - * @Date: 2023/8/24 0024 16:37 - */ - @Bean - public DaoAuthenticationProvider authenticationProvider() { - DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider(); - authProvider.setUserDetailsService(SpringUtils.getBean(OamStaffDetailsServiceImpl.class)); - authProvider.setPasswordEncoder(passwordEncoder()); - return authProvider; - } - /** * @Description: 小程序登陆认证方式(可自定义其他模式) * @param authenticationManager 认证管理器 @@ -197,7 +163,7 @@ public class SecurityConfig implements WebMvcConfigurer { */ @Bean public AuthenticationManager authenticationManager() { - return new ProviderManager(Arrays.asList(authenticationProvider(), miniProgramAuthenticationProvider(), sysAuthenticationProvider())); + return new ProviderManager(Arrays.asList(sysAuthenticationProvider(), miniProgramAuthenticationProvider())); } /** diff --git a/Cpop-Core/src/main/java/com/cpop/core/filter/JwtAuthenticationFilter.java b/Cpop-Core/src/main/java/com/cpop/core/filter/JwtAuthenticationFilter.java index 1ef2de4..db767cf 100644 --- a/Cpop-Core/src/main/java/com/cpop/core/filter/JwtAuthenticationFilter.java +++ b/Cpop-Core/src/main/java/com/cpop/core/filter/JwtAuthenticationFilter.java @@ -92,7 +92,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter { } //获取当前请求ip loginUser.setIpAddr(IpUtils.getIpAddr(request)); - multipleAuth(loginUser,username); + multipleAuth(loginUser); chain.doFilter(request, response); } @@ -122,17 +122,15 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter { private LoginUser multipleLoadUser(UserType userType, String username) { LoginUser loginUser = null; switch (userType) { - //OAM + //系统登陆 case OAM_USER: - // 获取用户的权限等信息 - loginUser = (LoginUser) oamStaffDetailsService.loadUserByUsername(username); + case MALL_USER: + loginUser = coreService.loadUserByUsername(username, userType); break; case MINI_USER: // 获取用户的权限等信息 loginUser = coreService.loadUserByPhone(username, userType, null); break; - case MALL_USER: - loginUser = coreService.loadUserByUsername(username, userType); default: } return loginUser; @@ -144,22 +142,20 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter { * @author DB * @Date: 2023/8/24 0024 17:12 */ - private void multipleAuth(LoginUser loginUser, String username) { + private void multipleAuth(LoginUser loginUser) { switch (loginUser.getUserType()) { case OAM_USER: + case MALL_USER: + //构建通用系统用户登陆 // 构建UsernamePasswordAuthenticationToken,这里密码为null,是因为提供了正确的JWT,实现自动登录 - UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, null); - SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken); + SysAuthenticationToken sysAuthenticationToken = new SysAuthenticationToken(loginUser.getIdentification(), loginUser, null); + SecurityContextHolder.getContext().setAuthentication(sysAuthenticationToken); break; case MINI_USER: // MiniProgramAuthenticationToken,实现自动登录 MiniProgramAuthenticationToken miniProgramAuthenticationToken = new MiniProgramAuthenticationToken(loginUser.getIdentification(), loginUser, null); SecurityContextHolder.getContext().setAuthentication(miniProgramAuthenticationToken); break; - case MALL_USER: - //构建通用系统用户登陆 - SysAuthenticationToken sysAuthenticationToken = new SysAuthenticationToken(loginUser.getIdentification(), loginUser, null); - SecurityContextHolder.getContext().setAuthentication(sysAuthenticationToken); default: } diff --git a/Cpop-Core/src/main/java/com/cpop/core/gateway/oam/CpopUsernamePasswordAuthenticationFilter.java b/Cpop-Core/src/main/java/com/cpop/core/gateway/oam/CpopUsernamePasswordAuthenticationFilter.java deleted file mode 100644 index b5d047b..0000000 --- a/Cpop-Core/src/main/java/com/cpop/core/gateway/oam/CpopUsernamePasswordAuthenticationFilter.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.cpop.core.gateway.oam; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.springframework.security.authentication.AuthenticationServiceException; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.AuthenticationException; -import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.InputStream; -import java.util.Map; - -/** - * @author DB - * @create 2023-04-05 17:38 - * Cpop管理系统为基础认证系统,使用默认的过滤认证方式 - */ -public class CpopUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter { - - @SuppressWarnings("rawtypes") - @Override - public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { - if ("POST".equalsIgnoreCase(request.getMethod())) { - ObjectMapper mapper = new ObjectMapper(); - InputStream inputStream; - Map authenticationBean; - try { - inputStream = request.getInputStream(); - authenticationBean = mapper.readValue(inputStream, Map.class); - String username = (String) authenticationBean.get("username"); - String password = (String) authenticationBean.get("password"); - UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password); - this.setDetails(request, authRequest); - return this.getAuthenticationManager().authenticate(authRequest); - } catch (IOException e) { - throw new AuthenticationServiceException("Authentication method io exception: " + request.getMethod()); - } - } - // 只能是post请求 - throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod()); - } -} diff --git a/Cpop-Core/src/main/java/com/cpop/core/gateway/sys/SysAuthenticationProvider.java b/Cpop-Core/src/main/java/com/cpop/core/gateway/sys/SysAuthenticationProvider.java index f13cd65..11009d8 100644 --- a/Cpop-Core/src/main/java/com/cpop/core/gateway/sys/SysAuthenticationProvider.java +++ b/Cpop-Core/src/main/java/com/cpop/core/gateway/sys/SysAuthenticationProvider.java @@ -25,7 +25,7 @@ public class SysAuthenticationProvider implements AuthenticationProvider { // 这个获取表单输入中返回的用户名; String principal = (String) authentication.getPrincipal(); // 这个是表单中输入的密码与用户类型 - Map credentials = (Map) authentication.getCredentials(); + Map credentials = (Map) authentication.getCredentials(); UserType userType = (UserType) credentials.get("userType"); //认证用户名密码 LoginUser loginUser = SpringUtils.getBean(CoreService.class).loadUserByUsername(principal, userType); diff --git a/Cpop-Core/src/main/java/com/cpop/core/service/impl/CoreServiceImpl.java b/Cpop-Core/src/main/java/com/cpop/core/service/impl/CoreServiceImpl.java index 56f92be..191db85 100644 --- a/Cpop-Core/src/main/java/com/cpop/core/service/impl/CoreServiceImpl.java +++ b/Cpop-Core/src/main/java/com/cpop/core/service/impl/CoreServiceImpl.java @@ -1,6 +1,5 @@ package com.cpop.core.service.impl; -import com.cpop.core.abstracts.AbstractLoginInfoBuild; import com.cpop.core.base.entity.LoginUser; import com.cpop.core.base.enums.OperationLogEnum; import com.cpop.core.base.enums.UserType; @@ -212,15 +211,7 @@ public class CoreServiceImpl implements CoreService { if (sysUser == null) { throw new UsernameNotFoundException(MessageUtils.message("i18n_alert_accountOrPwdError")); } - //构建登陆用户信息 - AbstractLoginInfoBuild instance = AbstractLoginInfoBuild.getInstance(userType); - LoginUser loginUser = instance.buildLoginUser(sysUser, null); - loginUser.setUserId(sysUser.getId()) - .setUserType(userType) - .setUserName(username) - .setLoginTime(System.currentTimeMillis()) - .setStatus(sysUser.getStatus()); - return loginUser; + return userType.getStrategy().getLoginUserInfo(sysUser, null); } /** @@ -253,14 +244,7 @@ public class CoreServiceImpl implements CoreService { } } //构建登陆用户信息 - AbstractLoginInfoBuild instance = AbstractLoginInfoBuild.getInstance(userType); - LoginUser loginUser = instance.buildLoginUser(sysUser, credentials); - loginUser.setUserId(sysUser.getId()) - .setUserType(userType) - .setUserName(sysUser.getUserName()) - .setLoginTime(System.currentTimeMillis()) - .setStatus(sysUser.getStatus()); - return loginUser; + return userType.getStrategy().getLoginUserInfo(sysUser, credentials); } /** diff --git a/Cpop-Core/src/main/java/com/cpop/core/strategy/ArrayToStringDeserializer.java b/Cpop-Core/src/main/java/com/cpop/core/strategy/json/ArrayToStringDeserializer.java similarity index 97% rename from Cpop-Core/src/main/java/com/cpop/core/strategy/ArrayToStringDeserializer.java rename to Cpop-Core/src/main/java/com/cpop/core/strategy/json/ArrayToStringDeserializer.java index 015042a..5bb7ea7 100644 --- a/Cpop-Core/src/main/java/com/cpop/core/strategy/ArrayToStringDeserializer.java +++ b/Cpop-Core/src/main/java/com/cpop/core/strategy/json/ArrayToStringDeserializer.java @@ -1,4 +1,4 @@ -package com.cpop.core.strategy; +package com.cpop.core.strategy.json; import com.cpop.common.utils.StringUtils; import com.fasterxml.jackson.core.JsonParser; diff --git a/Cpop-Core/src/main/java/com/cpop/core/strategy/StringToArraySerializer.java b/Cpop-Core/src/main/java/com/cpop/core/strategy/json/StringToArraySerializer.java similarity index 95% rename from Cpop-Core/src/main/java/com/cpop/core/strategy/StringToArraySerializer.java rename to Cpop-Core/src/main/java/com/cpop/core/strategy/json/StringToArraySerializer.java index fb629c6..95043ad 100644 --- a/Cpop-Core/src/main/java/com/cpop/core/strategy/StringToArraySerializer.java +++ b/Cpop-Core/src/main/java/com/cpop/core/strategy/json/StringToArraySerializer.java @@ -1,4 +1,4 @@ -package com.cpop.core.strategy; +package com.cpop.core.strategy.json; import com.cpop.common.utils.StringUtils; import com.fasterxml.jackson.core.JsonGenerator; diff --git a/Cpop-Core/src/main/java/com/cpop/core/strategy/login/LoginStrategy.java b/Cpop-Core/src/main/java/com/cpop/core/strategy/login/LoginStrategy.java new file mode 100644 index 0000000..1b5b089 --- /dev/null +++ b/Cpop-Core/src/main/java/com/cpop/core/strategy/login/LoginStrategy.java @@ -0,0 +1,23 @@ +package com.cpop.core.strategy.login; + +import com.cpop.core.base.entity.LoginUser; +import com.cpop.core.base.table.SysUser; + +import java.util.Map; + +/** + * 登陆策略 + * @author DB + */ +public interface LoginStrategy { + + /** + * @descriptions 获取登陆用户信息 + * @author DB + * @date 2023/11/08 9:34 + * @param sysUser 系统用户 + * @param credentials 登陆相关凭着 + * @return: com.cpop.core.base.entity.LoginUser + */ + LoginUser getLoginUserInfo(SysUser sysUser, Map credentials); +} diff --git a/Cpop-Core/src/main/java/com/cpop/core/gateway/sys/SysLoginInfoBuild.java b/Cpop-Core/src/main/java/com/cpop/core/strategy/login/MallLoginStrategy.java similarity index 55% rename from Cpop-Core/src/main/java/com/cpop/core/gateway/sys/SysLoginInfoBuild.java rename to Cpop-Core/src/main/java/com/cpop/core/strategy/login/MallLoginStrategy.java index 5a1b3f9..35694c6 100644 --- a/Cpop-Core/src/main/java/com/cpop/core/gateway/sys/SysLoginInfoBuild.java +++ b/Cpop-Core/src/main/java/com/cpop/core/strategy/login/MallLoginStrategy.java @@ -1,82 +1,39 @@ -package com.cpop.core.gateway.sys; +package com.cpop.core.strategy.login; import com.cpop.common.constant.Constants; import com.cpop.common.utils.bean.BeanUtils; -import com.cpop.core.abstracts.AbstractLoginInfoBuild; import com.cpop.core.base.entity.LoginUser; -import com.cpop.core.base.entity.Permission; import com.cpop.core.base.entity.loginInfo.MallStaffLoginInfo; -import com.cpop.core.base.entity.loginInfo.OamStaffLoginInfo; import com.cpop.core.base.enums.SourceType; import com.cpop.core.base.enums.UserType; import com.cpop.core.base.exception.CpopAuthenticationException; import com.cpop.core.base.table.SysUser; import com.mybatisflex.core.row.DbChain; import com.mybatisflex.core.row.Row; -import com.mybatisflex.core.row.RowUtil; +import org.springframework.stereotype.Component; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; -import java.util.stream.Collectors; /** * @author DB - * @createTime 2023/10/19 11:30 - * @description + * @createTime 2023/11/08 10:12 + * @description 商城员工登陆策略 */ -public class SysLoginInfoBuild extends AbstractLoginInfoBuild { +@Component +public class MallLoginStrategy implements LoginStrategy{ + /** + * @descriptions 获取登陆用户信息 + * @author DB + * @date 2023/11/08 9:34 + * @param sysUser 系统用户 + * @param credentials 登陆相关凭着 + * @return: com.cpop.core.base.entity.LoginUser + */ @Override - public LoginUser buildLoginUser(SysUser sysUser, Map credentials) { - switch (UserType.valueOf(sysUser.getUserType())) { - case OAM_USER: - return getOamStaffLoginInfo(sysUser); - case MALL_USER: - return getMallStaffLoginInfo(sysUser); - default: - return null; - } - } - - /** - * @descriptions Oam员工信息 - * @author DB - * @date 2023/10/19 13:21 - * @param sysUser 系统用户 - * @return: com.cpop.core.base.entity.loginInfo.OamStaffLoginInfo - */ - private LoginUser getOamStaffLoginInfo(SysUser sysUser) { - //构建登陆员工信息 - OamStaffLoginInfo staffLoginInfo = BeanUtils.mapToClass(sysUser, OamStaffLoginInfo.class); - staffLoginInfo.setUserId(sysUser.getId()); - //员工 - if (!staffLoginInfo.getUserName().equals(Constants.SUPER_ADMIN)) { - Row row = DbChain.table("cp_oam_staff") - .select() - .where("user_id = ?", staffLoginInfo.getUserId()) - .one(); - staffLoginInfo.setDeptId(row.getString("deptId")); - staffLoginInfo.setRoleId(row.getString("roleId")); - staffLoginInfo.setName(row.getString("name")); - staffLoginInfo.setStaffType(row.getInt("staffType")); - staffLoginInfo.setId(row.getString("id")); - }else { - staffLoginInfo.setName(Constants.SUPER_ADMIN); - staffLoginInfo.setId(Constants.SUPER_ADMIN); - } - return new LoginUser(staffLoginInfo, staffLoginInfo.getId(), getPermissionSet(sysUser.getUserName(), staffLoginInfo.getRoleId())); - } - - /** - * @descriptions Mall员工信息 - * @author DB - * @date 2023/10/19 13:21 - * @param sysUser 系统用户 - * @return: com.cpop.core.base.entity.loginInfo.OamStaffLoginInfo - */ - private LoginUser getMallStaffLoginInfo(SysUser sysUser) { + public LoginUser getLoginUserInfo(SysUser sysUser, Map credentials) { //构建登陆员工信息 MallStaffLoginInfo staffLoginInfo = BeanUtils.mapToClass(sysUser, MallStaffLoginInfo.class); staffLoginInfo.setUserId(sysUser.getId()); @@ -100,13 +57,24 @@ public class SysLoginInfoBuild extends AbstractLoginInfoBuild { staffLoginInfo.setSourceType(SourceType.valueOf(row.getString("sourceType"))); } else { //超级管理员 - staffLoginInfo.setId("1") - .setName(Constants.SUPER_ADMIN) - .setSourceType(SourceType.COMMON); + staffLoginInfo.setId(Constants.SUPER_ADMIN_ID).setName(Constants.SUPER_ADMIN).setSourceType(SourceType.COMMON); } - return new LoginUser(staffLoginInfo, staffLoginInfo.getId(), getPermissionSet(sysUser.getUserName(), staffLoginInfo.getRoleId())); + LoginUser loginUser = new LoginUser(staffLoginInfo, staffLoginInfo.getId(), getPermissionSet(sysUser.getUserName(), staffLoginInfo.getRoleId())); + loginUser.setUserType(UserType.MALL_USER) + .setUserName(sysUser.getUserName()) + .setLoginTime(System.currentTimeMillis()) + .setStatus(sysUser.getStatus()); + return loginUser; } + /** + * @descriptions 获取权限列表 + * @author DB + * @date 2023/11/08 9:37 + * @param username 用户名 + * @param roleId 角色id + * @return: java.util.Set + */ private Set getPermissionSet(String username, String roleId) { //获取权限 Set permissionSet = new HashSet<>(); diff --git a/Cpop-Core/src/main/java/com/cpop/core/gateway/miniProgram/MiniUserLoginInfoBuild.java b/Cpop-Core/src/main/java/com/cpop/core/strategy/login/MiniLoginStrategy.java similarity index 84% rename from Cpop-Core/src/main/java/com/cpop/core/gateway/miniProgram/MiniUserLoginInfoBuild.java rename to Cpop-Core/src/main/java/com/cpop/core/strategy/login/MiniLoginStrategy.java index f9b1456..f2a98d6 100644 --- a/Cpop-Core/src/main/java/com/cpop/core/gateway/miniProgram/MiniUserLoginInfoBuild.java +++ b/Cpop-Core/src/main/java/com/cpop/core/strategy/login/MiniLoginStrategy.java @@ -1,44 +1,41 @@ -package com.cpop.core.gateway.miniProgram; +package com.cpop.core.strategy.login; -import com.cpop.common.constant.Constants; import com.cpop.common.utils.bean.BeanUtils; -import com.cpop.core.abstracts.AbstractLoginInfoBuild; import com.cpop.core.base.entity.LoginUser; import com.cpop.core.base.entity.loginInfo.MiniUserLoginInfo; import com.cpop.core.base.enums.SourceType; -import com.cpop.core.base.exception.CpopAuthenticationException; +import com.cpop.core.base.enums.UserType; import com.cpop.core.base.exception.ServiceException; import com.cpop.core.base.table.SysUser; -import com.mybatisflex.annotation.KeyType; import com.mybatisflex.core.row.DbChain; import com.mybatisflex.core.row.Row; import com.mybatisflex.core.row.RowKey; -import lombok.Getter; +import org.springframework.stereotype.Component; import java.time.LocalDateTime; import java.util.Map; /** * @author DB - * @createTime 2023/10/24 11:09 - * @description + * @createTime 2023/11/08 10:34 + * @description 小程序登陆 */ -public class MiniUserLoginInfoBuild extends AbstractLoginInfoBuild { +@Component +public class MiniLoginStrategy implements LoginStrategy { /** - * 构建登陆小程序用户信息 - * @param user 系统用户 - * @return 登陆用户 + * @descriptions 获取登陆用户信息 + * @author DB + * @date 2023/11/08 9:34 + * @param sysUser 系统用户 + * @param credentials 登陆相关凭着 + * @return: com.cpop.core.base.entity.LoginUser */ @Override - public LoginUser buildLoginUser(SysUser user, Map credentials) { - return getMiniUserInfo(user, credentials); - } - - private LoginUser getMiniUserInfo(SysUser sysUser, Map credentials) { + public LoginUser getLoginUserInfo(SysUser sysUser, Map credentials) { MiniUserLoginInfo loginInfo = BeanUtils.mapToClass(sysUser, MiniUserLoginInfo.class); loginInfo.setUserId(sysUser.getId()); - //有用户信息 + //已经注册过有用户信息 if (credentials == null){ //构建用户信息 Row row = DbChain.table("cp_mini_user") @@ -59,7 +56,7 @@ public class MiniUserLoginInfoBuild extends AbstractLoginInfoBuild { SourceType sourceType; //校验用户来源 if (credentials.get("sourceType") == null) { - sourceType = SourceType.COMMON; + loginInfo.setSourceType(SourceType.COMMON); } else if (SourceType.JAMBOX == SourceType.valueOf(credentials.get("sourceType").toString())){ //来源自果酱 sourceType = SourceType.JAMBOX; @@ -67,10 +64,16 @@ public class MiniUserLoginInfoBuild extends AbstractLoginInfoBuild { changeJamboxUser(loginInfo, credentials ,sourceType); } else { //先用通用 - sourceType = SourceType.COMMON; + loginInfo.setSourceType(SourceType.COMMON); } } - return new LoginUser(loginInfo, loginInfo.getId(), null); + LoginUser loginUser = new LoginUser(loginInfo, loginInfo.getId(), null); + loginUser.setUserId(sysUser.getId()) + .setUserType(UserType.MINI_USER) + .setUserName(sysUser.getUserName()) + .setLoginTime(System.currentTimeMillis()) + .setStatus(sysUser.getStatus()); + return loginUser; } /** @@ -145,5 +148,4 @@ public class MiniUserLoginInfoBuild extends AbstractLoginInfoBuild { .setSourceType(sourceType); } } - } diff --git a/Cpop-Core/src/main/java/com/cpop/core/strategy/login/OamLoginStrategy.java b/Cpop-Core/src/main/java/com/cpop/core/strategy/login/OamLoginStrategy.java new file mode 100644 index 0000000..481c8d8 --- /dev/null +++ b/Cpop-Core/src/main/java/com/cpop/core/strategy/login/OamLoginStrategy.java @@ -0,0 +1,96 @@ +package com.cpop.core.strategy.login; + +import com.cpop.common.constant.Constants; +import com.cpop.common.utils.bean.BeanUtils; +import com.cpop.core.base.entity.LoginUser; +import com.cpop.core.base.entity.Permission; +import com.cpop.core.base.entity.loginInfo.OamStaffLoginInfo; +import com.cpop.core.base.enums.UserType; +import com.cpop.core.base.table.SysUser; +import com.mybatisflex.core.row.DbChain; +import com.mybatisflex.core.row.Row; +import com.mybatisflex.core.row.RowUtil; +import org.springframework.stereotype.Component; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * @author DB + * @createTime 2023/11/08 9:35 + * @description Oam登陆策略 + */ +@Component +public class OamLoginStrategy implements LoginStrategy{ + + /** + * @descriptions 获取登陆用户信息 + * @author DB + * @date 2023/11/08 9:34 + * @param sysUser 系统用户 + * @param credentials 登陆相关凭着 + * @return: com.cpop.core.base.entity.LoginUser + */ + @Override + public LoginUser getLoginUserInfo(SysUser sysUser, Map credentials) { + //构建登陆员工信息 + OamStaffLoginInfo staffLoginInfo = BeanUtils.mapToClass(sysUser, OamStaffLoginInfo.class); + staffLoginInfo.setUserId(sysUser.getId()); + //员工不是超级管理员 + if (!staffLoginInfo.getUserName().equals(Constants.SUPER_ADMIN)) { + Row row = DbChain.table("cp_oam_staff") + .select() + .where("user_id = ?", staffLoginInfo.getUserId()) + .one(); + staffLoginInfo.setDeptId(row.getString("deptId")); + staffLoginInfo.setRoleId(row.getString("roleId")); + staffLoginInfo.setName(row.getString("name")); + staffLoginInfo.setStaffType(row.getInt("staffType")); + staffLoginInfo.setId(row.getString("id")); + }else { + staffLoginInfo.setName(Constants.SUPER_ADMIN); + staffLoginInfo.setId(Constants.SUPER_ADMIN_ID); + } + LoginUser loginUser = new LoginUser(staffLoginInfo, staffLoginInfo.getId(), getPermissionSet(sysUser.getUserName(), staffLoginInfo.getRoleId())); + loginUser.setUserType(UserType.OAM_USER) + .setUserName(sysUser.getUserName()) + .setLoginTime(System.currentTimeMillis()) + .setStatus(sysUser.getStatus()); + return loginUser; + } + + /** + * @descriptions 获取权限列表 + * @author DB + * @date 2023/11/08 9:37 + * @param username 用户名 + * @param roleId 角色id + * @return: java.util.Set + */ + private Set getPermissionSet(String username, String roleId) { + //获取权限 + Set permissionSet = new HashSet<>(); + if (Constants.SUPER_ADMIN.equals(username)) { + permissionSet.add(Constants.ALL_PERMISSION); + } else { + permissionSet.add(Constants.ALL_PERMISSION); + //查询员工信息 + List list = DbChain.table("cp_sys_menu") + .select("pom.permission") + .from("cp_sys_menu").as("pom") + .leftJoin("cp_sys_role_menu").as("porm").on("porm.menu_id = pom.id") + .where("pom.type in (1,2)") + .and("porm.role_id = ?", roleId) + .and("pom.permission is not null") + .list(); + if (!list.isEmpty()) { + List permissions = RowUtil.toEntityList(list, Permission.class); + permissionSet = permissions.stream().map(Permission::getPermission).collect(Collectors.toSet()); + } + } + return permissionSet; + } +} diff --git a/Cpop-Mall/Cpop-Mall-Web/pom.xml b/Cpop-Mall/Cpop-Mall-Web/pom.xml index cdf923b..61ce281 100644 --- a/Cpop-Mall/Cpop-Mall-Web/pom.xml +++ b/Cpop-Mall/Cpop-Mall-Web/pom.xml @@ -12,6 +12,7 @@ Cpop-Mall-Web Cpop-Mall-Web jar + 1.1.0 diff --git a/Cpop-Mall/src/main/java/com/cpop/mall/business/entity/Staff.java b/Cpop-Mall/src/main/java/com/cpop/mall/business/entity/Staff.java index 879cf72..d769ebd 100644 --- a/Cpop-Mall/src/main/java/com/cpop/mall/business/entity/Staff.java +++ b/Cpop-Mall/src/main/java/com/cpop/mall/business/entity/Staff.java @@ -3,15 +3,11 @@ package com.cpop.mall.business.entity; import com.cpop.core.base.entity.BaseEntity; import com.cpop.core.base.entity.BaseInsertListener; import com.cpop.core.base.entity.BaseUpdateListener; -import com.cpop.core.base.enums.SourceType; -import com.cpop.core.gateway.miniProgram.MiniUserLoginInfoBuild; import com.mybatisflex.annotation.Column; import com.mybatisflex.annotation.Id; import com.mybatisflex.annotation.Table; import java.io.Serializable; -import java.time.LocalDateTime; -import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiModelProperty; + import lombok.*; import lombok.experimental.Accessors;