CurrentAdminMethodArgumentResolver.java
2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.xkl.authorization.resolvers;
import com.xkl.authorization.annotation.CurrentAdmin;
import com.xkl.config.Constants;
import com.xkl.controller.uploadsoft.UpSoftAccountController;
import com.xkl.controller.uspih.AdminAccountController;
import com.xkl.domain.XklAdminEntity;
import com.xkl.repository.AdminRepository;
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;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
/**
* 增加方法注入,将含有CurrentAdmin注解的方法参数注入当前登录用户
*
* @see CurrentAdmin
*/
@Component
public class CurrentAdminMethodArgumentResolver implements HandlerMethodArgumentResolver {
@Autowired
private AdminRepository adminRepository;
@Override
public boolean supportsParameter(MethodParameter parameter) {
//如果参数类型是Admin并且有CurrentAdmin注解则支持
if (parameter.getParameterType().isAssignableFrom(XklAdminEntity.class) &&
parameter.hasParameterAnnotation(CurrentAdmin.class)) {
return true;
}
return false;
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
//取出鉴权时存入的登录用户Id
String currentAdminId = ((String) webRequest.getAttribute(Constants.CURRENT_USER_ID, RequestAttributes.SCOPE_REQUEST)).
replace(UpSoftAccountController.UPSOFT_TOKEN_PREFIX, "").replace(AdminAccountController.USPIH_TOKEN_PREFIX, "").replace(Constants.ADMIN_TOKEN_PREFIX,"");
if (currentAdminId != null) {
//从数据库中查询并返回
XklAdminEntity admin = adminRepository.findByIdAndStatus(Long.parseLong(currentAdminId), Constants.STATUS_OK);
return admin;
}
throw new MissingServletRequestPartException(Constants.CURRENT_USER_ID);
}
}