package com.gk.hotwork.Config.Oauth2;
|
|
import com.gk.hotwork.Domain.DepartmentInfo;
|
import com.gk.hotwork.Domain.UserInfo;
|
import com.gk.hotwork.Domain.Utils.CommonUtil;
|
import com.gk.hotwork.Domain.Utils.Constants;
|
import com.gk.hotwork.Service.DepartmentService;
|
import com.gk.hotwork.Service.RoleService;
|
import com.gk.hotwork.Service.UserService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
import org.springframework.stereotype.Service;
|
|
import java.util.List;
|
import java.util.Optional;
|
|
/**
|
* 加载用户特定数据的核心接口(Core interface which loads user-specific data.)
|
*
|
* @author zhangby
|
* @date 2019-05-14 09:49
|
*/
|
@Service
|
public class UserServiceDetail implements UserDetailsService {
|
|
@Autowired
|
UserService userService;
|
@Autowired
|
RoleService roleService;
|
@Autowired
|
DepartmentService departmentService;
|
|
@Value("${workname}")
|
private String workname;
|
/**
|
* 根据用户名查询用户
|
*/
|
@Override
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
String authType = System.getProperty(Constants.AUTH_TYPE);
|
OauthUser oauthUser = new OauthUser();
|
//查询登录用户
|
UserInfo userInfo = userService.selectUserVoByName(workname,username);
|
Optional.of(userInfo).ifPresent(us ->{
|
//查询角色
|
List<OauthRole> roleByUser = roleService.selectOauthRoleByUser(us.getId().intValue());
|
//数据类型转换
|
List<OauthRole> oauthRoles = CommonUtil
|
.convers(roleByUser, role -> new OauthRole(
|
role.getId(),
|
role.getAuthority(),null,null)
|
);
|
//数据结果集封装
|
oauthUser.setId(us.getId().toString());
|
oauthUser.setPassword(us.getPassword());
|
oauthUser.setUsername(us.getRealname());
|
oauthUser.setOauthRoles(oauthRoles);
|
oauthUser.setDepartment(us.getDepartment());
|
if (us.getDepartment() != null) {
|
DepartmentInfo dep = departmentService.getById(us.getId());
|
if (dep != null) {
|
oauthUser.setDepartmentName(dep.getDepartment());
|
}
|
|
}
|
oauthUser.setCompany(us.getCompany());
|
oauthUser.setUserType(us.getType());
|
oauthUser.setIdcard(us.getIdcard());
|
});
|
//判断登录入口
|
return oauthUser;
|
}
|
}
|