generated from youfool-project/youfool-prj-springboot-template
124 lines
3.8 KiB
Java
124 lines
3.8 KiB
Java
|
|
package com.chinaweal.youfool.course.controller;
|
|||
|
|
|
|||
|
|
import cn.dev33.satoken.session.SaSession;
|
|||
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|||
|
|
import com.chinaweal.youfool.framework.springboot.common.base.BaseController;
|
|||
|
|
import com.chinaweal.youfool.framework.springboot.common.util.AssertUtils;
|
|||
|
|
import com.chinaweal.youfool.framework.springboot.common.util.RSAUtil;
|
|||
|
|
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
|
|||
|
|
import com.chinaweal.youfool.framework.springboot.rest.ResultCode;
|
|||
|
|
import com.chinaweal.youfool.framework.springboot.user.entity.UserBase;
|
|||
|
|
import com.chinaweal.youfool.course.common.constants.SessionConstants;
|
|||
|
|
import lombok.extern.slf4j.Slf4j;
|
|||
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|||
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|||
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|||
|
|
import org.springframework.web.bind.annotation.RestController;
|
|||
|
|
|
|||
|
|
import java.util.HashSet;
|
|||
|
|
import java.util.List;
|
|||
|
|
import java.util.Set;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 登录相关接口
|
|||
|
|
*
|
|||
|
|
* @author lroyia
|
|||
|
|
* @since 2022/4/20 15:02
|
|||
|
|
**/
|
|||
|
|
@Slf4j
|
|||
|
|
@RestController
|
|||
|
|
@RequestMapping("/user/auth")
|
|||
|
|
public class LoginController extends BaseController {
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 登录接口
|
|||
|
|
*
|
|||
|
|
* @param username 用户名
|
|||
|
|
* @param password 密码
|
|||
|
|
* @param encrypt 密码是否已经加密
|
|||
|
|
* @return 登录结果
|
|||
|
|
* @author lroyia
|
|||
|
|
* @since 2022年4月20日 15:47:35
|
|||
|
|
*/
|
|||
|
|
@PostMapping("login")
|
|||
|
|
public RestResult<?> doLogin(String username, String password, Boolean encrypt) {
|
|||
|
|
AssertUtils.isNotBlank(username, password);
|
|||
|
|
if (encrypt != null && encrypt) {
|
|||
|
|
String privateKey = getPrivateKey();
|
|||
|
|
try {
|
|||
|
|
password = RSAUtil.decrypt(password, privateKey);
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
log.error("密码解密失败:{}", password, e);
|
|||
|
|
return RestResult.error(ResultCode.BUSINESS_LOGIC_ERROR, "密码解密失败");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// TODO:与数据库匹配校验,具体按用户信息表
|
|||
|
|
|
|||
|
|
// 匹配成功的话,登记登录信息 TODO:这里的userId是用户唯一号,应根据实际的数据库信息进行替换
|
|||
|
|
StpUtil.login("userId");
|
|||
|
|
SaSession session = StpUtil.getSession();
|
|||
|
|
// 将用户信息存储至session TODO:登录信息存到这里
|
|||
|
|
UserBase userBase = new UserBase();
|
|||
|
|
Set<String> permissionSet = new HashSet<>();
|
|||
|
|
permissionSet.add("admin");
|
|||
|
|
userBase.setPermission(permissionSet);
|
|||
|
|
session.set("user", userBase);
|
|||
|
|
|
|||
|
|
return RestResult.ok();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取当前持有的公钥
|
|||
|
|
*
|
|||
|
|
* @return 获取结果
|
|||
|
|
* @author lroyia
|
|||
|
|
* @since 2022年4月20日 15:55:13
|
|||
|
|
*/
|
|||
|
|
@GetMapping("publicKey")
|
|||
|
|
public RestResult<String> getCurrentPublicKey() {
|
|||
|
|
return RestResult.ok(getPublicKey());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 登录信息
|
|||
|
|
*
|
|||
|
|
* @return 状态判断
|
|||
|
|
* @author lroyia
|
|||
|
|
* @since 2022年4月20日 15:58:26
|
|||
|
|
*/
|
|||
|
|
@GetMapping("login/info")
|
|||
|
|
public RestResult<UserBase> loginInfo() {
|
|||
|
|
boolean login = StpUtil.isLogin();
|
|||
|
|
if (login) {
|
|||
|
|
SaSession session = getSession();
|
|||
|
|
return RestResult.ok((UserBase) session.get(SessionConstants.USER_KEY));
|
|||
|
|
}
|
|||
|
|
return RestResult.ok(null);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 登出
|
|||
|
|
*
|
|||
|
|
* @return 操作结果
|
|||
|
|
* @author lroyia
|
|||
|
|
* @since 2022年4月20日 15:59:55
|
|||
|
|
*/
|
|||
|
|
@GetMapping("logout")
|
|||
|
|
public RestResult<?> logout() {
|
|||
|
|
StpUtil.logout();
|
|||
|
|
return RestResult.ok();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 列出用户权限信息
|
|||
|
|
*
|
|||
|
|
* @return 权限列表
|
|||
|
|
* @author lroyia
|
|||
|
|
* @since 2022年4月20日 16:12:09
|
|||
|
|
*/
|
|||
|
|
@GetMapping("perm/list")
|
|||
|
|
public RestResult<List<String>> getPermList() {
|
|||
|
|
return RestResult.ok(StpUtil.getPermissionList());
|
|||
|
|
}
|
|||
|
|
}
|