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