app权限适配2
This commit is contained in:
@ -169,15 +169,13 @@ public class AuthController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 3. 验证密码
|
// 3. 验证密码
|
||||||
// String enPassword = passwordEncoder.encode(authUser.getPassword());
|
if (!appUser.getPassword().equals(authUser.getPassword())) {
|
||||||
String enPassword = MD5.create().digestHex(authUser.getPassword());
|
|
||||||
if (!appUser.getPassword().equals(enPassword)) {
|
|
||||||
throw new BadRequestException("登录密码错误");
|
throw new BadRequestException("登录密码错误");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 4. 加载用户详情
|
// 4. 加载用户详情
|
||||||
JwtUserDto jwtUser = userDetailsService.loadAppUserByUsername(appUser.getUsername());
|
JwtUserDto jwtUser = userDetailsService.loadUserByAppUsername(appUser.getUsername());
|
||||||
|
|
||||||
// 5. 创建认证信息
|
// 5. 创建认证信息
|
||||||
Authentication authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities());
|
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.JWT;
|
||||||
import cn.hutool.jwt.JWTUtil;
|
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.exception.BadRequestException;
|
||||||
import com.fuyuanshen.modules.security.service.dto.AuthorityDto;
|
import com.fuyuanshen.modules.security.service.dto.AuthorityDto;
|
||||||
import com.fuyuanshen.modules.security.service.dto.JwtUserDto;
|
import com.fuyuanshen.modules.security.service.dto.JwtUserDto;
|
||||||
import com.fuyuanshen.modules.system.domain.User;
|
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.DataService;
|
||||||
import com.fuyuanshen.modules.system.service.RoleService;
|
import com.fuyuanshen.modules.system.service.RoleService;
|
||||||
import com.fuyuanshen.modules.system.service.UserService;
|
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.security.core.userdetails.UserDetailsService;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@ -48,32 +47,30 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|||||||
private final DataService dataService;
|
private final DataService dataService;
|
||||||
private final UserCacheManager userCacheManager;
|
private final UserCacheManager userCacheManager;
|
||||||
|
|
||||||
|
private final static String APP_USER_TYPE = "1"; // app用户类型
|
||||||
|
|
||||||
|
private final static String SYSTEM_USER_TYPE = "0"; // 系统用户类型
|
||||||
@Override
|
@Override
|
||||||
public JwtUserDto loadUserByUsername(String username) {
|
public JwtUserDto loadUserByUsername(String username) {
|
||||||
|
if(SecurityUtils.getToken() != null){
|
||||||
JWT jwt = JWTUtil.parseToken(SecurityUtils.getToken());
|
JWT jwt = JWTUtil.parseToken(SecurityUtils.getToken());
|
||||||
String userType = jwt.getPayload("userType").toString();
|
String userType = jwt.getPayload("userType").toString();
|
||||||
|
if(APP_USER_TYPE.equals(userType)){
|
||||||
if("1".equals(userType)){
|
return loadUserByAppUsername(username);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return jwtUserDto;
|
|
||||||
}else{
|
}else{
|
||||||
|
return loadSystemUserByUsername(username);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
return loadSystemUserByUsername(username);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加载系统用户详情信息
|
||||||
|
* @param username
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private JwtUserDto loadSystemUserByUsername(String username) {
|
||||||
JwtUserDto jwtUserDto = userCacheManager.getUserCache(username);
|
JwtUserDto jwtUserDto = userCacheManager.getUserCache(username);
|
||||||
if (jwtUserDto == null) {
|
if (jwtUserDto == null) {
|
||||||
User user = userService.getLoginData(username);
|
User user = userService.getLoginData(username);
|
||||||
@ -93,13 +90,16 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|||||||
}
|
}
|
||||||
return jwtUserDto;
|
return jwtUserDto;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public JwtUserDto loadAppUserByUsername(String username) {
|
/**
|
||||||
|
* 加载app用户详情信息
|
||||||
|
* @param username
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public JwtUserDto loadUserByAppUsername(String username) {
|
||||||
|
|
||||||
JwtUserDto jwtUserDto = userCacheManager.getUserCache(username);
|
JwtUserDto jwtUserDto = userCacheManager.getAppUserCache(username);
|
||||||
if (jwtUserDto == null) {
|
if (jwtUserDto == null) {
|
||||||
username = username.replace("APP_", "");
|
|
||||||
APPUser user = userService.appGetLoginData(username);
|
APPUser user = userService.appGetLoginData(username);
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
throw new BadRequestException("用户不存在");
|
throw new BadRequestException("用户不存在");
|
||||||
@ -118,9 +118,5 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|||||||
return jwtUserDto;
|
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));
|
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用户注册")
|
@ApiOperation("app用户注册")
|
||||||
@AnonymousPostMapping(value = "/app/register")
|
@AnonymousPostMapping(value = "/register")
|
||||||
public ResponseVO<String> APPRegister(@Validated @RequestBody APPUserDTO user) throws Exception {
|
public ResponseVO<String> APPRegister(@Validated @RequestBody APPUserDTO user) throws Exception {
|
||||||
|
|
||||||
//暫定0000
|
//暫定0000
|
||||||
|
@ -76,9 +76,7 @@ public class APPUserServiceImpl extends ServiceImpl<APPUserMapper, APPUser> impl
|
|||||||
APPUser appUser = new APPUser();
|
APPUser appUser = new APPUser();
|
||||||
appUser.setUsername(user.getPhoneNumber());
|
appUser.setUsername(user.getPhoneNumber());
|
||||||
|
|
||||||
// String enPassword = passwordEncoder.encode(user.getPassword());
|
appUser.setPassword(user.getPassword());
|
||||||
String enPassword = MD5.create().digestHex(user.getPassword());
|
|
||||||
appUser.setPassword(enPassword);
|
|
||||||
appUser.setNickName(user.getPhoneNumber());
|
appUser.setNickName(user.getPhoneNumber());
|
||||||
appUser.setUserLevel((byte) 1);
|
appUser.setUserLevel((byte) 1);
|
||||||
appUser.setPhone(Long.valueOf(user.getPhoneNumber()));
|
appUser.setPhone(Long.valueOf(user.getPhoneNumber()));
|
||||||
|
Reference in New Issue
Block a user