Controller返回结果拦截器

Controller返回结果拦截器

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
package com.gosun.common.config;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gosun.common.dao.UserDao;
import com.gosun.common.entity.ChildPlatform;
import com.gosun.common.entity.Role;
import com.gosun.common.entity.User;
import com.gosun.common.feign.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@ControllerAdvice
public class ResponseBodyAnalysis implements ResponseBodyAdvice {

@Autowired
UserFeignClient userFeignClient;
@Autowired
UserDao userDao;

@Override
public Object beforeBodyWrite(Object body, MethodParameter arg1,
MediaType arg2, Class arg3, ServerHttpRequest arg4,
ServerHttpResponse arg5) {
if(!arg4.getURI().getPath().contains("oauth/token")
&& !arg4.getURI().getPath().contains("oauth/check_token")
&& !arg4.getURI().getPath().contains("swagger-resources")
&& !arg4.getURI().getPath().contains("api-docs")
&& !arg4.getURI().getPath().contains("error")){
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Map map = (Map)body;
JSONObject jsonObject = new JSONObject(map);
JSONArray jsonArray = jsonObject.getJSONArray("data");
if(jsonArray != null){
Set<ChildPlatform> childPlatforms = findChildPlatformAuthority((String) authentication.getPrincipal());
for(int i=0;i<jsonArray.size();i++){
JSONObject jsonData = jsonArray.getJSONObject(i);
if(jsonData.getString("childPlatCode") != null){
String childPlatCode = jsonData.getString("childPlatCode");
Boolean flag = false;
for(ChildPlatform childPlatform:childPlatforms){
if(childPlatform.getChildPlatCode().equals(childPlatCode)){
flag = true;
}
}
if(!flag){
jsonArray.remove(jsonData);
}
}
}
}
}
return body;
}

@Override
public boolean supports(MethodParameter arg0, Class arg1) {
return true;
}

Set<ChildPlatform> findChildPlatformAuthority(String userName){
try {
User user = userDao.findByUsername(userName);
Set<Role> roles = user.getRoles();
Set<ChildPlatform> resultSet = new HashSet<>();
for(Role role:roles){
resultSet.addAll(role.getChildPlatforms());
}
return resultSet;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

人生两苦:想要却不得,拥有却失去。 –褚禄山
珍惜当下,与君共勉~


本文标题:Controller返回结果拦截器

文章作者:fufua

发布时间:2019年05月27日 - 10:54:13

最后更新:2019年05月27日 - 20:38:24

原始链接:https://qq1028951741.github.io/2019/05/27/Controller返回结果拦截器/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

this is end, thank you for reading