|
|
|
@ -1,10 +1,11 @@
|
|
|
|
|
package com.supervision.ai.service.hub.service.impl;
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
import com.supervision.ai.service.hub.domain.SysApp;
|
|
|
|
|
import com.supervision.ai.service.hub.domain.SysUser;
|
|
|
|
|
import com.supervision.ai.service.hub.mapper.SysUserMapper;
|
|
|
|
|
import com.supervision.ai.service.hub.service.SysAppService;
|
|
|
|
|
import com.supervision.ai.service.hub.service.SysUserService;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.security.authentication.DisabledException;
|
|
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
|
|
|
import org.springframework.security.core.userdetails.User;
|
|
|
|
@ -18,31 +19,35 @@ import java.util.List;
|
|
|
|
|
|
|
|
|
|
import static com.supervision.ai.service.hub.constant.UserConstant.USER_STATUS_DISABLED;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Service
|
|
|
|
|
public class SysUserService extends ServiceImpl<SysUserMapper, SysUser> implements UserDetailsService {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据用户名加载用户信息
|
|
|
|
|
* Spring Security 会调用该方法来获取用户信息
|
|
|
|
|
*
|
|
|
|
|
* @param username 用户名
|
|
|
|
|
* @return UserDetails
|
|
|
|
|
* @throws UsernameNotFoundException 用户名未找到异常
|
|
|
|
|
*/
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class UserDetailsServiceImpl implements UserDetailsService {
|
|
|
|
|
|
|
|
|
|
private final SysUserService sysUserService;
|
|
|
|
|
|
|
|
|
|
private final SysAppService sysAppService;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
|
|
|
|
SysUser user = this.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserName, username));
|
|
|
|
|
if (user == null) {
|
|
|
|
|
throw new UsernameNotFoundException("用户不存在: " + username);
|
|
|
|
|
}
|
|
|
|
|
if (USER_STATUS_DISABLED.equals(user.getStatus())) {
|
|
|
|
|
throw new DisabledException("用户已被禁用");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 由于sys_app表数据较少,所以优先从sys_app表中查询
|
|
|
|
|
SysApp sysApp = sysAppService.getByAppName(username);
|
|
|
|
|
|
|
|
|
|
// 将查询到的用户信息组装成UserDetails对象
|
|
|
|
|
// **扩展点**:如需加载用户角色权限,可在此处查询 sys_user_role 表关联的角色,并将角色加入 authorities 列表
|
|
|
|
|
List<GrantedAuthority> authorities = Collections.emptyList();
|
|
|
|
|
// 使用Spring Security提供的User对象作为UserDetails返回
|
|
|
|
|
return new User(user.getUserName(), user.getPassword(), authorities);
|
|
|
|
|
if (null == sysApp){
|
|
|
|
|
SysUser user = sysUserService.getByUsername(username);
|
|
|
|
|
if (user == null) {
|
|
|
|
|
throw new UsernameNotFoundException("用户不存在: " + username);
|
|
|
|
|
}
|
|
|
|
|
if (USER_STATUS_DISABLED.equals(user.getStatus())) {
|
|
|
|
|
throw new DisabledException("用户已被禁用");
|
|
|
|
|
}
|
|
|
|
|
return new User(user.getUserName(), user.getPassword(), authorities);
|
|
|
|
|
}
|
|
|
|
|
return new User(sysApp.getAppName(), sysApp.getPassword(), authorities);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|