Skip to content

Commit 202a935

Browse files
committed
完善部分注释
1 parent 8225290 commit 202a935

File tree

31 files changed

+321
-68
lines changed

31 files changed

+321
-68
lines changed

hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/AuthorizationHolder.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
1+
/*
2+
* Copyright 2016 http://www.hswebframework.org
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*
17+
*/
18+
119
package org.hswebframework.web.authorization;
220

321
/**
4-
* TODO 完成注释
22+
* 权限获取器,用于静态方式获取当前登录用户的权限信息
523
*
624
* @author zhouhao
25+
* @see AuthorizationSupplier
26+
* @since 3.0
727
*/
828
public final class AuthorizationHolder {
929
private static AuthorizationSupplier supplier;
1030

1131
public static Authorization get() {
32+
if (null == supplier) {
33+
throw new UnsupportedOperationException("AuthorizationSupplier is null!");
34+
}
1235
return supplier.get();
1336
}
1437

hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/AuthorizationSupplier.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
import java.util.function.Supplier;
2121

2222
/**
23-
* TODO 完成注释
24-
*
2523
* @author zhouhao
24+
* @see Supplier
25+
* @see Authorization
26+
* @see AuthorizationHolder
2627
*/
2728
public interface AuthorizationSupplier extends Supplier<Authorization> {
2829
}

hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/Role.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,20 @@
2020
import java.io.Serializable;
2121

2222
/**
23-
* TODO 完成注释
23+
* 角色信息
2424
*
2525
* @author zhouhao
2626
*/
2727
public interface Role extends Serializable {
28+
29+
/**
30+
* @return 角色ID
31+
*/
2832
String getId();
2933

34+
/**
35+
*
36+
* @return 角色名
37+
*/
3038
String getName();
3139
}

hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/User.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,24 @@
2020
import java.io.Serializable;
2121

2222
/**
23-
* TODO 完成注释
23+
* 用户信息
2424
*
2525
* @author zhouhao
26+
* @since 3.0
2627
*/
2728
public interface User extends Serializable {
29+
/**
30+
* @return 用户ID
31+
*/
2832
String getId();
2933

34+
/**
35+
* @return 用户名
36+
*/
3037
String getUsername();
3138

39+
/**
40+
* @return 姓名
41+
*/
3242
String getName();
3343
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package org.hswebframework.web.authorization.access;
22

33
/**
4-
* TODO 完成注释
5-
*
64
* @author zhouhao
75
* @see DataAccess.Type#CUSTOM
86
*/
97
public interface CustomDataAccess extends DataAccess {
108
DataAccessController getController();
9+
10+
default String getType() {
11+
return Type.CUSTOM.name();
12+
}
1113
}

hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/access/DataAccess.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
* Copyright 2016 http://www.hswebframework.org
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*
17+
*/
18+
119
package org.hswebframework.web.authorization.access;
220

321

@@ -6,7 +24,8 @@
624
import java.io.Serializable;
725

826
/**
9-
* 数据级的权限控制
27+
* 数据级的权限控制,此接口为控制方式配置
28+
* 具体的控制逻辑由控制器{@link DataAccessController}实现
1029
*
1130
* @author zhouhao
1231
* @see org.hswebframework.web.authorization.access.CustomDataAccess
@@ -27,6 +46,17 @@ public interface DataAccess extends Serializable {
2746
*/
2847
String getAction();
2948

49+
/**
50+
* 控制方式标识
51+
*
52+
* @return 控制方式
53+
* @see Type#name()
54+
*/
55+
String getType();
56+
57+
/**
58+
* 内置3中控制方式
59+
*/
3060
enum Type {
3161
OWN_CREATED("自己创建的数据"),
3262
SCRIPT("脚本"),
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package org.hswebframework.web.authorization.access;
22

33
/**
4-
* TODO 完成注释
4+
* 数据级别权限控制器,通过此控制器对当前登录用户进行的操作进行数据级别的权限控制。
5+
* 如:A用户只能查询自己创建的B数据,A用户只能修改自己创建的B数据
56
*
67
* @author zhouhao
8+
* @see 3.0
79
*/
810
public interface DataAccessController {
11+
/**
12+
* 执行权限控制
13+
* @param access 控制方式以及配置
14+
* @param params 当前操作的方法的参数上下文
15+
* @return
16+
*/
917
boolean doAccess(DataAccess access, ParamContext params);
1018
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
package org.hswebframework.web.authorization.access;
22

33
/**
4-
* TODO 完成注释
4+
* 数据级别权限控制处理器接口,负责处理支持的权限控制配置
55
*
66
* @author zhouhao
77
*/
88
public interface DataAccessHandler {
99

10+
/**
11+
* 是否支持处理此配置
12+
*
13+
* @param access 控制配置
14+
* @return 是否支持
15+
*/
1016
boolean isSupport(DataAccess access);
1117

12-
boolean doAccess(DataAccess access, ParamContext context);
18+
/**
19+
* 执行处理,返回处理结果
20+
*
21+
* @param access 控制配置
22+
* @param context 参数上下文
23+
* @return 处理结果
24+
*/
25+
boolean handle(DataAccess access, ParamContext context);
1326
}

hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/access/OwnCreatedDataAccess.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@
66
* @author zhouhao
77
*/
88
public interface OwnCreatedDataAccess extends DataAccess {
9+
default String getType() {
10+
return Type.OWN_CREATED.name();
11+
}
912
}

hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/access/ParamContext.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,57 @@
22

33
import java.io.Serializable;
44
import java.lang.annotation.Annotation;
5+
import java.lang.reflect.Method;
56
import java.util.Map;
67
import java.util.Optional;
78

89
/**
9-
* TODO 完成注释
10+
* 参数上下文,用于获取当前进行操作的方法的各种参数信息,如:当前所在类实例,参数集合,注解
1011
*
1112
* @author zhouhao
13+
* @see 3.0
1214
*/
1315
public interface ParamContext extends Serializable {
1416

17+
/**
18+
* 获取当前类实例
19+
*
20+
* @return 类实例对象
21+
*/
1522
Object getTarget();
1623

24+
/**
25+
* 当前操作的方法
26+
*
27+
* @return 方法实例
28+
*/
29+
Method getMethod();
30+
31+
/**
32+
* 根据参数名获取参数值,此参数为方法的参数,而非http参数 <br/>
33+
* 如:当前被操作的方法为 query(QueryParam param); 调用getParameter("param"); 则返回QueryParam实例<br/>
34+
* 注意:返回值为Optional对象,使用方法见{@link Optional}<br/>
35+
*
36+
* @param name 参数名称
37+
* @param <T> 参数泛型
38+
* @return Optional
39+
*/
1740
<T> Optional<T> getParameter(String name);
1841

19-
<T extends Annotation> T getAnnotation();
42+
/**
43+
* 获取当前操作方法或实例上指定类型的泛型,如果方法上未获取到,则获取实例类上的注解。实例类上未获取到,则返回null
44+
*
45+
* @param type 注解的类型
46+
* @param <T> 注解泛型
47+
* @return 注解
48+
*/
49+
<T extends Annotation> T getAnnotation(Class<T> type);
2050

51+
/**
52+
* 获取全部参数
53+
*
54+
* @return 参数集合
55+
* @see this#getParameter(String)
56+
*/
2157
Map<String, Object> getParams();
2258
}

hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/access/ScriptDataAccess.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
* @author zhouhao
77
*/
88
public interface ScriptDataAccess extends DataAccess {
9+
default String getType() {
10+
return Type.CUSTOM.name();
11+
}
912

1013
/**
1114
* 脚本语言: javascript(js),groovy

hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/DataAccessAnnotationMethodInterceptor.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.shiro.authz.aop.AuthorizingAnnotationMethodInterceptor;
2323
import org.hsweb.expands.script.engine.DynamicScriptEngine;
2424
import org.hsweb.expands.script.engine.DynamicScriptEngineFactory;
25+
import org.hswebframework.web.ApplicationContextHolder;
2526
import org.hswebframework.web.BusinessException;
2627
import org.hswebframework.web.authorization.Authorization;
2728
import org.hswebframework.web.authorization.AuthorizationHolder;
@@ -45,9 +46,14 @@
4546
import java.util.stream.Collectors;
4647

4748
/**
48-
* TODO 完成注释
49+
* 数据级权限控制实现 <br/>
50+
* 通过在方法上注解{@link RequiresDataAccess},标识需要进行数据级权限控制<br/>
51+
* 控制的方式和规则由 {@link Permission#getDataAccesses()}实现<br/>
4952
*
5053
* @author zhouhao
54+
* @see DefaultDataAccessController
55+
* @see DataAccessAnnotationHandler#assertAuthorized(Annotation)
56+
* @see 3.0
5157
*/
5258
public class DataAccessAnnotationMethodInterceptor extends AuthorizingAnnotationMethodInterceptor {
5359

@@ -75,8 +81,14 @@ public void assertAuthorized(Annotation a) throws AuthorizationException {
7581
logger.warn("MethodInterceptorHolder is null!");
7682
return;
7783
}
84+
//无权限信息
85+
Authorization authorization = AuthorizationHolder.get();
86+
if (authorization == null) {
87+
throw new AuthorizationException("{no_authorization}");
88+
}
7889
RequiresDataAccess accessAnn = ((RequiresDataAccess) a);
7990
DataAccessController accessController = dataAccessController;
91+
//在注解上自定义的权限控制器
8092
if (DataAccessController.class != accessAnn.controllerClass()) {
8193
if (null == (accessController = cache.get(accessAnn.controllerClass()))) {
8294
synchronized (cache) {
@@ -89,30 +101,29 @@ public void assertAuthorized(Annotation a) throws AuthorizationException {
89101
}
90102
}
91103
}
92-
} else if (StringUtils.isNullOrEmpty(accessAnn.controllerBeanName())) {
93-
// TODO: 17-2-8 get controller from spring context
104+
} else if (!StringUtils.isNullOrEmpty(accessAnn.controllerBeanName())) {
105+
//获取spring上下文中的控制器
106+
accessController = ApplicationContextHolder.get().getBean(accessAnn.controllerBeanName(), DataAccessController.class);
94107
}
95108
DataAccessController finalAccessController = accessController;
96109

97-
ParamContext context = holder.createParamContext(accessAnn);
98-
Authorization authorization = AuthorizationHolder.get();
99-
if (authorization == null) {
100-
throw new AuthorizationException("{no_authorization}");
101-
}
110+
ParamContext context = holder.createParamContext();
102111
String permission = accessAnn.permission();
103112
Permission permissionInfo = authorization.getPermission(permission);
104113
List<String> actionList = Arrays.asList(accessAnn.action());
105-
114+
//取得当前登录用户持有的控制规则
106115
Set<DataAccess> accesses = permissionInfo
107116
.getDataAccesses()
108117
.stream()
109118
.filter(access -> actionList.contains(access.getAction()))
110119
.collect(Collectors.toSet());
120+
//无规则,则代表不进行控制
111121
if (accesses.isEmpty()) return;
122+
//单个规则验证函数
112123
Function<Predicate<DataAccess>, Boolean> function =
113-
(accessAnn.logical() == Logical.AND) ?
124+
accessAnn.logical() == Logical.AND ?
114125
accesses.stream()::allMatch : accesses.stream()::anyMatch;
115-
126+
//调用控制器进行验证
116127
boolean isAccess = function.apply(access -> finalAccessController.doAccess(access, context));
117128
if (!isAccess) {
118129
throw new AuthorizationException("{access_deny}");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public boolean doAccess(DataAccess access, ParamContext params) {
3939
if (parent != null) parent.doAccess(access, params);
4040
return handlers.parallelStream()
4141
.filter(handler -> handler.isSupport(access))
42-
.anyMatch(handler -> handler.doAccess(access, params));
42+
.anyMatch(handler -> handler.handle(access, params));
4343
}
4444

4545
public DefaultDataAccessController addHandler(DataAccessHandler handler) {

hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/FieldAccessAnnotationMethodInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void assertAuthorized(Annotation a) throws AuthorizationException {
6565
return;
6666
}
6767
RequiresFieldAccess accessAnn = ((RequiresFieldAccess) a);
68-
ParamContext context = holder.createParamContext(accessAnn);
68+
ParamContext context = holder.createParamContext();
6969
Authorization authorization = AuthorizationHolder.get();
7070
if (authorization == null) {
7171
throw new AuthorizationException("{no_authorization}");

0 commit comments

Comments
 (0)