使用自定義參數(shù)注解獲取請求頭中的Authorization信息
背景
項(xiàng)目開發(fā)中將用戶信息加密后放在一個(gè)字符串中矮锈,返回給前端;前端將其放在 Authorization中歌溉,請求時(shí) 放在請求頭中检眯,為了使后端可以統(tǒng)一獲取俯逾,故采用自定義注解方式;
實(shí)現(xiàn)方式如下
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. 配置參數(shù)解析器
繼承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));
}
需要獲取用戶信息時(shí)儒拂,只需要在方法中 加上 @RequestUser
即可