springboot 从 token中获取 userID

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package io.github.lyr2000.dissertation.components;

import java.lang.annotation.*;

/**
 * @author lyr
 * @description 获取用户的 ID
 * @create 2021-11-20 14:14
 */
@Documented
@Inherited
@Retention(value = RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface UserId {
}
 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package io.github.lyr2000.dissertation.components;

import cn.hutool.core.util.StrUtil;
import io.github.lyr2000.common.shiro.JwtResult;
import io.github.lyr2000.common.shiro.entity.JwtToken;
import io.github.lyr2000.common.shiro.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
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.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author lyr
 * @description
 * @create 2021-11-20 18:33
 */
@Component
@Slf4j
public class UserIdResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(UserId.class);
    }

    @Autowired
    JwtUtil jwtUtil;

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
        String token = request.getHeader("token");
        if (StrUtil.isBlank(token)) {
            //parameter
            token = request.getParameter("token");
        }
        if (StrUtil.isBlank(token)) {
            // request attribute
            token = (String) request.getAttribute("token");
        }
        if (StrUtil.isBlank(token)) {
            return null;
        }
        try {
            JwtToken jwtToken = jwtUtil.decodeJwtToken(token);
            if (jwtToken == null || (jwtToken.getResult() != JwtResult.OK)) {
                return null;
            }
            //如果是错误的问题,直接返回 null
            Map<String, Object> data = jwtToken.getData();
            if (data == null) {
                return null;
            }
            Object id = data.get("id");
            if (id == null) {
                return null;
            }

            Class<?> clazz = parameter.getParameterType();
            if (clazz == int.class || clazz == Integer.class) {
                return Integer.parseInt(id.toString());
            } else if (clazz == Long.class || clazz == long.class) {
                return Long.parseLong(id.toString());
            } else if (clazz == String.class) {
                return id.toString();

            }
            return null;
        } catch (Exception e) {
            log.error("error msg = {}", e.getMessage());
        }
        return null;
        // return null;
    }
}