54 lines
1.9 KiB
Java
54 lines
1.9 KiB
Java
/**
|
||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||
*
|
||
* https://www.renren.io
|
||
*
|
||
* 版权所有,侵权必究!
|
||
*/
|
||
|
||
package com.peanut.modules.app.resolver;
|
||
|
||
import com.peanut.modules.app.annotation.LoginUser;
|
||
import com.peanut.modules.app.interceptor.AuthorizationInterceptor;
|
||
import com.peanut.modules.common.entity.MyUserEntity;
|
||
import com.peanut.modules.book.service.MyUserService;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.core.MethodParameter;
|
||
import org.springframework.stereotype.Component;
|
||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||
import org.springframework.web.context.request.NativeWebRequest;
|
||
import org.springframework.web.context.request.RequestAttributes;
|
||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||
|
||
/**
|
||
* 有@LoginUser注解的方法参数,注入当前登录用户
|
||
*
|
||
* @author Mark sunlightcs@gmail.com
|
||
*/
|
||
@Component
|
||
public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
|
||
@Autowired
|
||
private MyUserService userService;
|
||
|
||
@Override
|
||
public boolean supportsParameter(MethodParameter parameter) {
|
||
return parameter.getParameterType().isAssignableFrom(MyUserEntity.class) && parameter.hasParameterAnnotation(LoginUser.class);
|
||
}
|
||
|
||
@Override
|
||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container,
|
||
NativeWebRequest request, WebDataBinderFactory factory) throws Exception {
|
||
//获取用户ID
|
||
Object object = request.getAttribute(AuthorizationInterceptor.USER_KEY, RequestAttributes.SCOPE_REQUEST);
|
||
if(object == null){
|
||
return null;
|
||
}
|
||
|
||
//获取用户信息
|
||
MyUserEntity user = userService.getById((Long)object);
|
||
|
||
return user;
|
||
}
|
||
}
|