Commit f0a40ba6 authored by YazhouChen's avatar YazhouChen

获取当前登录用户

parent 3580ecc6
......@@ -43,21 +43,6 @@ public class LoginUtil {
LoginUtil.redisTemplate = redisTemplate;
}
/**
* 获取当前登录用户对象
*
* @return
*/
public static Object getWxUser() {
// 获取当前登录用户
String token = JwtTokenUtil.getToken();
if (StringUtils.isBlank(token)) {
return null;
}
return redisTemplate.opsForValue().get("USER_"+token);
}
public static Object getSysUser() {
// 获取当前后端登录用户
String token = JwtTokenUtil.getToken();
......
......@@ -2,6 +2,8 @@ package com.hongxinhui.controller;
import com.hongxinhui.entity.User;
import com.hongxinhui.service.UserService;
import com.hongxinhui.utils.StringUtils;
import io.geekidea.springbootplus.framework.log.annotation.OperationLogIgnore;
import lombok.extern.slf4j.Slf4j;
import com.hongxinhui.param.UserPageParam;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
......@@ -19,6 +21,9 @@ import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
/**
* 用户表 控制器
*
......@@ -35,6 +40,38 @@ public class UserController extends BaseController {
@Autowired
private UserService userService;
@PostMapping("/login")
@OperationLogIgnore
@ApiOperation(value = "后台用户登录", notes = "后台用户登录")
public ApiResult<Object> login(@RequestParam(value = "name", required = true) String name,
@RequestParam(value = "pwd", required = true) String pwd,
HttpServletResponse response) throws Exception {
if(StringUtils.isEmpty(name)||StringUtils.isEmpty(pwd) ){
return ApiResult.fail(500, "必填不能为空");
}
HashMap<String,Object> info = userService.login(name,pwd);
if(info.get("code").toString().equals("200")){
return ApiResult.ok(info.get("data"),"登录成功");
}else{
return ApiResult.fail(500, info.get("info").toString());
}
}
@PostMapping("/loginOut")
@OperationLogIgnore
@ApiOperation(value = "退出登录", notes = "退出登录")
public ApiResult<Object> loginOut(HttpServletResponse response) throws Exception {
HashMap<String,Object> info = userService.loginOut();
if(info.get("code").toString().equals("200")){
return ApiResult.ok(info.get("data"),"退出成功");
}else{
return ApiResult.fail(500, info.get("info").toString());
}
}
/**
* 添加用户表
*/
......
package com.hongxinhui.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import io.geekidea.springbootplus.framework.common.entity.BaseEntity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.Version;
......@@ -32,7 +33,7 @@ public class User extends BaseEntity {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty("用户名字")
@ApiModelProperty("用户账号")
private String name;
@ApiModelProperty("真实姓名")
......@@ -50,5 +51,7 @@ public class User extends BaseEntity {
@ApiModelProperty("密码,md5加密")
private String pwd;
@TableField(exist = false)
private String token;
}
......@@ -5,6 +5,8 @@ import com.hongxinhui.param.UserPageParam;
import io.geekidea.springbootplus.framework.common.service.BaseService;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import java.util.HashMap;
/**
* 用户表 服务类
*
......@@ -50,4 +52,7 @@ public interface UserService extends BaseService<User> {
*/
Paging<User> getUserPageList(UserPageParam userPageParam) throws Exception;
HashMap<String, Object> login(String name, String pwd);
HashMap<String, Object> loginOut();
}
package com.hongxinhui.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hongxinhui.entity.User;
import com.hongxinhui.mapper.UserMapper;
import com.hongxinhui.service.UserService;
import com.hongxinhui.param.UserPageParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.geekidea.springbootplus.config.properties.JwtProperties;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.geekidea.springbootplus.framework.shiro.util.JwtTokenUtil;
import io.geekidea.springbootplus.framework.shiro.util.JwtUtil;
import io.geekidea.springbootplus.framework.util.LoginUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.time.Duration;
import java.util.HashMap;
/**
* 用户表 服务实现类
*
......@@ -28,6 +38,11 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
@Autowired
private UserMapper userMapper;
@Lazy
@Autowired
private JwtProperties jwtProperties;
@Autowired
private RedisTemplate redisTemplate;
@Transactional(rollbackFor = Exception.class)
@Override
......@@ -49,10 +64,76 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
@Override
public Paging<User> getUserPageList(UserPageParam userPageParam) throws Exception {
User user1 = (User) LoginUtil.getSysUser();
if(user1==null){
System.out.println("没登录 = " );
}else{
System.out.println("登录 = " + user1.toString());
}
Page<User> page = new PageInfo<>(userPageParam, OrderItem.desc(getLambdaColumn(User::getId)));
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
IPage<User> iPage = userMapper.selectPage(page, wrapper);
return new Paging<User>(iPage);
}
@Override
public HashMap<String, Object> login(String name, String pwd) {
HashMap<String, Object> hashMap = new HashMap<>();
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("name",name);
// wrapper.eq("enable",1);
User superadmindb1 = userMapper.selectOne(wrapper);
if(superadmindb1==null){
hashMap.put("code","500");
hashMap.put("info","账号不存在");
return hashMap;
}
// if(superadmindb1.getStatus()==2){
// hashMap.put("code","500");
// hashMap.put("info","账号状态异常,不能登录");
// return hashMap;
// }
if(!superadmindb1.getPwd().equalsIgnoreCase(pwd)){
hashMap.put("code","500");
hashMap.put("info","密码错误");
return hashMap;
}
hashMap.put("code","200");
hashMap.put("info","登录成功");
String username = superadmindb1.getName();
// 生成token字符串并返回
Long expireSecond = jwtProperties.getExpireSecond();
String newSalt = "666666";// 如果盐值为空,则使用默认值:666666
String token = JwtUtil.generateToken(username, newSalt, Duration.ofSeconds(expireSecond));
log.debug("token:{}", token);
redisTemplate.opsForValue().set("SYSUSER_"+token,superadmindb1);
superadmindb1.setToken(token);
hashMap.put("data",superadmindb1);
return hashMap;
}
@Override
public HashMap<String, Object> loginOut() {
HashMap<String, Object> hashMap = new HashMap<>();
String token = JwtTokenUtil.getToken();
if (org.apache.commons.lang3.StringUtils.isBlank(token)) {
hashMap.put("code","500");
hashMap.put("info","还没登录,退出失败");
return hashMap;
}
redisTemplate.opsForValue().set("SYSUSER_"+token,null);
hashMap.put("code","200");
hashMap.put("info","退出成功");
return hashMap;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment