Spring Boot 自定义参数注解获取请求头信息

使用自定义参数注解获取请求头中的Authorization信息

背景

项目开发中将用户信息加密后放在一个字符串中,返回给前端;前端将其放在 Authorization中,请求时 放在请求头中,为了使后端可以统一获取,故采用自定义注解方式;
实现方式如下

1. 自定义注解 @RequestUser

import java.lang.annotation.Documented;
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)
@Documented
public @interface RequestUser {
}

2. 添加注解处理类 LoginInfoMethodArgumentResolver

import org.apache.commons.lang3.StringUtils;
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 LoginInfoMethodArgumentResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        if (methodParameter.getParameterType().isAssignableFrom(LoginInfo.class)
                && methodParameter.hasParameterAnnotation(RequestUser.class)) {
            return true;
        }
        return false;
    }
    
    /**
      * 获取请求头中的Authorization,处理之后返回用户信息
      */
    @Override
    public Object resolveArgument(MethodParameter methodParameter,
                                  ModelAndViewContainer modelAndViewContainer,
                                  NativeWebRequest nativeWebRequest,
                                  WebDataBinderFactory webDataBinderFactory) throws Exception {
        String authorization = nativeWebRequest.getHeader("Authorization");
        if (StringUtils.isEmpty(authorization)) {
            return null;
        }

        GetUserInfoByTokenRequest request = RequestFactory.createGetUserInfoByTokenRequest(authorization);
        GetUserInfoResponse response = PubClient.getInstance().excute(request);
        ResponseCheckUtil.check(response);

        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUserId(response.getUserInfo().getUserId());
        loginInfo.setUsername(StringUtils.isEmpty(response.getUserInfo().getRealName()) ?
                response.getUserInfo().getUserName() : response.getUserInfo().getRealName());
        loginInfo.setAreaId(response.getUserInfo().getAreaId());
        loginInfo.setToken(authorization);

        return loginInfo;
    }

3. 配置参数解析器

继承WebMvcConfigurationSupport

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

import java.util.List;

@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport {
    @Override
    protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new LoginInfoMethodArgumentResolver());
    }
}

4. 使用方法

 @PostMapping(path = "/createOfflineOrder", consumes = "application/json", produces = "application/json; charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "新增线下充值订单接口")
    public ResponseEntity<RestResponse<String>> createOfflineOrder(@RequestBody CreateOrderRequest orderRequest,
                                                                   @RequestUser LoginInfo loginInfo) {
        String userCity = "";
        if (Objects.nonNull(loginInfo.getAreaId())) {
            SysAreaVo areaVo = getSysArea(loginInfo.getAreaId().toString(), loginInfo.getToken());
            userCity = areaVo.getSysAreaName();
        }
        String orderNo = agencyOrderService.createOfflineOrder(new BigDecimal(orderRequest.getAmount()), orderRequest.getUserId(), orderRequest.getUserName(),
                orderRequest.getTransNo(), orderRequest.getPayTime(), new BussinessManager().setId(loginInfo.getUserId()).setName(loginInfo.getUsername()).setUserCity(userCity));
        return ResponseEntity.ok(RestResponse.buildSuccessResp(orderNo));
    }

需要获取用户信息时,只需要在方法中 加上 @RequestUser 即可