用户登陆功能实现,基本接口鉴权功能实现
This commit is contained in:
@@ -11,6 +11,8 @@ public enum ErrorCode {
|
||||
SUCCESS(0, "ok"),
|
||||
PARAMS_ERROR(40000, "请求参数错误"),
|
||||
NOT_LOGIN_ERROR(40100, "未登录"),
|
||||
USERNAME_OR_PASSWORD_ERROR(40200, "账号或密码错误"),
|
||||
USER_DISABLE(40300, "用户被禁用"),
|
||||
NO_AUTH_ERROR(40101, "无权限"),
|
||||
NOT_FOUND_ERROR(40400, "请求数据不存在"),
|
||||
FORBIDDEN_ERROR(40300, "禁止访问"),
|
||||
|
||||
@@ -21,11 +21,12 @@ public class MyBatisPlusConfig {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
// @Bean
|
||||
// public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
// MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
// // 分页插件
|
||||
// interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
// return interceptor;
|
||||
// }
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
// 分页插件
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.yupi.springbootinit.config;
|
||||
|
||||
import cn.dev33.satoken.interceptor.SaInterceptor;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class SaTokenConfigure implements WebMvcConfigurer {
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
// 注册Sa-Token的拦截器
|
||||
registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
|
||||
.addPathPatterns("/**")
|
||||
.excludePathPatterns(getExcludePaths());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取需要放行的路径
|
||||
*/
|
||||
private String[] getExcludePaths() {
|
||||
return new String[]{
|
||||
// Swagger & Knife4j 相关
|
||||
"/doc.html",
|
||||
"/webjars/**",
|
||||
"/swagger-resources/**",
|
||||
"/v2/api-docs",
|
||||
"/v3/api-docs",
|
||||
"/v3/api-docs/**",
|
||||
"/swagger-ui.html",
|
||||
"/swagger-ui/**",
|
||||
"/favicon.ico",
|
||||
// 你的其他放行路径,例如登录接口
|
||||
"/user/doLogin",
|
||||
"/save_data/add_host"
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.yupi.springbootinit.controller;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.yupi.springbootinit.common.BaseResponse;
|
||||
import com.yupi.springbootinit.common.ErrorCode;
|
||||
import com.yupi.springbootinit.common.ResultUtils;
|
||||
import com.yupi.springbootinit.exception.BusinessException;
|
||||
import com.yupi.springbootinit.model.dto.user.SystemUsersDTO;
|
||||
import com.yupi.springbootinit.model.entity.SystemUsers;
|
||||
import com.yupi.springbootinit.model.enums.CommonStatusEnum;
|
||||
import com.yupi.springbootinit.model.vo.user.SystemUsersVO;
|
||||
import com.yupi.springbootinit.service.SystemUsersService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/6/11 19:50
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
@Slf4j
|
||||
@CrossOrigin
|
||||
public class UserController {
|
||||
|
||||
|
||||
@Resource
|
||||
private SystemUsersService usersService;
|
||||
|
||||
// 测试登录,浏览器访问: http://localhost:8081/user/doLogin?username=zhang&password=123456
|
||||
@PostMapping("doLogin")
|
||||
public BaseResponse<SystemUsersVO> doLogin(@RequestBody SystemUsersDTO usersDTO) {
|
||||
SystemUsers user = usersService.getUserByUserName(usersDTO.getUsername());
|
||||
if (user == null) {
|
||||
throw new BusinessException(ErrorCode.USERNAME_OR_PASSWORD_ERROR);
|
||||
}
|
||||
|
||||
if (!usersService.isPasswordMatch(usersDTO.getPassword(), user.getPassword())) {
|
||||
throw new BusinessException(ErrorCode.USERNAME_OR_PASSWORD_ERROR);
|
||||
}
|
||||
|
||||
if (CommonStatusEnum.isDisable(user.getStatus())) {
|
||||
throw new BusinessException(ErrorCode.USER_DISABLE);
|
||||
}
|
||||
SystemUsersVO systemUsersVO = new SystemUsersVO();
|
||||
BeanUtil.copyProperties(user, systemUsersVO);
|
||||
StpUtil.login(user.getId());
|
||||
return ResultUtils.success(systemUsersVO);
|
||||
}
|
||||
|
||||
// // 查询登录状态,浏览器访问: http://localhost:8081/user/isLogin
|
||||
// @RequestMapping("isLogin")
|
||||
// public String isLogin() {
|
||||
// return "当前会话是否登录:" + StpUtil.isLogin();
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.yupi.springbootinit.mapper;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/6/11 20:13
|
||||
*/
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.yupi.springbootinit.model.entity.SystemUsers;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface SystemUsersMapper extends BaseMapper<SystemUsers> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.yupi.springbootinit.model.dto.user;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/6/11 20:13
|
||||
*/
|
||||
|
||||
/**
|
||||
* 用户信息表
|
||||
*/
|
||||
@Data
|
||||
public class SystemUsersDTO {
|
||||
|
||||
/**
|
||||
* 用户账号
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 租户编号
|
||||
*/
|
||||
private Long tenantId;
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package com.yupi.springbootinit.model.entity;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import lombok.Data;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/6/11 20:13
|
||||
*/
|
||||
|
||||
/**
|
||||
* 用户信息表
|
||||
*/
|
||||
@Data
|
||||
public class SystemUsers {
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 用户账号
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 用户昵称
|
||||
*/
|
||||
private String nickname;
|
||||
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* 部门ID
|
||||
*/
|
||||
@TableField("dept_id")
|
||||
private Long deptId;
|
||||
|
||||
/**
|
||||
* 岗位编号数组
|
||||
*/
|
||||
@TableField("post_ids")
|
||||
private String postIds;
|
||||
|
||||
/**
|
||||
* 用户邮箱
|
||||
*/
|
||||
private String email;
|
||||
|
||||
/**
|
||||
* 手机号码
|
||||
*/
|
||||
private String mobile;
|
||||
|
||||
/**
|
||||
* 用户性别
|
||||
*/
|
||||
private Byte sex;
|
||||
|
||||
/**
|
||||
* 头像地址
|
||||
*/
|
||||
private String avatar;
|
||||
|
||||
/**
|
||||
* 帐号状态(0正常 1停用)
|
||||
*/
|
||||
private Byte status;
|
||||
|
||||
/**
|
||||
* 最后登录IP
|
||||
*/
|
||||
@TableField("login_ip")
|
||||
private String loginIp;
|
||||
|
||||
/**
|
||||
* 最后登录时间
|
||||
*/
|
||||
@TableField("login_date")
|
||||
private Date loginDate;
|
||||
|
||||
/**
|
||||
* 创建者
|
||||
*/
|
||||
private String creator;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@TableField("create_time")
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 更新者
|
||||
*/
|
||||
private String updater;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@TableField("update_time")
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
* 是否删除
|
||||
*/
|
||||
private Boolean deleted;
|
||||
|
||||
/**
|
||||
* 租户编号
|
||||
*/
|
||||
@TableField("tenant_id")
|
||||
private Long tenantId;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.yupi.springbootinit.model.enums;
|
||||
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
|
||||
import com.yupi.springbootinit.utils.ArrayValuable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 通用状态枚举
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum CommonStatusEnum implements ArrayValuable<Integer> {
|
||||
|
||||
ENABLE(0, "开启"),
|
||||
DISABLE(1, "关闭");
|
||||
|
||||
public static final Integer[] ARRAYS = Arrays.stream(values()).map(CommonStatusEnum::getStatus).toArray(Integer[]::new);
|
||||
|
||||
/**
|
||||
* 状态值
|
||||
*/
|
||||
private final Integer status;
|
||||
/**
|
||||
* 状态名
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
@Override
|
||||
public Integer[] array() {
|
||||
return ARRAYS;
|
||||
}
|
||||
|
||||
public static boolean isEnable(Integer status) {
|
||||
return ObjUtil.equal(ENABLE.status, status);
|
||||
}
|
||||
|
||||
public static boolean isDisable(Byte status) {
|
||||
return ObjUtil.equal(DISABLE.status, status);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.yupi.springbootinit.model.vo.user;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/6/11 20:13
|
||||
*/
|
||||
|
||||
/**
|
||||
* 用户信息表
|
||||
*/
|
||||
@Data
|
||||
public class SystemUsersVO {
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 租户编号
|
||||
*/
|
||||
private Long tenantId;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.yupi.springbootinit.service;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/6/11 20:13
|
||||
*/
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.yupi.springbootinit.model.entity.SystemUsers;
|
||||
|
||||
public interface SystemUsersService extends IService<SystemUsers> {
|
||||
|
||||
SystemUsers getUserByUserName(String username);
|
||||
|
||||
boolean isPasswordMatch(String rawPassWord, String encodedPassword);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.yupi.springbootinit.service.impl;
|
||||
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.yupi.springbootinit.model.entity.SystemUsers;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.yupi.springbootinit.mapper.SystemUsersMapper;
|
||||
import com.yupi.springbootinit.service.SystemUsersService;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/6/11 20:13
|
||||
*/
|
||||
|
||||
@Service
|
||||
public class SystemUsersServiceImpl extends ServiceImpl<SystemUsersMapper,SystemUsers> implements SystemUsersService{
|
||||
|
||||
@Value("${md5.salt}")
|
||||
private String MD5_SALT;
|
||||
|
||||
@Override
|
||||
public SystemUsers getUserByUserName(String username) {
|
||||
QueryWrapper <SystemUsers> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("username",username);
|
||||
return baseMapper.selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPasswordMatch(String rawPassword, String encodedPassword) {
|
||||
String s = SecureUtil.md5(MD5_SALT + rawPassword);
|
||||
return s.equals(encodedPassword);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.yupi.springbootinit.utils;
|
||||
|
||||
/**
|
||||
* 可生成 T 数组的接口
|
||||
*
|
||||
* @author HUIHUI
|
||||
*/
|
||||
public interface ArrayValuable<T> {
|
||||
|
||||
/**
|
||||
* @return 数组
|
||||
*/
|
||||
T[] array();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user