app权限适配2
This commit is contained in:
@ -169,15 +169,13 @@ public class AuthController {
|
||||
}
|
||||
|
||||
// 3. 验证密码
|
||||
// String enPassword = passwordEncoder.encode(authUser.getPassword());
|
||||
String enPassword = MD5.create().digestHex(authUser.getPassword());
|
||||
if (!appUser.getPassword().equals(enPassword)) {
|
||||
if (!appUser.getPassword().equals(authUser.getPassword())) {
|
||||
throw new BadRequestException("登录密码错误");
|
||||
}
|
||||
|
||||
|
||||
// 4. 加载用户详情
|
||||
JwtUserDto jwtUser = userDetailsService.loadAppUserByUsername(appUser.getUsername());
|
||||
JwtUserDto jwtUser = userDetailsService.loadUserByAppUsername(appUser.getUsername());
|
||||
|
||||
// 5. 创建认证信息
|
||||
Authentication authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities());
|
||||
|
@ -17,18 +17,17 @@ package com.fuyuanshen.modules.security.service;
|
||||
|
||||
import cn.hutool.jwt.JWT;
|
||||
import cn.hutool.jwt.JWTUtil;
|
||||
import com.fuyuanshen.modules.security.service.dto.app.AppJwtUserDto;
|
||||
import com.fuyuanshen.modules.system.domain.app.APPUser;
|
||||
import com.fuyuanshen.utils.SecurityUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import com.fuyuanshen.exception.BadRequestException;
|
||||
import com.fuyuanshen.modules.security.service.dto.AuthorityDto;
|
||||
import com.fuyuanshen.modules.security.service.dto.JwtUserDto;
|
||||
import com.fuyuanshen.modules.system.domain.User;
|
||||
import com.fuyuanshen.modules.system.domain.app.APPUser;
|
||||
import com.fuyuanshen.modules.system.service.DataService;
|
||||
import com.fuyuanshen.modules.system.service.RoleService;
|
||||
import com.fuyuanshen.modules.system.service.UserService;
|
||||
import com.fuyuanshen.utils.SecurityUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -48,58 +47,59 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
||||
private final DataService dataService;
|
||||
private final UserCacheManager userCacheManager;
|
||||
|
||||
private final static String APP_USER_TYPE = "1"; // app用户类型
|
||||
|
||||
private final static String SYSTEM_USER_TYPE = "0"; // 系统用户类型
|
||||
@Override
|
||||
public JwtUserDto loadUserByUsername(String username) {
|
||||
JWT jwt = JWTUtil.parseToken(SecurityUtils.getToken());
|
||||
String userType = jwt.getPayload("userType").toString();
|
||||
|
||||
if("1".equals(userType)){
|
||||
JwtUserDto jwtUserDto = userCacheManager.getAppUserCache(username);
|
||||
if (jwtUserDto == null) {
|
||||
APPUser user = userService.appGetLoginData(username);
|
||||
if (user == null) {
|
||||
throw new BadRequestException("用户不存在");
|
||||
} else {
|
||||
if (!user.getEnabled()) {
|
||||
throw new BadRequestException("账号未激活!");
|
||||
}
|
||||
// 获取用户的权限
|
||||
List<AuthorityDto> authorities = roleService.appBuildPermissions(user);
|
||||
// 初始化JwtUserDto
|
||||
jwtUserDto = new JwtUserDto(user,null, dataService.getDeptIds(user), authorities);
|
||||
// 添加缓存数据
|
||||
userCacheManager.addAppUserCache(username, jwtUserDto);
|
||||
}
|
||||
if(SecurityUtils.getToken() != null){
|
||||
JWT jwt = JWTUtil.parseToken(SecurityUtils.getToken());
|
||||
String userType = jwt.getPayload("userType").toString();
|
||||
if(APP_USER_TYPE.equals(userType)){
|
||||
return loadUserByAppUsername(username);
|
||||
}else{
|
||||
return loadSystemUserByUsername(username);
|
||||
}
|
||||
return jwtUserDto;
|
||||
}else{
|
||||
JwtUserDto jwtUserDto = userCacheManager.getUserCache(username);
|
||||
if (jwtUserDto == null) {
|
||||
User user = userService.getLoginData(username);
|
||||
if (user == null) {
|
||||
throw new BadRequestException("用户不存在");
|
||||
} else {
|
||||
if (!user.getEnabled()) {
|
||||
throw new BadRequestException("账号未激活!");
|
||||
}
|
||||
// 获取用户的权限
|
||||
List<AuthorityDto> authorities = roleService.buildPermissions(user);
|
||||
// 初始化JwtUserDto
|
||||
jwtUserDto = new JwtUserDto(null,user, dataService.getDeptIds(user), authorities);
|
||||
// 添加缓存数据
|
||||
userCacheManager.addUserCache(username, jwtUserDto);
|
||||
}
|
||||
}
|
||||
return jwtUserDto;
|
||||
return loadSystemUserByUsername(username);
|
||||
}
|
||||
}
|
||||
|
||||
public JwtUserDto loadAppUserByUsername(String username) {
|
||||
|
||||
/**
|
||||
* 加载系统用户详情信息
|
||||
* @param username
|
||||
* @return
|
||||
*/
|
||||
private JwtUserDto loadSystemUserByUsername(String username) {
|
||||
JwtUserDto jwtUserDto = userCacheManager.getUserCache(username);
|
||||
if (jwtUserDto == null) {
|
||||
username = username.replace("APP_", "");
|
||||
User user = userService.getLoginData(username);
|
||||
if (user == null) {
|
||||
throw new BadRequestException("用户不存在");
|
||||
} else {
|
||||
if (!user.getEnabled()) {
|
||||
throw new BadRequestException("账号未激活!");
|
||||
}
|
||||
// 获取用户的权限
|
||||
List<AuthorityDto> authorities = roleService.buildPermissions(user);
|
||||
// 初始化JwtUserDto
|
||||
jwtUserDto = new JwtUserDto(null,user, dataService.getDeptIds(user), authorities);
|
||||
// 添加缓存数据
|
||||
userCacheManager.addUserCache(username, jwtUserDto);
|
||||
}
|
||||
}
|
||||
return jwtUserDto;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载app用户详情信息
|
||||
* @param username
|
||||
* @return
|
||||
*/
|
||||
public JwtUserDto loadUserByAppUsername(String username) {
|
||||
|
||||
JwtUserDto jwtUserDto = userCacheManager.getAppUserCache(username);
|
||||
if (jwtUserDto == null) {
|
||||
APPUser user = userService.appGetLoginData(username);
|
||||
if (user == null) {
|
||||
throw new BadRequestException("用户不存在");
|
||||
@ -118,9 +118,5 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
||||
return jwtUserDto;
|
||||
}
|
||||
|
||||
private boolean isAppUser(String username) {
|
||||
// 实现你的判断逻辑,比如前缀、数据库查询等
|
||||
return username.startsWith("APP_");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -87,9 +87,19 @@ public class APPUserController {
|
||||
return ResponseVO.success(appUserService.queryAPPUser(criteria, page));
|
||||
}
|
||||
|
||||
@Log("app")
|
||||
|
||||
@ApiOperation("用户中心")
|
||||
@GetMapping(value = "/get")
|
||||
@PreAuthorize("@el.check('appUser:get')")
|
||||
public ResponseVO<APPUser> getAPPUser(UserQueryCriteria criteria) {
|
||||
String userName = SecurityUtils.getCurrentUsername();
|
||||
return null;
|
||||
// return ResponseVO.success(appUserService.getAPPUser(criteria));
|
||||
}
|
||||
|
||||
@Log("app用户注册")
|
||||
@ApiOperation("app用户注册")
|
||||
@AnonymousPostMapping(value = "/app/register")
|
||||
@AnonymousPostMapping(value = "/register")
|
||||
public ResponseVO<String> APPRegister(@Validated @RequestBody APPUserDTO user) throws Exception {
|
||||
|
||||
//暫定0000
|
||||
|
@ -76,9 +76,7 @@ public class APPUserServiceImpl extends ServiceImpl<APPUserMapper, APPUser> impl
|
||||
APPUser appUser = new APPUser();
|
||||
appUser.setUsername(user.getPhoneNumber());
|
||||
|
||||
// String enPassword = passwordEncoder.encode(user.getPassword());
|
||||
String enPassword = MD5.create().digestHex(user.getPassword());
|
||||
appUser.setPassword(enPassword);
|
||||
appUser.setPassword(user.getPassword());
|
||||
appUser.setNickName(user.getPhoneNumber());
|
||||
appUser.setUserLevel((byte) 1);
|
||||
appUser.setPhone(Long.valueOf(user.getPhoneNumber()));
|
||||
|
Reference in New Issue
Block a user