springboot拦截登录信息并自动放入controller层

一、首先创建一个LoginUser的自定义注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.Documented;

/**
 * @Author: leemeea
 * @Date: 2021/7/9 9:52
 */
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LoginUser {
}

二、然后创建一个Resolver,代码如下

import com.common.enums.BaseErrorCode;
import com.common.utils.BearerUtil;
import com.common.utils.ServiceAssert;
import com.console.pojo.dto.user.LoginUserDTO;
import org.springframework.core.MethodParameter;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author: leemeea
 * @Date: 2021/7/9 10:04
 */
@Service("loginUserResolver")
public class LoginUserResolver implements HandlerMethodArgumentResolver {

    @Resource
    public RedisTemplate redisTemplate;

    private static final String bearerKeyPrefix = "bearer-ebi-user-";

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.hasParameterAnnotation(LoginUser.class)
                && methodParameter.getParameterType().isAssignableFrom(LoginUserDTO.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter,
                                  ModelAndViewContainer modelAndViewContainer,
                                  NativeWebRequest nativeWebRequest,
                                  WebDataBinderFactory webDataBinderFactory) {
        // 获取request
        HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
        // request为空,抛出异常
        ServiceAssert.notNull(request, BaseErrorCode.NOT_AUTHORIZATION);
        // 从request获取鉴权信息
        String bearer = BearerUtil.getBearer(request);

        // 从redis获取鉴权信息对应的登录用户dto
        LoginUserDTO loginUserDTO = (LoginUserDTO) redisTemplate.opsForValue().get(bearerKeyPrefix + bearer);
        // 如果登录用户dto为空,抛出异常
        ServiceAssert.notNull(loginUserDTO, BaseErrorCode.NOT_AUTHORIZATION);
        // 返回
        return loginUserDTO;
    }
}

三、将创建的Resolver注入Spring的WebMvcConfigurer中,由WebMvcConfigurer接管。

import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: leemeea
 * @Date: 2021/7/9 11:12
 */
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {

    @Resource
    private LoginUserResolver loginUserResolver;

    public void MyWebMvcConfigurer() {
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(loginUserResolver);
    }
}

四、最后调用的位置,在Controller层传入参数添加LoginUserDTO userDTO,并在其前面添加@LoginUser的注解

    /**
     * 查询列表
     */
    @Log(name = "/query/list", description = "查询列表")
    @PostMapping(value = "/list")
    public BaseResponse list(@LoginUser LoginUserDTO userDTO, @RequestBody ArticleApproveQueryRequest req) {
        req.setLoginUserId(userDTO.getUserId());
        req.setLoginOrganizationId(1L);

        PageResponse<articleApproveQueryResponse> response = articleApproveService.list(req);
        return Results.success(response);
    }

完结–

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值