提交了登录方法
This commit is contained in:
parent
1c07580224
commit
3e0cb7396a
@ -199,7 +199,7 @@ private Long jwtExpirationMs;
|
||||
|
||||
@PostMapping("/login")
|
||||
@ResponseBody
|
||||
public ResponseResult login(String username, String password) throws Exception {
|
||||
public ResponseResult login(String username, String password) {
|
||||
try {
|
||||
// 密码解密
|
||||
String encrypt_password = RsaUtils.decryptByPrivateKey(privateKey, password);
|
||||
@ -256,6 +256,10 @@ private Long jwtExpirationMs;
|
||||
} catch (AuthenticationException e) {
|
||||
// 捕获其他认证异常
|
||||
return ResponseResult.error("认证失败:" + e.getMessage());
|
||||
} catch (Exception e) {
|
||||
// 捕获其他认证异常
|
||||
System.out.printf("登录错误异常!");
|
||||
return ResponseResult.error("认证失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
@PostMapping("/logout")
|
||||
|
@ -18,9 +18,9 @@ public class LoginUser implements UserDetails {
|
||||
|
||||
private User user;
|
||||
|
||||
private Collection<? extends GrantedAuthority> permissions;
|
||||
private List<Menu> permissions;
|
||||
|
||||
public LoginUser(User user, Collection<? extends GrantedAuthority> permissions) {
|
||||
public LoginUser(User user, List<Menu> permissions) {
|
||||
this.user = user;
|
||||
this.permissions = permissions;
|
||||
}
|
||||
@ -28,9 +28,13 @@ public class LoginUser implements UserDetails {
|
||||
@JSONField(serialize = false)
|
||||
private List<SimpleGrantedAuthority> authorities;
|
||||
|
||||
public List<Menu> getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||
return permissions;
|
||||
return authorities;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -16,10 +16,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@ -51,8 +48,8 @@ public class CustomUserDetailsService implements UserDetailsService {
|
||||
if (appUser == null) {
|
||||
throw new UsernameNotFoundException("用户不存在: " + username);
|
||||
}
|
||||
Collection<GrantedAuthority> authorities = buildUserAuthorities(appUser);
|
||||
LoginUser loginUser = new LoginUser(appUser,authorities);
|
||||
List<Menu> permissions = buildUserAuthorities(appUser);
|
||||
LoginUser loginUser = new LoginUser(appUser,permissions);
|
||||
return loginUser;
|
||||
|
||||
}
|
||||
@ -63,30 +60,14 @@ public class CustomUserDetailsService implements UserDetailsService {
|
||||
* @param appUser 用户信息
|
||||
* @return 权限集合
|
||||
*/
|
||||
private Collection<GrantedAuthority> buildUserAuthorities(User appUser) {
|
||||
Set<GrantedAuthority> authorities = new HashSet<>();
|
||||
private List<Menu> buildUserAuthorities(User appUser) {
|
||||
List<Menu> permissions = new ArrayList<>();
|
||||
|
||||
try {
|
||||
// 根据用户类型添加基本角色权限
|
||||
if ("0".equals(appUser.getUsertype())) {
|
||||
authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
|
||||
} else {
|
||||
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
|
||||
}
|
||||
|
||||
// 使用RoleMapper直接查询用户的角色信息
|
||||
List<Role> roles = roleMapper.getRoleByUserId(appUser.getId());
|
||||
|
||||
if (!roles.isEmpty()) {
|
||||
// 处理角色权限
|
||||
for (Role role : roles) {
|
||||
if ("1".equals(role.getIsvaild())) {
|
||||
// 添加角色权限,格式:ROLE_角色编码
|
||||
if (StringUtils.hasText(role.getRolecode())) {
|
||||
authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getRolecode().toUpperCase()));
|
||||
}
|
||||
}
|
||||
}
|
||||
// 获取角色ID列表
|
||||
List<String> roleIds = roles.stream()
|
||||
.map(Role::getId)
|
||||
@ -103,44 +84,20 @@ public class CustomUserDetailsService implements UserDetailsService {
|
||||
for (Menu menu : menus) {
|
||||
if ("1".equals(menu.getIsdisplay()) && StringUtils.hasText(menu.getCode())) {
|
||||
// 添加菜单权限,格式:菜单编码
|
||||
authorities.add(new SimpleGrantedAuthority(menu.getCode()));
|
||||
|
||||
// 根据菜单类型添加操作权限
|
||||
String menuCode = menu.getCode();
|
||||
if (StringUtils.hasText(menuCode)) {
|
||||
// 为每个菜单添加基本操作权限
|
||||
authorities.add(new SimpleGrantedAuthority(menuCode + ":list"));
|
||||
authorities.add(new SimpleGrantedAuthority(menuCode + ":detail"));
|
||||
|
||||
// 管理员拥有所有操作权限
|
||||
if ("0".equals(appUser.getUsertype())) {
|
||||
authorities.add(new SimpleGrantedAuthority(menuCode + ":add"));
|
||||
authorities.add(new SimpleGrantedAuthority(menuCode + ":edit"));
|
||||
authorities.add(new SimpleGrantedAuthority(menuCode + ":delete"));
|
||||
authorities.add(new SimpleGrantedAuthority(menuCode + ":permission"));
|
||||
}
|
||||
}
|
||||
permissions.add(menu);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.debug("用户 {} 的权限列表: {}", appUser.getUsername(),
|
||||
authorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList()));
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("构建用户权限失败: {}", e.getMessage(), e);
|
||||
// 发生异常时,至少保证基本角色权限
|
||||
authorities.clear();
|
||||
if ("0".equals(appUser.getUsertype())) {
|
||||
authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
|
||||
} else {
|
||||
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
|
||||
}
|
||||
permissions.clear();
|
||||
}
|
||||
|
||||
return authorities;
|
||||
return permissions;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user