mirror of
https://github.com/RemainderTime/spring-boot-base-demo.git
synced 2026-06-09 03:27:42 +08:00
获取用户信息接口实现
This commit is contained in:
@@ -2,6 +2,8 @@ package cn.xf.basedemo.common.model;
|
|||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @program: xf-boot-base
|
* @program: xf-boot-base
|
||||||
* @ClassName LoginUserInfo
|
* @ClassName LoginUserInfo
|
||||||
@@ -10,7 +12,7 @@ import lombok.Data;
|
|||||||
* @create: 2022-06-17 14:54
|
* @create: 2022-06-17 14:54
|
||||||
**/
|
**/
|
||||||
@Data
|
@Data
|
||||||
public class LoginUser {
|
public class LoginUser implements Serializable {
|
||||||
|
|
||||||
private Integer id;
|
private Integer id;
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
package cn.xf.basedemo.controller;
|
package cn.xf.basedemo.controller;
|
||||||
|
|
||||||
|
import cn.xf.basedemo.common.model.LoginUser;
|
||||||
import cn.xf.basedemo.common.model.RetObj;
|
import cn.xf.basedemo.common.model.RetObj;
|
||||||
|
import cn.xf.basedemo.interceptor.SessionContext;
|
||||||
import cn.xf.basedemo.model.res.LoginInfoRes;
|
import cn.xf.basedemo.model.res.LoginInfoRes;
|
||||||
import cn.xf.basedemo.service.UserService;
|
import cn.xf.basedemo.service.UserService;
|
||||||
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
||||||
import io.swagger.annotations.ApiModelProperty;
|
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
@@ -33,4 +34,12 @@ public class UserController {
|
|||||||
|
|
||||||
return userService.login(res);
|
return userService.login(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value = "用户信息", notes = "用户信息")
|
||||||
|
@ApiOperationSupport(order = 1)
|
||||||
|
@PostMapping("/info")
|
||||||
|
public RetObj info(){
|
||||||
|
LoginUser loginUser = SessionContext.getInstance().get();
|
||||||
|
return RetObj.success(loginUser);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,6 @@ public class InterceptorConfig implements WebMvcConfigurer {
|
|||||||
public void addInterceptors(InterceptorRegistry registry) {
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
registry.addInterceptor(new TokenInterceptor()) //登录逻辑拦截类
|
registry.addInterceptor(new TokenInterceptor()) //登录逻辑拦截类
|
||||||
.addPathPatterns("/**") //需要拦截的请求(设置的全部拦截)
|
.addPathPatterns("/**") //需要拦截的请求(设置的全部拦截)
|
||||||
.excludePathPatterns("/user/**","/web/**"); //忽略的请求
|
.excludePathPatterns("/user/login","/web/**"); //忽略的请求
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ package cn.xf.basedemo.interceptor;
|
|||||||
|
|
||||||
import cn.xf.basedemo.common.model.LoginUser;
|
import cn.xf.basedemo.common.model.LoginUser;
|
||||||
import cn.xf.basedemo.common.utils.ApplicationContextUtils;
|
import cn.xf.basedemo.common.utils.ApplicationContextUtils;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
||||||
import org.springframework.web.servlet.HandlerInterceptor;
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@@ -28,9 +28,6 @@ public class TokenInterceptor implements HandlerInterceptor {
|
|||||||
RedisTemplate<Object,Object> redisTemplate =
|
RedisTemplate<Object,Object> redisTemplate =
|
||||||
(RedisTemplate<Object,Object>) ApplicationContextUtils.getBean("redisTemplate");
|
(RedisTemplate<Object,Object>) ApplicationContextUtils.getBean("redisTemplate");
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ObjectMapper objectMapper;
|
|
||||||
|
|
||||||
//不拦截的请求列表
|
//不拦截的请求列表
|
||||||
private static final List<String> EXCLUDE_PATH_LIST = Arrays.asList("/user/login","/web/login");
|
private static final List<String> EXCLUDE_PATH_LIST = Arrays.asList("/user/login","/web/login");
|
||||||
|
|
||||||
@@ -52,7 +49,9 @@ public class TokenInterceptor implements HandlerInterceptor {
|
|||||||
if(StringUtils.isEmpty(value)){
|
if(StringUtils.isEmpty(value)){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
LoginUser loginUserInfo = objectMapper.convertValue(value, LoginUser.class);
|
JSONObject jsonObject = JSONObject.parseObject(value);
|
||||||
|
//JSON对象转换成Java对象
|
||||||
|
LoginUser loginUserInfo = JSONObject.toJavaObject(jsonObject, LoginUser.class);
|
||||||
if(loginUserInfo == null || loginUserInfo.getId() <= 0){
|
if(loginUserInfo == null || loginUserInfo.getId() <= 0){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user