Initial commit
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package com.noblelift.ota.module.auth.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.noblelift.ota.common.annotation.Log;
|
||||
import com.noblelift.ota.common.dto.ApiMessage;
|
||||
import com.noblelift.ota.module.auth.dto.LoginResponse;
|
||||
import com.noblelift.ota.module.auth.param.LoginParam;
|
||||
import com.noblelift.ota.module.auth.service.AuthService;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/auth")
|
||||
@RequiredArgsConstructor
|
||||
public class AuthController {
|
||||
|
||||
private final AuthService authService;
|
||||
|
||||
@Log("用户登录")
|
||||
@PostMapping("/login")
|
||||
public LoginResponse login(@Valid @RequestBody LoginParam param) {
|
||||
return authService.login(param);
|
||||
}
|
||||
|
||||
@Log("用户退出")
|
||||
@SaCheckLogin
|
||||
@PostMapping("/logout")
|
||||
public ApiMessage logout() {
|
||||
authService.logout();
|
||||
return new ApiMessage("OK", "logout success", Instant.now());
|
||||
}
|
||||
|
||||
@Log("查询当前用户")
|
||||
@SaCheckLogin
|
||||
@GetMapping("/me")
|
||||
public Map<String, Object> currentUser() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("loginId", StpUtil.getLoginIdAsLong());
|
||||
result.put("roles", authService.getCurrentRoles());
|
||||
result.put("token", StpUtil.getTokenValue());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.noblelift.ota.module.auth.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class LoginResponse {
|
||||
|
||||
private String token;
|
||||
private Long userId;
|
||||
private String username;
|
||||
private String nickname;
|
||||
private List<String> roles;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.noblelift.ota.module.auth.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@Data
|
||||
@TableName("sys_role")
|
||||
public class SysRoleEntity {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
private String roleCode;
|
||||
private String roleName;
|
||||
private Integer status;
|
||||
private Instant createdAt;
|
||||
private Instant updatedAt;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.noblelift.ota.module.auth.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@Data
|
||||
@TableName("sys_user")
|
||||
public class SysUserEntity {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
private String username;
|
||||
private String password;
|
||||
private String nickname;
|
||||
private Integer status;
|
||||
private Instant createdAt;
|
||||
private Instant updatedAt;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.noblelift.ota.module.auth.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@Data
|
||||
@TableName("sys_user_role")
|
||||
public class SysUserRoleEntity {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
private Long userId;
|
||||
private Long roleId;
|
||||
private Instant createdAt;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.noblelift.ota.module.auth.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.noblelift.ota.module.auth.entity.SysRoleEntity;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface SysRoleMapper extends BaseMapper<SysRoleEntity> {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.noblelift.ota.module.auth.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.noblelift.ota.module.auth.entity.SysUserEntity;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface SysUserMapper extends BaseMapper<SysUserEntity> {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.noblelift.ota.module.auth.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.noblelift.ota.module.auth.entity.SysUserRoleEntity;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface SysUserRoleMapper extends BaseMapper<SysUserRoleEntity> {
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.noblelift.ota.module.auth.param;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class LoginParam {
|
||||
|
||||
@NotBlank
|
||||
private String username;
|
||||
|
||||
@NotBlank
|
||||
private String password;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.noblelift.ota.module.auth.service;
|
||||
|
||||
import com.noblelift.ota.module.auth.dto.LoginResponse;
|
||||
import com.noblelift.ota.module.auth.param.LoginParam;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface AuthService {
|
||||
|
||||
LoginResponse login(LoginParam param);
|
||||
|
||||
void logout();
|
||||
|
||||
List<String> getCurrentRoles();
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.noblelift.ota.module.auth.service.impl;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.noblelift.ota.module.auth.dto.LoginResponse;
|
||||
import com.noblelift.ota.module.auth.entity.SysRoleEntity;
|
||||
import com.noblelift.ota.module.auth.entity.SysUserEntity;
|
||||
import com.noblelift.ota.module.auth.entity.SysUserRoleEntity;
|
||||
import com.noblelift.ota.module.auth.mapper.SysRoleMapper;
|
||||
import com.noblelift.ota.module.auth.mapper.SysUserMapper;
|
||||
import com.noblelift.ota.module.auth.mapper.SysUserRoleMapper;
|
||||
import com.noblelift.ota.module.auth.param.LoginParam;
|
||||
import com.noblelift.ota.module.auth.service.AuthService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.DigestUtils;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class AuthServiceImpl implements AuthService {
|
||||
|
||||
private final SysUserMapper sysUserMapper;
|
||||
private final SysRoleMapper sysRoleMapper;
|
||||
private final SysUserRoleMapper sysUserRoleMapper;
|
||||
|
||||
@Override
|
||||
public LoginResponse login(LoginParam param) {
|
||||
SysUserEntity user = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUserEntity>()
|
||||
.eq(SysUserEntity::getUsername, param.getUsername())
|
||||
.last("limit 1"));
|
||||
if (user == null || user.getStatus() == null || user.getStatus() != 1) {
|
||||
throw new IllegalArgumentException("用户不存在或已禁用");
|
||||
}
|
||||
|
||||
String encodedPassword = DigestUtils.md5DigestAsHex(param.getPassword().getBytes(StandardCharsets.UTF_8));
|
||||
if (!encodedPassword.equals(user.getPassword())) {
|
||||
throw new IllegalArgumentException("用户名或密码错误");
|
||||
}
|
||||
|
||||
StpUtil.login(user.getId());
|
||||
List<String> roles = getRolesByUserId(user.getId());
|
||||
return new LoginResponse(StpUtil.getTokenValue(), user.getId(), user.getUsername(), user.getNickname(), roles);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logout() {
|
||||
StpUtil.logout();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getCurrentRoles() {
|
||||
Object loginId = StpUtil.getLoginIdDefaultNull();
|
||||
if (loginId == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return getRolesByUserId(Long.valueOf(String.valueOf(loginId)));
|
||||
}
|
||||
|
||||
private List<String> getRolesByUserId(Long userId) {
|
||||
List<Long> roleIds = sysUserRoleMapper.selectList(new LambdaQueryWrapper<SysUserRoleEntity>()
|
||||
.eq(SysUserRoleEntity::getUserId, userId))
|
||||
.stream()
|
||||
.map(SysUserRoleEntity::getRoleId)
|
||||
.toList();
|
||||
if (roleIds.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return sysRoleMapper.selectBatchIds(roleIds).stream()
|
||||
.map(SysRoleEntity::getRoleCode)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.noblelift.ota.module.auth.service.impl;
|
||||
|
||||
import cn.dev33.satoken.stp.StpInterface;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.noblelift.ota.module.auth.entity.SysRoleEntity;
|
||||
import com.noblelift.ota.module.auth.entity.SysUserRoleEntity;
|
||||
import com.noblelift.ota.module.auth.mapper.SysRoleMapper;
|
||||
import com.noblelift.ota.module.auth.mapper.SysUserRoleMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class StpInterfaceImpl implements StpInterface {
|
||||
|
||||
private final SysUserRoleMapper sysUserRoleMapper;
|
||||
private final SysRoleMapper sysRoleMapper;
|
||||
|
||||
@Override
|
||||
public List<String> getPermissionList(Object loginId, String loginType) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getRoleList(Object loginId, String loginType) {
|
||||
Long userId = Long.valueOf(String.valueOf(loginId));
|
||||
List<Long> roleIds = sysUserRoleMapper.selectList(new LambdaQueryWrapper<SysUserRoleEntity>()
|
||||
.eq(SysUserRoleEntity::getUserId, userId))
|
||||
.stream()
|
||||
.map(SysUserRoleEntity::getRoleId)
|
||||
.toList();
|
||||
if (roleIds.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return sysRoleMapper.selectBatchIds(roleIds).stream()
|
||||
.map(SysRoleEntity::getRoleCode)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user