yxh
7 天以前 68ff33beff11310e830b0615b5a81f83fad0f683
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package cn.lihu.jh.module.ecg.aspect;
 
import cn.lihu.jh.module.ecg.annotation.RequireBedOwner;
import cn.lihu.jh.module.ecg.service.room.RoomService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;
 
import javax.annotation.Resource;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
 
/**
 * 「必须是工位主人」的写操作鉴权切面(主/围观模型的核心)。
 *
 * <p>处理被 {@link RequireBedOwner} 标注的 Controller 方法:解析出 roomId / bedNo,
 * 交给 {@link RoomService#assertBedOwner(Long, String)} 校验;
 * 围观人或无座者调用写接口时直接抛 {@code ROOM_NOT_OWNER}("您处于围观模式,不能执行该操作")。
 *
 * <p><b>为什么解析不到就放行</b>:只带 rentId 之类的接口拿不到 roomId/bedNo,
 * 若一律拒绝会把正常业务也挡掉(例如确费)。这类接口记警告日志,并由业务自行校验。
 */
@Aspect
@Component
@Slf4j
public class BedOwnerAspect {
 
    @Resource
    private RoomService roomService;
 
    @Before("@annotation(requireBedOwner)")
    public void checkBedOwner(JoinPoint joinPoint, RequireBedOwner requireBedOwner) {
        Long roomId = null;
        String bedNo = null;
 
        Object[] args = joinPoint.getArgs();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Annotation[][] paramAnnotations = method.getParameterAnnotations();
 
        for (int i = 0; i < args.length; i++) {
            Object arg = args[i];
            if (null == arg) {
                continue;
            }
            // ① @RequestParam("roomId"/"bedNo") 直接参数
            Annotation[] annotations = (i < paramAnnotations.length) ? paramAnnotations[i] : null;
            if (null != annotations) {
                for (Annotation annotation : annotations) {
                    if (!(annotation instanceof RequestParam)) {
                        continue;
                    }
                    String name = ((RequestParam) annotation).value();
                    if (name.isEmpty()) {
                        name = ((RequestParam) annotation).name();
                    }
                    if ("roomId".equals(name) && arg instanceof Number) {
                        roomId = ((Number) arg).longValue();
                    } else if ("bedNo".equals(name)) {
                        bedNo = String.valueOf(arg);
                    }
                }
            }
            // ② 请求体 VO:反射读 getRoomId() / getBedNo()
            if (isSimpleValue(arg)) {
                continue;
            }
            if (null == roomId) {
                Object v = readGetter(arg, "getRoomId");
                if (v instanceof Number) {
                    roomId = ((Number) v).longValue();
                }
            }
            if (null == bedNo) {
                Object v = readGetter(arg, "getBedNo");
                if (null != v) {
                    bedNo = String.valueOf(v);
                }
            }
        }
 
        if (null == roomId || null == bedNo || bedNo.isEmpty() || "null".equals(bedNo)) {
            log.warn("[RequireBedOwner] 未能从参数解析 roomId/bedNo,跳过主人校验(该接口需自行校验). 接口={} 标记={}",
                    signature.getName(), requireBedOwner.value());
            return;
        }
        roomService.assertBedOwner(roomId, bedNo);
    }
 
    /** 简单类型(数字/字符串/集合等)不需要做 getter 反射 */
    private boolean isSimpleValue(Object arg) {
        return arg instanceof Number || arg instanceof CharSequence || arg instanceof Boolean
                || arg instanceof java.util.Collection || arg instanceof java.util.Map;
    }
 
    private Object readGetter(Object target, String getterName) {
        try {
            Method getter = target.getClass().getMethod(getterName);
            return getter.invoke(target);
        } catch (Exception e) {
            return null;
        }
    }
}