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