使用自定义注解实现登录用户获取

  • 业务功能场景
    • 在代码程序中很多接口的调用会有限制,需要用户在登录状态下才能进行操作(需要众多用户的信息等),这里就使用注解在调用接口的时候进行验证拦截。

自定义一个注解

package com.timothy.annotation;

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

/**
 * 自定义注解-验证是否登录
 */
//定义注解使用于参数上
@Target(ElementType.PARAMETER)
//定义注解在运行时生效
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginUser {
    
}

定义一个类去实现 HandlerMethodArgumentResolver 并重写其方法

 

package com.timothy.annotation.support;

import com.timothy.annotation.LoginUser;
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;

/**
 *
 *
 */
public class LoginUserHandlerMethodArgumentResolver implements
        HandlerMethodArgumentResolver {
    public static final String LOGIN_TOKEN_KEY = "X-Login-Token";


    /**
     * 判断是否支持要转换的参数类型
     */
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.getParameterType().isAssignableFrom(Integer.class)&&methodParameter.hasParameterAnnotation(LoginUser.class);
    }

    /**
     * 当支持后进行相应的转换 做业务操作
     */
    @Override
    public Object resolveArgument(MethodParameter methodParameter,
                                  ModelAndViewContainer modelAndViewContainer,
                                  NativeWebRequest request,
                                  WebDataBinderFactory webDataBinderFactory) throws Exception {
        //从请求头中获参数信息
        String token = request.getHeader(LOGIN_TOKEN_KEY);
        if(token == null || token.isEmpty()){
            return "参数缺少token";
        }
        //若存在token可以从 redis 或 自定义的工具类中获取userId(必须返回值与使用注解参数同类型)
        return 1;
    }
}

自定义一个配置类把上述交给spring管理

package com.timothy.config;

import com.timothy.annotation.support.LoginUserHandlerMethodArgumentResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * Created by wo on 2018/9/17.
 */
@Configuration
public class UserConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new LoginUserHandlerMethodArgumentResolver());
    }

}

在controller中使用即可

    @PostMapping("/add")
    public String loginInterface(@LoginUser Integer userId){
        //注解返回userId
        return "success";
    }

 

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值