zhouhao 8 роки тому
батько
коміт
125d38a836

+ 0 - 4
hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/ListenerAuthorizingRealm.java

@@ -101,10 +101,6 @@ public class ListenerAuthorizingRealm extends AuthorizingRealm
     public void on(AuthorizationSuccessEvent event) {
         Authentication authentication = event.getAuthentication();
         boolean remember = Boolean.valueOf((String) event.getParameter("remember").orElse("false"));
-
-//        authentication.setAttribute(AuthorizationInfo.class.getName(), authorizationInfo);
-//        authentication.setAttribute(AuthenticationInfo.class.getName(), createAuthenticationInfo(authentication));
-
         Subject subject = SecurityUtils.getSubject();
         subject.login(new SimpleAuthenticationToken(authentication, remember));
     }

+ 24 - 17
hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/ShiroAutoConfiguration.java

@@ -29,6 +29,7 @@ import org.apache.shiro.session.mgt.DefaultSessionManager;
 import org.apache.shiro.spring.LifecycleBeanPostProcessor;
 import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
 import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
+import org.apache.shiro.web.mgt.WebSecurityManager;
 import org.hswebframework.web.authorization.AuthenticationHolder;
 import org.hswebframework.web.authorization.AuthenticationManager;
 import org.hswebframework.web.authorization.access.DataAccessController;
@@ -41,9 +42,7 @@ import org.hswebframework.web.authorization.shiro.cache.SpringCacheManagerWrappe
 import org.hswebframework.web.authorization.shiro.remember.SimpleRememberMeManager;
 import org.hswebframework.web.controller.message.ResponseMessage;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
+import org.springframework.boot.autoconfigure.condition.*;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
@@ -65,15 +64,12 @@ import java.util.List;
  * @author zhouhao
  */
 @Configuration
