新增:新增用户登录和退出 API(引入 Sa-Token 依赖,详情可见 README 介绍)

This commit is contained in:
2022-12-22 19:39:27 +08:00
parent d54c93aebc
commit 00e2b44d0e
27 changed files with 1352 additions and 49 deletions

View File

@@ -0,0 +1,59 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.auth.config.satoken;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.jwt.StpLogicJwtForSimple;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
/**
* Sa-Token 配置
*
* @author Charles7c
* @since 2022/12/19 22:13
*/
@Slf4j
@Configuration
@RequiredArgsConstructor
public class SaTokenConfiguration implements WebMvcConfigurer {
private final SecurityProperties securityProperties;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 注册 Sa-Token 拦截器,校验规则为 StpUtil.checkLogin() 登录校验
registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin())).addPathPatterns("/**")
.excludePathPatterns(securityProperties.getExcludes());
}
/**
* Sa-Token 整合 JWT简单模式
*/
@Bean
public StpLogic getStpLogicJwt() {
return new StpLogicJwtForSimple();
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.auth.config.satoken;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* Sa-Token 安全配置属性
*
* @author Charles7c
* @since 2022/12/19 22:14
*/
@Data
@Component
@ConfigurationProperties(prefix = "security")
public class SecurityProperties {
/**
* 排除路径配置
*/
private String[] excludes = new String[0];
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.auth.model.entity;
import lombok.Data;
import top.charles7c.cnadmin.common.model.entity.BaseEntity;
/**
* 用户实体
*
* @author Charles7c
* @since 2022/12/21 20:42
*/
@Data
public class SysUser extends BaseEntity {
private static final long serialVersionUID = 1L;
/**
* 用户 ID
*/
private Long userId;
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 昵称
*/
private String nickname;
/**
* 性别0未知 1男 2女
*/
private Integer gender;
/**
* 头像
*/
private String avatar;
/**
* 状态1启用 2禁用
*/
private Integer status;
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.auth.model.request;
import java.io.Serializable;
import javax.validation.constraints.NotBlank;
import lombok.Data;
import io.swagger.v3.oas.annotations.media.Schema;
/**
* 登录信息
*
* @author Charles7c
* @since 2022/12/21 20:43
*/
@Data
@Schema(description = "登录信息")
public class LoginRequest implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 用户名
*/
@Schema(description = "用户名")
@NotBlank(message = "用户名不能为空")
private String username;
/**
* 密码
*/
@Schema(description = "密码")
@NotBlank(message = "密码不能为空")
private String password;
/**
* 验证码
*/
@Schema(description = "验证码")
@NotBlank(message = "验证码不能为空")
private String captcha;
/**
* 验证码标识
*/
@Schema(description = "验证码标识")
@NotBlank(message = "验证码标识不能为空")
private String uuid;
}

View File

@@ -37,9 +37,9 @@ public class CaptchaVO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 验证码唯一标识
* 验证码标识
*/
@Schema(description = "验证码唯一标识")
@Schema(description = "验证码标识")
private String uuid;
/**

View File

@@ -0,0 +1,44 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.auth.model.vo;
import java.io.Serializable;
import lombok.Data;
import lombok.experimental.Accessors;
import io.swagger.v3.oas.annotations.media.Schema;
/**
* 令牌信息
*
* @author Charles7c
* @since 2022/12/21 20:42
*/
@Data
@Accessors(chain = true)
@Schema(description = "令牌信息")
public class LoginVO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 令牌
*/
@Schema(description = "令牌")
private String token;
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.auth.service;
/**
* 登录业务接口
*
* @author Charles7c
* @since 2022/12/21 21:48
*/
public interface LoginService {
/**
* 用户登录
*
* @param username
* 用户名
* @param password
* 密码
* @return 令牌
*/
String login(String username, String password);
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.auth.service;
import top.charles7c.cnadmin.auth.model.entity.SysUser;
/**
* 用户业务接口
*
* @author Charles7c
* @since 2022/12/21 21:48
*/
public interface UserService {
/**
* 根据用户名查询
*
* @param username
* 用户名
* @return 用户信息
*/
SysUser getByUsername(String username);
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.auth.service.impl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import cn.dev33.satoken.stp.StpUtil;
import top.charles7c.cnadmin.auth.model.entity.SysUser;
import top.charles7c.cnadmin.auth.service.LoginService;
import top.charles7c.cnadmin.auth.service.UserService;
import top.charles7c.cnadmin.common.consts.CommonConstants;
import top.charles7c.cnadmin.common.util.CheckUtils;
import top.charles7c.cnadmin.common.util.SecureUtils;
/**
* 登录业务实现类
*
* @author Charles7c
* @since 2022/12/21 21:49
*/
@Service
@RequiredArgsConstructor
public class LoginServiceImpl implements LoginService {
private final UserService userService;
@Override
public String login(String username, String password) {
// 查询用户
SysUser sysUser = userService.getByUsername(username);
// 校验
CheckUtils.exIfNull(sysUser, "用户名或密码错误");
Long userId = sysUser.getUserId();
CheckUtils.exIfNotEqual(sysUser.getPassword(), SecureUtils.md5Salt(password, userId.toString()), "用户名或密码错误");
CheckUtils.exIfEqual(CommonConstants.STATUS_DISABLE, sysUser.getStatus(), "此账号已被禁用,如有疑问,请联系管理员");
// 登录
StpUtil.login(userId);
// 返回令牌
return StpUtil.getTokenValue();
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.auth.service.impl;
import java.util.Date;
import org.springframework.stereotype.Service;
import top.charles7c.cnadmin.auth.model.entity.SysUser;
import top.charles7c.cnadmin.auth.service.UserService;
import top.charles7c.cnadmin.common.util.SecureUtils;
/**
* 用户业务实现类
*
* @author Charles7c
* @since 2022/12/21 21:49
*/
@Service
public class UserServiceImpl implements UserService {
@Override
public SysUser getByUsername(String username) {
if (!"admin".equals(username)) {
return null;
}
SysUser sysUser = new SysUser();
sysUser.setUserId(1L);
sysUser.setUsername("admin");
sysUser.setPassword(SecureUtils.md5Salt("123456", sysUser.getUserId().toString()));
sysUser.setNickname("超级管理员");
sysUser.setGender(1);
sysUser.setStatus(1);
sysUser.setCreateUser(1L);
sysUser.setCreateTime(new Date());
sysUser.setUpdateUser(1L);
sysUser.setUpdateTime(new Date());
return sysUser;
}
}