|
@@ -0,0 +1,152 @@
|
|
1
|
+package com.example.demo.security;
|
|
2
|
+
|
|
3
|
+import ch.qos.logback.core.joran.conditional.ElseAction;
|
|
4
|
+import com.example.demo.config.HttpSessionConfig;
|
|
5
|
+import com.example.demo.config.entity.UserElement;
|
|
6
|
+import com.example.demo.constant.Constants;
|
|
7
|
+import com.example.demo.constant.Header;
|
|
8
|
+import com.example.demo.constant.Status;
|
|
9
|
+import com.example.demo.dto.RoleDTO;
|
|
10
|
+import com.example.demo.entity.ToSysMenu;
|
|
11
|
+import com.example.demo.service.IRoleService;
|
|
12
|
+import com.example.userserver.entity.User;
|
|
13
|
+import org.apache.commons.lang.StringUtils;
|
|
14
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
15
|
+import org.springframework.security.core.GrantedAuthority;
|
|
16
|
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
17
|
+import org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter;
|
|
18
|
+import org.springframework.util.AntPathMatcher;
|
|
19
|
+
|
|
20
|
+import javax.servlet.http.HttpServletRequest;
|
|
21
|
+import javax.servlet.http.HttpSession;
|
|
22
|
+import java.util.ArrayList;
|
|
23
|
+import java.util.List;
|
|
24
|
+import java.util.stream.Collectors;
|
|
25
|
+
|
|
26
|
+/**
|
|
27
|
+ * 预授权
|
|
28
|
+ * @author weiximei
|
|
29
|
+ */
|
|
30
|
+public class RestAbstractPreAuthenticatedProcessingFilter extends AbstractPreAuthenticatedProcessingFilter {
|
|
31
|
+
|
|
32
|
+ private AntPathMatcher matcher = new AntPathMatcher();
|
|
33
|
+
|
|
34
|
+ private IRoleService iRoleService;
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+ /** 用户类型请求登陆类型 **/
|
|
38
|
+ private String loginType;
|
|
39
|
+
|
|
40
|
+ /**
|
|
41
|
+ * 不需要权限访问的路径
|
|
42
|
+ */
|
|
43
|
+ private List<ToSysMenu> noneSecurity;
|
|
44
|
+
|
|
45
|
+ /**
|
|
46
|
+ * 获取主体信息
|
|
47
|
+ *
|
|
48
|
+ * 1.验证是否有权限
|
|
49
|
+ * 2.授权
|
|
50
|
+ *
|
|
51
|
+ * @param request
|
|
52
|
+ * @return
|
|
53
|
+ */
|
|
54
|
+ @Override
|
|
55
|
+ protected Object getPreAuthenticatedPrincipal(HttpServletRequest request) {
|
|
56
|
+ List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
|
|
57
|
+ // 不需要的拦截请求处理
|
|
58
|
+ if (isNoneSecurity(request.getRequestURI().toString()) || "OPTIONS".equals(request.getMethod())){
|
|
59
|
+ GrantedAuthority[] authorities = new GrantedAuthority[1];
|
|
60
|
+ GrantedAuthority roleNone = new SimpleGrantedAuthority(Constants.ROLE_NONE);
|
|
61
|
+ authorities[0]=roleNone;
|
|
62
|
+ grantedAuthorityList.add(authorities[0]);
|
|
63
|
+ return new TokenAuthrentication(grantedAuthorityList);
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ isInspection(request);
|
|
67
|
+
|
|
68
|
+ if (null == request.getAttribute(Header.HEADER_ERROR.getValue())){
|
|
69
|
+ try {
|
|
70
|
+ UserElement userElement = getUserElement(loginType, request);
|
|
71
|
+ List<RoleDTO> roleDTOList = iRoleService.getUserRoleByUserId(userElement.getId());
|
|
72
|
+ grantedAuthorityList = roleDTOList.stream()
|
|
73
|
+ .map(e->new SimpleGrantedAuthority(e.getRoleName().trim()))
|
|
74
|
+ .collect(Collectors.toList());
|
|
75
|
+ } catch (Exception e){
|
|
76
|
+ System.out.println("用户授权失败!");
|
|
77
|
+ e.printStackTrace();
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ }else {
|
|
81
|
+ // 校验有问题后, 需要给个角色, 让流程继续下去
|
|
82
|
+ GrantedAuthority[] authorities = new GrantedAuthority[1];
|
|
83
|
+ GrantedAuthority roleNone = new SimpleGrantedAuthority("ROLE_NONE");
|
|
84
|
+ authorities[0]=roleNone;
|
|
85
|
+ grantedAuthorityList.add(authorities[0]);
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ return new TokenAuthrentication(grantedAuthorityList);
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+ @Override
|
|
93
|
+ protected Object getPreAuthenticatedCredentials(HttpServletRequest httpServletRequest) {
|
|
94
|
+ return null;
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ /**
|
|
98
|
+ * 验证url是否是不需要权限访问
|
|
99
|
+ * @param url
|
|
100
|
+ * @return
|
|
101
|
+ */
|
|
102
|
+ private boolean isNoneSecurity(String url){
|
|
103
|
+ for (ToSysMenu sysMenu : noneSecurity) {
|
|
104
|
+ if (matcher.match(sysMenu.getMenuUrl(),url)){
|
|
105
|
+ return true;
|
|
106
|
+ }
|
|
107
|
+ }
|
|
108
|
+ return false;
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ /**
|
|
112
|
+ * 检验Token, App版本, 用户登录类型
|
|
113
|
+ * @param request
|
|
114
|
+ */
|
|
115
|
+ private void isInspection(HttpServletRequest request){
|
|
116
|
+ // Token检测
|
|
117
|
+ String token = request.getHeader(Header.REQUEST_X_AUTH_TOKEN.getValue()).trim();
|
|
118
|
+ if (StringUtils.isNotBlank(token)) {
|
|
119
|
+ request.setAttribute(Header.HEADER_ERROR.getValue(), Status.RESPONSE_STATUS_802.getValue());
|
|
120
|
+ }
|
|
121
|
+ // APP版本检测
|
|
122
|
+ String version = request.getHeader(Header.REQUEST_VERSION.getValue()).trim();
|
|
123
|
+ if (StringUtils.isBlank(version)){
|
|
124
|
+ request.setAttribute(Header.HEADER_ERROR.getValue(),Status.RESPONSE_STATUS_800.getValue());
|
|
125
|
+ }
|
|
126
|
+ // 用户登陆类型检测
|
|
127
|
+ loginType = ((String) request.getAttribute(Header.LOGIN_TYPE.getValue())).trim();
|
|
128
|
+ if (StringUtils.isBlank(loginType)) {
|
|
129
|
+ request.setAttribute(Header.HEADER_ERROR.getValue(),Status.RESPONSE_STATUS_801.getValue());
|
|
130
|
+ }
|
|
131
|
+ }
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+ /**
|
|
135
|
+ * 获取用户session信息
|
|
136
|
+ * @param loginType 用户类型
|
|
137
|
+ * @param request
|
|
138
|
+ * @return
|
|
139
|
+ */
|
|
140
|
+ public UserElement getUserElement(String loginType, HttpServletRequest request){
|
|
141
|
+ HttpSession session = request.getSession();
|
|
142
|
+ UserElement userElement = null;
|
|
143
|
+ if ("app".equals(loginType)) {
|
|
144
|
+ userElement = (UserElement) session.getAttribute(Constants.APP_USER_SESSION);
|
|
145
|
+ } else if ("web".equals(loginType)) {
|
|
146
|
+ userElement = (UserElement) session.getAttribute(Constants.WEB_USER_SESSION);
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ return userElement;
|
|
150
|
+ }
|
|
151
|
+
|
|
152
|
+}
|