三端登录
This commit is contained in:
parent
80c6699006
commit
682922ae6e
@ -75,25 +75,26 @@ public class SecurityUtils {
|
||||
userDetailsService = SpringBeanHolder.getBean("bUserDetailsService", UserDetailsService.class);
|
||||
} else if (UserTypeEnum.ADMIN.equals(UserTypeEnum.valueOf(userType))){
|
||||
userDetailsService = SpringBeanHolder.getBean("userDetailsService", UserDetailsService.class);
|
||||
} else {
|
||||
} else if (UserTypeEnum.EMPLOYEES.equals(UserTypeEnum.valueOf(userType))) {
|
||||
userDetailsService = SpringBeanHolder.getBean("aUserDetailsService", UserDetailsService.class);
|
||||
}else {
|
||||
throw new RuntimeException("用户类型查询失败!");
|
||||
}
|
||||
return userDetailsService.loadUserByUsername(getCurrentUsername());
|
||||
}
|
||||
|
||||
// 添加获取当前用户类型的方法
|
||||
public static String getCurrentUserTypeVo() {
|
||||
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
public static UserTypeEnum getCurrentUserTypeVo() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication == null) {
|
||||
throw new BadRequestException("当前登录状态过期");
|
||||
}
|
||||
|
||||
// 从认证信息中获取用户类型(在登录时设置)
|
||||
if (authentication.getDetails() instanceof Map) {
|
||||
Map<?, ?> details = (Map<?, ?>) authentication.getDetails();
|
||||
return (String) details.get("userType");
|
||||
return UserTypeEnum.getByValue((Integer) details.get("userType"));
|
||||
}
|
||||
return "ADMIN"; // 默认类型
|
||||
return UserTypeEnum.ADMIN; // 默认类型
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -98,13 +98,11 @@ public class AuthController {
|
||||
if (!passwordEncoder.matches(password, jwtUser.getPassword())) {
|
||||
throw new BadRequestException("登录密码错误");
|
||||
}
|
||||
Map<String, String> details = new HashMap<>();
|
||||
details.put("userType", String.valueOf(jwtUser.getUserType().getValue()));
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities());
|
||||
authentication.setDetails(details);
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
// 生成令牌
|
||||
String token = tokenProvider.createToken(jwtUser);
|
||||
String token = tokenProvider.createToken(jwtUser,Map.of(
|
||||
"userType", jwtUser.getUserType().getValue()));
|
||||
// 返回 token 与 用户信息
|
||||
Map<String, Object> authInfo = new HashMap<String, Object>(2) {{
|
||||
put("token", properties.getTokenStartWith() + token);
|
||||
@ -133,7 +131,8 @@ public class AuthController {
|
||||
Authentication authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities());
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
// 生成令牌
|
||||
String token = tokenProvider.createToken(jwtUser);
|
||||
String token = tokenProvider.createToken(jwtUser,Map.of(
|
||||
"userType", jwtUser.getUserType().getValue()));
|
||||
// 返回 token 与 用户信息
|
||||
Map<String, Object> authInfo = new HashMap<String, Object>(2) {{
|
||||
put("token", properties.getTokenStartWith() + token);
|
||||
@ -165,17 +164,14 @@ public class AuthController {
|
||||
if (!passwordEncoder.matches(password, jwtUser.getPassword())) {
|
||||
throw new BadRequestException("登录密码错误");
|
||||
}
|
||||
Map<String, String> details = new HashMap<>();
|
||||
details.put("userType", String.valueOf(jwtUser.getUserType().getValue()));
|
||||
// 设置认证信息
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
|
||||
jwtUser, null, jwtUser.getAuthorities()
|
||||
);
|
||||
authentication.setDetails(details);
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
|
||||
// 生成令牌
|
||||
String token = tokenProvider.createToken(jwtUser);
|
||||
String token = tokenProvider.createToken(jwtUser,Map.of(
|
||||
"userType", jwtUser.getUserType().getValue()));
|
||||
|
||||
Map<String, Object> authInfo = new HashMap<>(2) {{
|
||||
put("token", properties.getTokenStartWith() + token);
|
||||
@ -206,13 +202,11 @@ public class AuthController {
|
||||
if (!passwordEncoder.matches(password, jwtUser.getPassword())) {
|
||||
throw new BadRequestException("登录密码错误");
|
||||
}
|
||||
Map<String, String> details = new HashMap<>();
|
||||
details.put("userType", String.valueOf(jwtUser.getUserType().getValue()));
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities());
|
||||
authentication.setDetails(details);
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
// 生成令牌
|
||||
String token = tokenProvider.createToken(jwtUser);
|
||||
String token = tokenProvider.createToken(jwtUser,Map.of(
|
||||
"userType", jwtUser.getUserType().getValue()));
|
||||
// 返回 token 与 用户信息
|
||||
Map<String, Object> authInfo = new HashMap<String, Object>(2) {{
|
||||
put("token", properties.getTokenStartWith() + token);
|
||||
|
@ -70,13 +70,14 @@ public class TokenProvider implements InitializingBean {
|
||||
* @param user /
|
||||
* @return /
|
||||
*/
|
||||
public String createToken(JwtUserDto user) {
|
||||
public String createToken(JwtUserDto user,Map<String, Object> values) {
|
||||
// 设置参数
|
||||
Map<String, Object> claims = new HashMap<>(6);
|
||||
// 设置用户ID
|
||||
claims.put(AUTHORITIES_UID_KEY, user.getUser().getId());
|
||||
// 设置UUID,确保每次Token不一样
|
||||
claims.put(AUTHORITIES_UUID_KEY, IdUtil.simpleUUID());
|
||||
claims.put("userType", values.get("userType"));
|
||||
// 直接调用 Jwts.builder() 创建新实例
|
||||
return Jwts.builder()
|
||||
// 设置自定义 Claims
|
||||
@ -97,7 +98,12 @@ public class TokenProvider implements InitializingBean {
|
||||
Authentication getAuthentication(String token) {
|
||||
Claims claims = getClaims(token);
|
||||
User principal = new User(claims.getSubject(), "******", new ArrayList<>());
|
||||
return new UsernamePasswordAuthenticationToken(principal, token, new ArrayList<>());
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(principal, token, new ArrayList<>());
|
||||
// 从token claims中恢复details
|
||||
Map<String, Object> details = new HashMap<>();
|
||||
details.put("userType", claims.get("userType"));
|
||||
authentication.setDetails(details);
|
||||
return authentication;
|
||||
}
|
||||
|
||||
public Claims getClaims(String token) {
|
||||
|
@ -39,7 +39,7 @@ public class AUserDetailsService implements UserDetailsService {
|
||||
// 使用构造函数传递必要的参数
|
||||
LoginUserDto userDto = new LoginUserDto();
|
||||
userDto.setId(emEmployees.getId());
|
||||
userDto.setUsername(emEmployees.getPhone());
|
||||
userDto.setUsername(emEmployees.getUsername());
|
||||
userDto.setNickName(emEmployees.getName());
|
||||
userDto.setDept(new Dept());
|
||||
userDto.setPassword(emEmployees.getPassword());
|
||||
|
@ -38,7 +38,7 @@ public class BUserDetailsService implements UserDetailsService {
|
||||
// 使用构造函数传递必要的参数
|
||||
LoginUserDto userDto = new LoginUserDto();
|
||||
userDto.setId(customer.getId());
|
||||
userDto.setUsername(customer.getPhone());
|
||||
userDto.setUsername(customer.getUsername());
|
||||
userDto.setNickName(customer.getName());
|
||||
userDto.setDept(new Dept());
|
||||
userDto.setPassword(customer.getPassword());
|
||||
|
Loading…
Reference in New Issue
Block a user