Commit cfb9c5a6 authored by fangxinjiang's avatar fangxinjiang

B端&LDAP登录调整

parent a65eaa6d
...@@ -116,7 +116,8 @@ public class YifuTokenEndpoint { ...@@ -116,7 +116,8 @@ public class YifuTokenEndpoint {
user.getPhone(), true, true, true, user.getPhone(), true, true, true,
CommonConstants.STATUS_NORMAL.equals(user.getLockFlag()), CommonConstants.STATUS_NORMAL.equals(user.getLockFlag()),
user.getUserGroup(),authorities, user.getLdapDn(), user.getUserGroup(),authorities, user.getLdapDn(),
info.getClientRoleMap(),info.getSettleIdList()); info.getClientRoleMap(),info.getSettleIdList(),
user.getType());
thisUser.setClientRoleMap(info.getClientRoleMap()); thisUser.setClientRoleMap(info.getClientRoleMap());
return thisUser; return thisUser;
} }
......
...@@ -92,13 +92,19 @@ public class YifuUser extends User { ...@@ -92,13 +92,19 @@ public class YifuUser extends User {
@Getter @Getter
@Setter @Setter
private List<String> settleIdList; private List<String> settleIdList;
/**
* @Author fxj
* @Description 员工类型
**/
@Getter
private String type;
public YifuUser(String id, Long deptId,String deptName, String username, String nickname,String systemFlag, public YifuUser(String id, Long deptId,String deptName, String username, String nickname,String systemFlag,
String password, String phone, boolean enabled, boolean accountNonExpired, String password, String phone, boolean enabled, boolean accountNonExpired,
boolean credentialsNonExpired, boolean accountNonLocked, boolean credentialsNonExpired, boolean accountNonLocked,
String userGroup, Collection<? extends GrantedAuthority> authorities, String ldapDn, String userGroup, Collection<? extends GrantedAuthority> authorities, String ldapDn,
Map<String, List<Long>> clientRoleMap, Map<String, List<Long>> clientRoleMap,
List<String> settleIdList) { List<String> settleIdList,String type) {
super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities); super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
this.id = id; this.id = id;
this.deptId = deptId; this.deptId = deptId;
...@@ -110,5 +116,6 @@ public class YifuUser extends User { ...@@ -110,5 +116,6 @@ public class YifuUser extends User {
this.ldapDn = ldapDn; this.ldapDn = ldapDn;
this.clientRoleMap=clientRoleMap; this.clientRoleMap=clientRoleMap;
this.settleIdList=settleIdList; this.settleIdList=settleIdList;
this.type=type;
} }
} }
...@@ -97,7 +97,8 @@ public class YifuDaoAuthenticationProvider extends AbstractUserDetailsAuthentica ...@@ -97,7 +97,8 @@ public class YifuDaoAuthenticationProvider extends AbstractUserDetailsAuthentica
} }
try { try {
UserDetails loadedUser = optional.get().loadUserByUsername(username); //接收到前端传的密码
UserDetails loadedUser = optional.get().loadUserByUsernameDiy(username,(String)authentication.getCredentials());
if (loadedUser == null) { if (loadedUser == null) {
throw new InternalAuthenticationServiceException( throw new InternalAuthenticationServiceException(
"UserDetailsService returned null, which is an interface contract violation"); "UserDetailsService returned null, which is an interface contract violation");
......
...@@ -14,6 +14,8 @@ import org.springframework.security.core.authority.AuthorityUtils; ...@@ -14,6 +14,8 @@ import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
...@@ -25,7 +27,6 @@ import java.util.Set; ...@@ -25,7 +27,6 @@ import java.util.Set;
* @date 2021/12/21 * @date 2021/12/21
*/ */
public interface YifuUserDetailsService extends UserDetailsService, Ordered { public interface YifuUserDetailsService extends UserDetailsService, Ordered {
/** /**
* 是否支持此客户端校验 * 是否支持此客户端校验
* @param clientId 目标客户端 * @param clientId 目标客户端
...@@ -52,7 +53,6 @@ public interface YifuUserDetailsService extends UserDetailsService, Ordered { ...@@ -52,7 +53,6 @@ public interface YifuUserDetailsService extends UserDetailsService, Ordered {
if (result == null || result.getData() == null) { if (result == null || result.getData() == null) {
throw new UsernameNotFoundException("用户不存在"); throw new UsernameNotFoundException("用户不存在");
} }
UserInfo info = result.getData(); UserInfo info = result.getData();
Set<String> dbAuthsSet = new HashSet<>(); Set<String> dbAuthsSet = new HashSet<>();
...@@ -63,17 +63,16 @@ public interface YifuUserDetailsService extends UserDetailsService, Ordered { ...@@ -63,17 +63,16 @@ public interface YifuUserDetailsService extends UserDetailsService, Ordered {
dbAuthsSet.addAll(Arrays.asList(info.getPermissions())); dbAuthsSet.addAll(Arrays.asList(info.getPermissions()));
} }
Collection<? extends GrantedAuthority> authorities = AuthorityUtils Collection<? extends GrantedAuthority> authorities = AuthorityUtils
.createAuthorityList(dbAuthsSet.toArray(new String[0])); .createAuthorityList(dbAuthsSet.toArray(new String[0]));
SysUser user = info.getSysUser(); SysUser user = info.getSysUser();
// 构造security用户 // 构造security用户
return new YifuUser(user.getUserId(), user.getDeptId(),user.getDeptName(), user.getUsername(), return new YifuUser(user.getUserId(), user.getDeptId(),user.getDeptName(), user.getUsername(),
user.getNickname(),user.getSystemFlag(), SecurityConstants.BCRYPT + user.getPassword(), user.getNickname(),user.getSystemFlag(), SecurityConstants.BCRYPT + user.getPassword(),
user.getPhone(), true, true, true, user.getPhone(), true, true, true,
StrUtil.equals(user.getLockFlag(), CommonConstants.STATUS_NORMAL), StrUtil.equals(user.getLockFlag(), CommonConstants.STATUS_NORMAL),
user.getUserGroup(),authorities, user.getLdapDn(),info.getClientRoleMap(),info.getSettleIdList()); user.getUserGroup(),authorities, user.getLdapDn(),info.getClientRoleMap(),
info.getSettleIdList(),user.getType());
} }
/** /**
...@@ -85,4 +84,11 @@ public interface YifuUserDetailsService extends UserDetailsService, Ordered { ...@@ -85,4 +84,11 @@ public interface YifuUserDetailsService extends UserDetailsService, Ordered {
return this.loadUserByUsername(yifuUser.getUsername()); return this.loadUserByUsername(yifuUser.getUsername());
} }
/**
* 自定义获取用户信息 兼容 LDAP 或 EKP
*/
default UserDetails loadUserByUsernameDiy(String username,String password) {
return this.loadUserByUsernameDiy(username,password);
}
} }
...@@ -67,7 +67,25 @@ public class YifuUserDetailsServiceImpl implements YifuUserDetailsService { ...@@ -67,7 +67,25 @@ public class YifuUserDetailsServiceImpl implements YifuUserDetailsService {
} }
return userDetails; return userDetails;
} }
/**
* 用户名密码登录
* @param username 用户名
* @return
*/
@Override
@SneakyThrows
public UserDetails loadUserByUsernameDiy(String username,String password) {
Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS);
if (cache != null && cache.get(username) != null) {
return (YifuUser) cache.get(username).get();
}
R<UserInfo> result = HttpDaprUtil.invokeMethodGet(daprUpmsProperties.getAppUrl(),daprUpmsProperties.getAppId(), "/user/getInfoByUsername", "?username="+username+"&password="+password, UserInfo.class, SecurityConstants.FROM_IN);
UserDetails userDetails = getUserDetails(result);
if (cache != null) {
cache.put(username, userDetails);
}
return userDetails;
}
@Override @Override
public int getOrder() { public int getOrder() {
return Integer.MIN_VALUE; return Integer.MIN_VALUE;
......
# 数据源配置 # 数据源配置
spring: spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
redis: redis:
host: 127.0.0.1 host: 127.0.0.1
password: '@yf_2017' password: '@yf_2017'
......
# 数据源配置 # 数据源配置
spring: spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
redis: redis:
host: 192.168.1.65 host: 192.168.1.65
port: 22379 port: 22379
......
...@@ -145,4 +145,11 @@ public class SysUser extends BaseEntity { ...@@ -145,4 +145,11 @@ public class SysUser extends BaseEntity {
@Schema(description = "LDAP_DN") @Schema(description = "LDAP_DN")
private String ldapDn; private String ldapDn;
/**
* @Author fxj
* @Description 0 系统用户 1 LDAP用户 2 B端用户 3.微信用户 默认系统用户
* @Date 14:56 2023/1/6
**/
private String type;
} }
...@@ -134,4 +134,11 @@ public class UserVO implements Serializable { ...@@ -134,4 +134,11 @@ public class UserVO implements Serializable {
*/ */
@Schema(description = "LDAP_DN") @Schema(description = "LDAP_DN")
private String ldapDn; private String ldapDn;
/**
* @Author fxj
* @Description 0 系统用户 1 LDAP用户 2 B端用户 3.微信用户 默认系统用户
* @Date 14:56 2023/1/6
**/
private String type;
} }
...@@ -56,6 +56,7 @@ import java.util.Set; ...@@ -56,6 +56,7 @@ import java.util.Set;
* @date 2019/2/1 * @date 2019/2/1
*/ */
@Tag(name = "用户管理模块", description = "用户管理") @Tag(name = "用户管理模块", description = "用户管理")
@Log4j2
@RestController @RestController
@RequiredArgsConstructor @RequiredArgsConstructor
@RequestMapping("/user") @RequestMapping("/user")
...@@ -101,10 +102,16 @@ public class UserController { ...@@ -101,10 +102,16 @@ public class UserController {
} }
return R.ok(userService.getUserInfo(user)); return R.ok(userService.getUserInfo(user));
} }
@SysLog("登录获取账号信息异常")
@Inner @Inner
@GetMapping("/getInfoByUsername") @GetMapping("/getInfoByUsername")
public UserInfo infoAPI(@RequestParam(required = true, name = "username") String username) { public UserInfo infoAPI(@RequestParam(required = true, name = "username") String username,
@RequestParam(required = false, name = "password") String password) {
SysUser user = userService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUsername, username)); SysUser user = userService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUsername, username));
if (Common.isNotNull(user)){
userService.ldapLogin(username,password,user);
}
return userService.getUserInfo(user); return userService.getUserInfo(user);
} }
......
...@@ -169,5 +169,13 @@ public interface SysUserService extends IService<SysUser> { ...@@ -169,5 +169,13 @@ public interface SysUserService extends IService<SysUser> {
* @return * @return
*/ */
Boolean updateLockFlagById(SysUser user, String lockFlag); Boolean updateLockFlagById(SysUser user, String lockFlag);
/**
* @Author fxj
* @Description LDAP用户登录处理
* @Date 14:51 2023/1/9
* @Param
* @return
**/
void ldapLogin(String username,String password,SysUser user);
} }
...@@ -42,10 +42,7 @@ import com.yifu.cloud.plus.v1.yifu.common.core.constant.CacheConstants; ...@@ -42,10 +42,7 @@ import com.yifu.cloud.plus.v1.yifu.common.core.constant.CacheConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants; import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.enums.MenuTypeEnum; import com.yifu.cloud.plus.v1.yifu.common.core.constant.enums.MenuTypeEnum;
import com.yifu.cloud.plus.v1.yifu.common.core.exception.ErrorCodes; import com.yifu.cloud.plus.v1.yifu.common.core.exception.ErrorCodes;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common; import com.yifu.cloud.plus.v1.yifu.common.core.util.*;
import com.yifu.cloud.plus.v1.yifu.common.core.util.MsgUtils;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.core.util.RedisUtil;
import com.yifu.cloud.plus.v1.yifu.common.dapr.util.ArchivesDaprUtil; import com.yifu.cloud.plus.v1.yifu.common.dapr.util.ArchivesDaprUtil;
import com.yifu.cloud.plus.v1.yifu.common.ldap.entity.PersonVo; import com.yifu.cloud.plus.v1.yifu.common.ldap.entity.PersonVo;
import com.yifu.cloud.plus.v1.yifu.common.ldap.util.LdapUtil; import com.yifu.cloud.plus.v1.yifu.common.ldap.util.LdapUtil;
...@@ -134,6 +131,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl ...@@ -134,6 +131,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
*/ */
@Override @Override
public UserInfo getUserInfo(SysUser sysUser) { public UserInfo getUserInfo(SysUser sysUser) {
// LDAP用户处理
UserInfo userInfo = new UserInfo(); UserInfo userInfo = new UserInfo();
userInfo.setSysUser(sysUser); userInfo.setSysUser(sysUser);
// 设置角色列表 // 设置角色列表
...@@ -171,7 +169,26 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl ...@@ -171,7 +169,26 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
userInfo.setClientRoleMap(clientRoleMap); userInfo.setClientRoleMap(clientRoleMap);
return userInfo; return userInfo;
} }
/**
* @Author fxj
* @Description LDAP登录验证
* @Date 15:01 2023/1/9
**/
public void ldapLogin(String username,String password,SysUser user){
// 如果是LDAP用户 登录LDAP验证并重置密码为用户的正确密码
if (Common.isNotNull(user) && CommonConstants.ONE_STRING.equals(user.getType())){
try {
if (ldapUtil.authenticate(username,password)){
user.setPassword(ENCODER.encode(password));
}else {
// 登录失败,随机密码,保证用户密码不一致
user.setPassword(password+RandomUtil.getSix());
}
} catch (Exception e) {
log.error("LDAP登录异常",e);
}
}
}
/** /**
* 分页查询用户信息(含有角色信息) * 分页查询用户信息(含有角色信息)
* @param page 分页对象 * @param page 分页对象
......
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
<result column="post" property="post"/> <result column="post" property="post"/>
<result column="email" property="email"/> <result column="email" property="email"/>
<result column="system_flag" property="systemFlag"/> <result column="system_flag" property="systemFlag"/>
<result column="type" property="type"/>
<collection property="roleList" ofType="com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysRole" <collection property="roleList" ofType="com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysRole"
select="com.yifu.cloud.plus.v1.yifu.admin.mapper.SysRoleMapper.listRolesByUserId" column="user_id"> select="com.yifu.cloud.plus.v1.yifu.admin.mapper.SysRoleMapper.listRolesByUserId" column="user_id">
</collection> </collection>
...@@ -60,6 +61,7 @@ ...@@ -60,6 +61,7 @@
<result column="post" property="post"/> <result column="post" property="post"/>
<result column="email" property="email"/> <result column="email" property="email"/>
<result column="system_flag" property="systemFlag"/> <result column="system_flag" property="systemFlag"/>
<result column="type" property="type"/>
<collection property="roleList" ofType="com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysRole"> <collection property="roleList" ofType="com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysRole">
<id column="role_id" property="roleId"/> <id column="role_id" property="roleId"/>
<result column="role_name" property="roleName"/> <result column="role_name" property="roleName"/>
...@@ -158,6 +160,7 @@ ...@@ -158,6 +160,7 @@
sys_user.post, sys_user.post,
sys_user.email, sys_user.email,
sys_user.system_flag, sys_user.system_flag,
sys_user.type,
sys_dept.name AS deptName sys_dept.name AS deptName
FROM sys_user FROM sys_user
LEFT JOIN sys_dept ON sys_dept.dept_id = sys_user.dept_id LEFT JOIN sys_dept ON sys_dept.dept_id = sys_user.dept_id
...@@ -192,6 +195,7 @@ ...@@ -192,6 +195,7 @@
u.user_group, u.user_group,
u.email, u.email,
u.system_flag, u.system_flag,
u.type
d.name AS deptName d.name AS deptName
FROM sys_user u FROM sys_user u
LEFT JOIN sys_dept d ON d.dept_id = u.dept_id LEFT JOIN sys_dept d ON d.dept_id = u.dept_id
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment