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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.dy.common.aop;
 
import com.dy.common.contant.Constant;
import com.dy.common.multiDataSource.DataSourceContext;
import com.dy.common.webFilter.UserTokenContext;
import com.dy.common.webUtil.BaseResponseUtils;
import com.mysql.cj.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
 
import java.lang.reflect.Method;
import java.util.Objects;
 
@Slf4j
@Aspect
@Order(Constant.AspectOrderSsoAutho)
@Component
public class SsoPowerAspect {
 
    @Value("${pipIrr.global.dev}")
    public String isDevStage ;//是否为开发阶段
 
    @Value("${pipIrr.sso.checkUrl}")
    public String SsoCheckUrl ;
 
    private RestTemplate restTemplate;
 
    @Autowired
    public void setRestTemplate(RestTemplate restTemplate){
        this.restTemplate = restTemplate ;
    }
 
    @Pointcut("@annotation(com.dy.common.aop.SsoPowerAop)")
    public void ssoPowerPointCut() {
    }
 
    @Around("ssoPowerPointCut()")
    public Object execute(ProceedingJoinPoint point) throws Throwable {
        if(isDevStage != null && !isDevStage.trim().equals("") && isDevStage.trim().equalsIgnoreCase("true")){
            return point.proceed();
        }else{
            MethodSignature signature = (MethodSignature) point.getSignature();
            Method method = signature.getMethod();
            SsoPowerAop aop = method.getAnnotation(SsoPowerAop.class) ;
            if (Objects.nonNull(aop)){
                String power = aop.value() ;
                if(power.trim().equals("")){
                    power = aop.power() ;
                }
                if(power.trim().equals("-1")){
                    //不进行登录及权限验证
                    return point.proceed();
                }else{
                    String[] allPower = aop.ifAllPower() ;
                    String[] anyPower = aop.ifAnyPower() ;
                    String token = UserTokenContext.get() ;
                    Object rObj = this.check(token, power, allPower, anyPower);
                    if(rObj != null){
                        if(rObj instanceof SsoVo ssoVo){
                            if(ssoVo.logined){
                                if(ssoVo.hasPower){
                                    if(!StringUtils.isNullOrEmpty(ssoVo.dataSourceName)){
                                        DataSourceContext.set(ssoVo.dataSourceName);
                                        Object obj = point.proceed();
                                        DataSourceContext.remove();
                                        return obj ;
                                    }else{
                                        //无数据源
                                        return BaseResponseUtils.buildError("后端系统出错,未得到当前登录用户所属机构标签(数据源名)") ;
                                    }
                                }else{
                                    //无权限
                                    return BaseResponseUtils.buildNoPower() ;
                                }
                            }else{
                                //未登录
                                return BaseResponseUtils.buildToLogin() ;
                            }
                        }else{
                            return rObj ;
                        }
                    }else{
                        return BaseResponseUtils.buildError("后端系统出错,check方法返回null") ;
                    }
                }
            }else{
                //已经进入注解处理了,还得不到注解,这种情况是不可能的。
                return BaseResponseUtils.buildError("后端系统出错,DyAop注解为null") ;
            }
        }
    }
 
    /**
     * 调用SSO系统进行验证
     * @param token Header中的用户token
     * @param power 一个权限
     * @param allPower 多个权限
     * @param anyPower 多个权限
     * @return 返回对象
     */
    private Object check(String token, String power, String[] allPower, String[] anyPower){
        if(!StringUtils.isNullOrEmpty(SsoCheckUrl)){
            String url = UriComponentsBuilder.fromUriString(SsoCheckUrl)
                    .queryParam("token", token)
                    .queryParam("power", power)
                    .queryParam("allPower", (Object) allPower)
                    .queryParam("anyPower", (Object) anyPower)
                    .build()
                    .toUriString();
            HttpHeaders headers = new HttpHeaders();
            HttpEntity<?> httpEntity = new HttpEntity<>(headers);
            ResponseEntity<SsoVo> response = null;
            try {
                // 通过Get方式调用接口
                response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, SsoVo.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            assert response != null;
            return response.getBody();
        }else {
            return BaseResponseUtils.buildError("后端系统出错,未得到SsoCheckUrl");
        }
    }
 
}