自定义注解获取当前登录人信息

 

import java.lang.annotation.*;

/**
 * 
 * 获取当前登录人信息
 * 该注解可用于 类 ,方法,参数上
 * @author 
 * @since  2020-06-18
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD,ElementType.PARAMETER})
public @interface User {

}

 


import cn.com.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * 参数解析器 配置信息
 * @author 
 * @create 2020-06-18 17:08
 */
@Configuration
public class ArgumentResolverConfig   implements WebMvcConfigurer {
    
    // 获取用户信息的server 
    @Autowired
    private UserService userService;
    
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new LoginUserArgumentResolver(userService));
    }

}

import cn.com.annotation.User;
import cn.com.security.SecurityUtils;
import cn.com.sys.dto.DeptDto;
import cn.com.sys.dto.UserDto;
import cn.com.sys.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;

/**
 * 用户信息参数解析器
 * @author 
 * @create 2020-06-18 15:18
 */
@Slf4j
public class LoginUserArgumentResolver implements HandlerMethodArgumentResolver {

    private UserService userService;


    public LoginUserArgumentResolver() {

    }
    public LoginUserArgumentResolver(UserService userService) {
       this.userService = userService;
    }

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        final Method method = parameter.getMethod();
        final Class<?> clazz = parameter.getMethod().getDeclaringClass();
        boolean hasParameterAnnotation = parameter.hasParameterAnnotation(User.class);

        boolean isHasLoginAuth = clazz.isAnnotationPresent(User.class) || method.isAnnotationPresent(User.class) || hasParameterAnnotation;
        boolean isHasLoginUserParameter = parameter.getParameterType().isAssignableFrom(UserDto.class);

        return isHasLoginAuth && isHasLoginUserParameter;

    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        return getUserByJWT();
    }

    /**
     * 获取用户信息
     * @return
     */
    public UserDto getUserByJWT() {
        UserDto user = (UserDto)SecurityUtils.getCurrentUser();
        List<DeptDto> depts = userService.findDepts(user.getId(), null);
        user.setDepts(new HashSet(depts));
        return user;
    }

}
    /**
     * 使用示例
     */
    @User
    @GetMapping(value = "/home")
    public ResponseEntity getHomeData(UserDto user,HomeCriteria criteria){
        return ResponseEntity.ok(homeService.queryAll(criteria,user));
    }

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潇凝子潇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值