weiximei 6 年 前
コミット
a166646699

+ 3
- 3
CODE/smart-community/zuul/src/main/java/com/community/huiju/constant/RequestURI.java ファイルの表示

@@ -6,11 +6,11 @@ package com.community.huiju.constant;
6 6
  */
7 7
 public enum RequestURI {
8 8
 
9
-    APP_API("/app-api", "APP端请求路径"),
9
+    APP_API("/app-api/**", "APP端请求路径"),
10 10
 
11
-    OPERATE_API("/operate-api", "运营端请求路径"),
11
+    OPERATE_API("/operate-api/**", "运营端请求路径"),
12 12
 
13
-    PROPERTY_API("/property-api", "物业端请求路径");
13
+    PROPERTY_API("/property-api/**", "物业端请求路径");
14 14
 
15 15
 
16 16
     RequestURI(String url, String comment) {

+ 1
- 1
CODE/smart-community/zuul/src/main/java/com/community/huiju/filter/DomainZuulPostFilter.java ファイルの表示

@@ -22,7 +22,7 @@ import javax.servlet.http.HttpServletResponse;
22 22
  * 拦截Token
23 23
  * @author weiximei
24 24
  */
25
-@Component
25
+//@Component
26 26
 @Slf4j
27 27
 public class DomainZuulPostFilter extends ZuulFilter {
28 28
 

+ 16
- 2
CODE/smart-community/zuul/src/main/java/com/community/huiju/security/RestPreAuthenticateProvider.java ファイルの表示

@@ -2,12 +2,15 @@ package com.community.huiju.security;
2 2
 
3 3
 import com.community.commom.constant.Constant;
4 4
 import com.community.huiju.exception.WisdomSecurityException;
5
+import com.community.huiju.service.IRoleService;
6
+import lombok.extern.slf4j.Slf4j;
5 7
 import org.springframework.security.authentication.AuthenticationProvider;
6 8
 import org.springframework.security.core.Authentication;
7 9
 import org.springframework.security.core.AuthenticationException;
8 10
 import org.springframework.security.core.GrantedAuthority;
9 11
 import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
10 12
 
13
+import javax.servlet.http.HttpServletRequest;
11 14
 import java.util.Collection;
12 15
 import java.util.List;
13 16
 
@@ -16,12 +19,17 @@ import java.util.List;
16 19
  *
17 20
  * @author weiximei
18 21
  */
22
+@Slf4j
19 23
 public class RestPreAuthenticateProvider implements AuthenticationProvider {
20 24
 
25
+   private HttpServletRequest request;
26
+
27
+   private IRoleService iRoleService;
28
+
21 29
     @Override
22 30
     public Authentication authenticate(Authentication authentication) throws AuthenticationException {
23 31
 
24
-
32
+        log.info("provider session: {}", request.getSession().getId());
25 33
         TokenAuthrentication tokenAuthrentication = (TokenAuthrentication) authentication.getPrincipal();
26 34
         if (null != tokenAuthrentication) {
27 35
             Collection<GrantedAuthority> grantedAuthorityList = tokenAuthrentication.getAuthorities();
@@ -31,7 +39,8 @@ public class RestPreAuthenticateProvider implements AuthenticationProvider {
31 39
                     tokenAuthrentication.setAuthenticated(false);
32 40
                     return tokenAuthrentication;
33 41
                 }
34
-                tokenAuthrentication.setAuthenticated(true);
42
+                boolean hasPermission = iRoleService.hasPermission(request);
43
+                tokenAuthrentication.setAuthenticated(hasPermission);
35 44
                 return tokenAuthrentication;
36 45
             }
37 46
 
@@ -52,4 +61,9 @@ public class RestPreAuthenticateProvider implements AuthenticationProvider {
52 61
         return PreAuthenticatedAuthenticationToken.class.isAssignableFrom(authentication)
53 62
                 || TokenAuthrentication.class.isAssignableFrom(authentication);
54 63
     }
64
+
65
+    public RestPreAuthenticateProvider(HttpServletRequest request, IRoleService iRoleService) {
66
+        this.request = request;
67
+        this.iRoleService = iRoleService;
68
+    }
55 69
 }

+ 6
- 4
CODE/smart-community/zuul/src/main/java/com/community/huiju/security/WisdomSecurityConfig.java ファイルの表示

@@ -13,6 +13,7 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe
13 13
 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
14 14
 import org.springframework.security.config.http.SessionCreationPolicy;
15 15
 
16
+import javax.servlet.http.HttpServletRequest;
16 17
 import java.util.Arrays;
17 18
 
18 19
 /**
@@ -29,6 +30,9 @@ public class WisdomSecurityConfig extends WebSecurityConfigurerAdapter {
29 30
     @Autowired
30 31
     private SecurityParametes parametes;
31 32
 
33
+    @Autowired
34
+    private HttpServletRequest request;
35
+
32 36
     /**
33 37
      * 配置预授权
34 38
      * @return
@@ -49,12 +53,10 @@ public class WisdomSecurityConfig extends WebSecurityConfigurerAdapter {
49 53
     protected void configure(HttpSecurity http) throws Exception {
50 54
 
51 55
         http.csrf().disable();
52
-        http.authenticationProvider(new RestPreAuthenticateProvider())
56
+        http.authenticationProvider(new RestPreAuthenticateProvider(request, iRoleService))
53 57
                 .authorizeRequests()
54 58
                 .antMatchers(parametes.getNoneSercurityPath().toArray(new String[parametes.getNoneSercurityPath().size()])).permitAll() // 不要授权
55
-                .anyRequest()
56
-                .access("@iRoleService.hasPermission(httpServletRequest)")// 任何请求需要授权
57
-                .anyRequest()
59
+                .anyRequest()// 任何请求需要授权
58 60
                 .authenticated() // 需要一个身份
59 61
 //                .antMatchers(parametes.getAppSercurityPath().get(0).split("=")[1]).hasRole(parametes.getAppSercurityPath().get(0).split("=")[0])
60 62
 //                .antMatchers(parametes.getAppSercurityPath().get(1).split("=")[1]).hasRole(parametes.getAppSercurityPath().get(1).split("=")[0])

+ 6
- 6
CODE/smart-community/zuul/src/main/java/com/community/huiju/service/impl/RoleService.java ファイルの表示

@@ -113,20 +113,20 @@ public class RoleService  extends BaseController implements IRoleService {
113 113
         String token = request.getHeader(Header.REQUEST_X_AUTH_TOKEN.getValue());
114 114
         log.info("获取的token: {}", token);
115 115
 
116
-        if (requestURI.indexOf(RequestURI.APP_API.getUrl()) != -1) {
116
+        if (antPathMatcher.match(RequestURI.APP_API.getUrl(), requestURI)) {
117 117
             UserElement appElement = (UserElement) request.getSession().getAttribute(Constant.APP_USER_SESSION);
118 118
             if (null != appElement) {
119 119
                 // 这里先默认放行
120 120
                 hasPermission = true;
121 121
             }
122 122
 
123
-        } else if (requestURI.indexOf(RequestURI.OPERATE_API.getUrl()) != -1) {
123
+        } else if (antPathMatcher.match(RequestURI.OPERATE_API.getUrl(), requestURI)) {
124 124
             UserElement webOperateElement = (UserElement) request.getSession().getAttribute(Constant.WEB_USER_SESSION);
125 125
             if (null != webOperateElement) {
126 126
                 List<RoleDTO> roleDTOS = getWebOperateUserRoleByUserId(webOperateElement.getId());
127 127
                 hasPermission = hasMenuUrl(roleDTOS, RequestURI.OPERATE_API.getUrl(), requestURI);
128 128
             }
129
-        } else if (requestURI.indexOf(RequestURI.PROPERTY_API.getUrl()) != -1) {
129
+        } else if (antPathMatcher.match(RequestURI.PROPERTY_API.getUrl(), requestURI)) {
130 130
             UserElement webPropertyElement = (UserElement) request.getSession().getAttribute(Constant.WEB_USER_SESSION);
131 131
             if (null != webPropertyElement) {
132 132
                 List<RoleDTO> roleDTOS = getWebPropertyUserRoleUserId(webPropertyElement.getId());
@@ -149,9 +149,9 @@ public class RoleService  extends BaseController implements IRoleService {
149 149
         boolean hasMenuUrl = false;
150 150
         for (RoleDTO role : roleDTOS) {
151 151
             // 是 物业端/运营端/APP端
152
-            List<MenuDTO> urlList = RequestURI.OPERATE_API.getUrl().equals(apiUrl)
153
-                    ? getWebOperateMenuUrlByRoleId(role.getId()) : RequestURI.PROPERTY_API.getUrl().equals(apiUrl)
154
-                    ? getWebPropertyMenuUrlRoleId(role.getId())  : RequestURI.APP_API.getUrl().equals(apiUrl)
152
+            List<MenuDTO> urlList = antPathMatcher.match(RequestURI.OPERATE_API.getUrl(), requestURI)
153
+                    ? getWebOperateMenuUrlByRoleId(role.getId()) : antPathMatcher.match(RequestURI.PROPERTY_API.getUrl(), requestURI)
154
+                    ? getWebPropertyMenuUrlRoleId(role.getId())  : antPathMatcher.match(RequestURI.APP_API.getUrl(), requestURI)
155 155
                     ? Collections.EMPTY_LIST : Collections.EMPTY_LIST;
156 156
 
157 157
             for (MenuDTO menu : urlList) {