-@EnableConfigurationProperties(ShiroProperties.class)
+@Order(Ordered.LOWEST_PRECEDENCE)
 public class ShiroAutoConfiguration {
 
     @Autowired(required = false)
     private org.springframework.cache.CacheManager cacheManager;
 
-    @Autowired
-    private ShiroProperties shiroProperties;
-
     @Autowired(required = false)
     private List<DataAccessHandler> dataAccessHandlers;
 
@@ -161,16 +157,27 @@ public class ShiroAutoConfiguration {
         return advisor;
     }
 
-    @Bean(name = "shiroFilter")
-    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
-        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
-        // 必须设置 SecurityManager
-        shiroFilterFactoryBean.setSecurityManager(securityManager);
-        if (null != shiroProperties)
-            shiroFilterFactoryBean.setFilterChainDefinitionMap(shiroProperties.getFilters());
-        else
-            shiroFilterFactoryBean.setFilterChainDefinitionMap(Collections.emptyMap());
-        return shiroFilterFactoryBean;
+    @Configuration
+    @EnableConfigurationProperties(ShiroProperties.class)
+    @ConditionalOnProperty(prefix = "hsweb.authorize", name = "enable", havingValue = "true", matchIfMissing = true)
+    static class FilterConfiguration {
+        @Autowired
+        private ShiroProperties shiroProperties;
+
+        @Bean(name = "shiroFilter")
+        public ShiroFilterFactoryBean shiroFilterFactoryBean(WebSecurityManager securityManager) {
+            ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
+            // 必须设置 SecurityManager
+            shiroFilterFactoryBean.setSecurityManager(securityManager);
+            if (null != shiroProperties)
+                shiroFilterFactoryBean.setFilterChainDefinitionMap(shiroProperties.getFilters());
+            else
+                shiroFilterFactoryBean.setFilterChainDefinitionMap(Collections.emptyMap());
+            shiroFilterFactoryBean.setSuccessUrl(shiroProperties.getSuccessUrl());
+            shiroFilterFactoryBean.setLoginUrl(shiroProperties.getLoginUrl());
+            shiroFilterFactoryBean.setUnauthorizedUrl(shiroProperties.getUnauthorizedUrl());
+            return shiroFilterFactoryBean;
+        }
     }
 
     @RestControllerAdvice

+ 43 - 1
hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/ShiroProperties.java

@@ -17,6 +17,9 @@
 
 package org.hswebframework.web.authorization.shiro;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.InitializingBean;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 
 import java.util.Map;
@@ -27,11 +30,43 @@ import java.util.Map;
  * @author zhouhao
  */
 @ConfigurationProperties(prefix = "hsweb.authorize")
-public class ShiroProperties {
+public class ShiroProperties implements InitializingBean {
+    static Logger logger = LoggerFactory.getLogger(ShiroProperties.class);
+
     private Map<String, String> filters;
 
+    private String loginUrl = "/401.html";
+
+    private String unauthorizedUrl = "";
+
+    private String successUrl = "/";
+
     private boolean enable = true;
 
+    public String getLoginUrl() {
+        return loginUrl;
+    }
+
+    public void setLoginUrl(String loginUrl) {
+        this.loginUrl = loginUrl;
+    }
+
+    public String getUnauthorizedUrl() {
+        return unauthorizedUrl;
+    }
+
+    public void setUnauthorizedUrl(String unauthorizedUrl) {
+        this.unauthorizedUrl = unauthorizedUrl;
+    }
+
+    public String getSuccessUrl() {
+        return successUrl;
+    }
+
+    public void setSuccessUrl(String successUrl) {
+        this.successUrl = successUrl;
+    }
+
     public Map<String, String> getFilters() {
         return filters;
     }
@@ -47,4 +82,11 @@ public class ShiroProperties {
     public void setEnable(boolean enable) {
         this.enable = enable;
     }
+
+    @Override
+    public void afterPropertiesSet() throws Exception {
+        if (logger.isDebugEnabled() && null != filters) {
+            filters.forEach((k, v) -> logger.debug("path [{}] use filter [{}]", k, v));
+        }
+    }
 }

+ 4 - 1
hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/DefaultDataAccessController.java

@@ -12,9 +12,11 @@ import java.util.LinkedList;
 import java.util.List;
 
 /**
- * TODO 完成注释
+ * 默认的行级权限控制.通过获取DataAccessHandler进行实际处理
  *
  * @author zhouhao
+ * @see DataAccessHandler
+ * @since 3.0
  */
 public final class DefaultDataAccessController implements DataAccessController {
 
@@ -38,6 +40,7 @@ public final class DefaultDataAccessController implements DataAccessController {
     public boolean doAccess(DataAccessConfig access, MethodInterceptorParamContext params) {
         if (parent != null) parent.doAccess(access, params);
         return handlers.parallelStream()
+                // TODO: 17-3-28 可以换成access对应的handler以提高效率
                 .filter(handler -> handler.isSupport(access))
                 .anyMatch(handler -> handler.handle(access, params));
     }

+ 1 - 1
hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/handler/OwnCreatedDataAccessHandler.java

@@ -113,7 +113,7 @@ public class OwnCreatedDataAccessHandler implements DataAccessHandler {
             queryParamEntity.setTerms(new ArrayList<>());
             //添加一个查询条件
             queryParamEntity
-                    .where("creatorId", AuthenticationHolder.get().getUser().getId())
+                    .where(RecordCreationEntity.creatorId, AuthenticationHolder.get().getUser().getId())
                     //客户端提交的参数 作为嵌套参数
                     .nest().setTerms(oldParam);
         } else if (entity instanceof RecordCreationEntity) {

+ 3 - 0
hsweb-authorization/hsweb-authorization-shiro/src/main/resources/META-INF/spring.factories

@@ -0,0 +1,3 @@
+# Auto Configure
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+org.hswebframework.web.authorization.shiro.ShiroAutoConfiguration

+ 3 - 0
hsweb-examples/hsweb-examples-simple/src/main/resources/application.yml

@@ -11,6 +11,9 @@ spring:
     cache:
        type: simple
 hsweb:
+    authorize:
+#      filters:
+#           "/swagger-ui.html": "authc"
     app:
       name: hsweb示例
       version: 3.0.0