Skip to content

Commit 4de4861

Browse files
committed
优化说明
1 parent e5df1e8 commit 4de4861

File tree

24 files changed

+767
-45
lines changed

24 files changed

+767
-45
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
import java.lang.annotation.*;
66

7+
/**
8+
* @deprecated 已弃用
9+
*/
710
@DataAccessType(id = "FIELD_DENY", name = "字段权限")
811
@Retention(RetentionPolicy.RUNTIME)
912
@Documented
1013
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
14+
@Deprecated
1115
public @interface FieldDataAccess {
1216

1317
@AliasFor(annotation = DataAccessType.class)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,101 @@
11
package org.hswebframework.web.authorization.annotation;
22

33

4+
import org.hswebframework.web.authorization.Permission;
45
import org.hswebframework.web.authorization.define.Phased;
56

67
import java.lang.annotation.*;
78

9+
/**
10+
* 接口资源声明注解,声明Controller的资源相关信息,用于进行权限控制。
11+
* <br>
12+
* 在Controller进行注解,表示此接口需要有对应的权限{@link Permission#getId()}才能进行访问.
13+
* 具体的操作权限控制,需要在方法上注解{@link ResourceAction}.
14+
* <br>
15+
*
16+
*
17+
* <pre>{@code
18+
* @RestController
19+
* //声明资源
20+
* @Resource(id = "test", name = "测试功能")
21+
* public class TestController implements ReactiveCrudController<TestEntity, String> {
22+
*
23+
* //声明操作,需要有 test:query 权限才能访问此接口
24+
* @QueryAction
25+
* public Mono<User> getUser() {
26+
* return Authentication.currentReactive()
27+
* .switchIfEmpty(Mono.error(new UnAuthorizedException()))
28+
* .map(Authentication::getUser);
29+
* }
30+
*
31+
* }
32+
* }
33+
* </pre>
34+
* 如果接口不需要进行权限控制,可注解{@link Authorize#ignore()}来标识此接口不需要权限控制.
35+
* 或者通过监听 {@link org.hswebframework.web.authorization.events.AuthorizingHandleBeforeEvent}来进行自定义处理
36+
* <pre>{@code
37+
* @EventListener
38+
* public void handleAuthEvent(AuthorizingHandleBeforeEvent e) {
39+
* //admin用户可以访问全部操作
40+
* if ("admin".equals(e.getContext().getAuthentication().getUser().getUsername())) {
41+
* e.setAllow(true);
42+
* }
43+
* }
44+
* }</pre>
45+
*
46+
* @author zhouhao
47+
* @see ResourceAction
48+
* @see Authorize
49+
* @see org.hswebframework.web.authorization.events.AuthorizingHandleBeforeEvent
50+
* @since 4.0
51+
*/
852
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.TYPE})
953
@Retention(RetentionPolicy.RUNTIME)
1054
@Inherited
1155
@Documented
1256
public @interface Resource {
57+
58+
/**
59+
* 资源ID
60+
*
61+
* @return 资源ID
62+
*/
1363
String id();
1464

65+
/**
66+
* @return 资源名称
67+
*/
1568
String name();
1669

70+
/**
71+
* @return 资源操作定义
72+
*/
1773
ResourceAction[] actions() default {};
1874

75+
/**
76+
* @return 多个操作控制逻辑
77+
*/
1978
Logical logical() default Logical.DEFAULT;
2079

80+
/**
81+
* @return 权限控制阶段
82+
*/
2183
Phased phased() default Phased.before;
2284

85+
/**
86+
* @return 资源描述
87+
*/
2388
String[] description() default {};
2489

90+
/**
91+
* @return 资源分组
92+
*/
2593
String[] group() default {};
2694

95+
/**
96+
* 如果在方法上设置此属性,表示是否合并类上注解的属性
97+
*
98+
* @return 是否合并
99+
*/
27100
boolean merge() default true;
28101
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,65 @@
11
package org.hswebframework.web.authorization.annotation;
22

3-
import org.hswebframework.web.authorization.define.Phased;
3+
4+
import org.hswebframework.web.authorization.Permission;
45

56
import java.lang.annotation.*;
67

78
/**
9+
* 对资源操作的描述,通常用来进行权限控制.
10+
* <p>
11+
* 在Controller方法上添加此注解,来声明根据权限操作{@link Permission#getActions()}进行权限控制.
12+
* <p>
13+
* 可以使用注解继承的方式来统一定义操作:
14+
* <pre>{@code
15+
* @Target(ElementType.METHOD)
16+
* @Retention(RetentionPolicy.RUNTIME)
17+
* @Inherited
18+
* @Documented
19+
* @ResourceAction(id = "create", name = "新增")
20+
* public @interface CreateAction {
21+
*
22+
* }
23+
* }
24+
* </pre>
25+
*
826
* @see CreateAction
27+
* @see DeleteAction
28+
* @see SaveAction
29+
* @see org.hswebframework.web.authorization.Authentication
30+
* @see Permission#getActions()
931
*/
1032
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
1133
@Retention(RetentionPolicy.RUNTIME)
1234
@Inherited
1335
@Documented
1436
public @interface ResourceAction {
37+
/**
38+
* 操作标识
39+
*
40+
* @return 操作标识
41+
* @see Permission#getActions()
42+
*/
1543
String id();
1644

45+
/**
46+
* @return 操作名称
47+
*/
1748
String name();
1849

50+
/**
51+
* @return 操作说明
52+
*/
1953
String[] description() default {};
2054

55+
/**
56+
* @return 多个操作时的判断逻辑
57+
*/
2158
Logical logical() default Logical.DEFAULT;
2259

60+
/**
61+
* @deprecated 已弃用, 4.1中移除
62+
*/
63+
@Deprecated
2364
DataAccess[] dataAccess() default @DataAccess(ignore = true);
2465
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@
55

66
import java.lang.annotation.*;
77

8+
/**
9+
* 继承{@link ResourceAction},提供统一的id定义
10+
*
11+
* @author zhouhao
12+
* @since 4.0
13+
*/
814
@Target(ElementType.METHOD)
915
@Retention(RetentionPolicy.RUNTIME)
1016
@Inherited
1117
@Documented
1218
@ResourceAction(id = Permission.ACTION_SAVE, name = "保存")
1319
public @interface SaveAction {
1420

15-
@AliasFor(annotation = ResourceAction.class,attribute = "dataAccess")
21+
@Deprecated
22+
@AliasFor(annotation = ResourceAction.class, attribute = "dataAccess")
1623
DataAccess dataAccess() default @DataAccess(ignore = true);
1724
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
/**
66
* 声明某个操作支持用户查看自己的数据
7+
*
8+
* @deprecated 已弃用
79
*/
810
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
911
@Retention(RetentionPolicy.RUNTIME)
1012
@Inherited
1113
@Documented
1214
@DataAccessType(id = "user_own_data", name = "用户自己的数据")
15+
@Deprecated
1316
public @interface UserOwnData {
1417

1518
}

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44
import org.hswebframework.web.authorization.define.HandleType;
55
import org.springframework.context.ApplicationEvent;
66

7+
/**
8+
* 权限控制事件,在进行权限控制之前会推送此事件,用于自定义权限控制结果:
9+
* <pre>{@code
10+
* @EventListener
11+
* public void handleAuthEvent(AuthorizingHandleBeforeEvent e) {
12+
* //admin用户可以访问全部操作
13+
* if ("admin".equals(e.getContext().getAuthentication().getUser().getUsername())) {
14+
* e.setAllow(true);
15+
* }
16+
* }
17+
* }</pre>
18+
*
19+
* @author zhouhao
20+
* @since 4.0
21+
*/
22+
// TODO: 2021/12/21 Reactive支持
723
public class AuthorizingHandleBeforeEvent extends ApplicationEvent implements AuthorizationEvent {
824

925
private static final long serialVersionUID = -1095765748533721998L;
@@ -14,7 +30,7 @@ public class AuthorizingHandleBeforeEvent extends ApplicationEvent implements Au
1430

1531
private String message;
1632

17-
private HandleType handleType;
33+
private final HandleType handleType;
1834

1935
public AuthorizingHandleBeforeEvent(AuthorizingContext context, HandleType handleType) {
2036
super(context);
@@ -33,6 +49,11 @@ public boolean isAllow() {
3349
return allow;
3450
}
3551

52+
/**
53+
* 设置通过当前请求
54+
*
55+
* @param allow allow
56+
*/
3657
public void setAllow(boolean allow) {
3758
execute = false;
3859
this.allow = allow;
@@ -42,11 +63,18 @@ public String getMessage() {
4263
return message;
4364
}
4465

66+
/**
67+
* 设置错误提示消息
68+
*
69+
* @param message 消息
70+
*/
4571
public void setMessage(String message) {
4672
this.message = message;
4773
}
4874

49-
75+
/**
76+
* @return 权限控制类型
77+
*/
5078
public HandleType getHandleType() {
5179
return handleType;
5280
}

hsweb-commons/hsweb-commons-api/src/main/java/org/hswebframework/web/api/crud/entity/Entity.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,48 @@
3232
*/
3333
public interface Entity extends Serializable {
3434

35+
/**
36+
* 使用jsr303对当前实体类进行验证,如果未通过验证则会抛出{@link org.hswebframework.web.exception.ValidationException}异常
37+
*
38+
* @param groups 分组
39+
* @see org.hswebframework.web.exception.ValidationException
40+
*/
3541
default void tryValidate(Class<?>... groups) {
3642
ValidatorUtils.tryValidate(this, groups);
3743
}
3844

45+
/**
46+
* 将当前实体类复制到指定其他类型中,类型将会被自动实例化,在类型明确时,建议使用{@link Entity#copyFrom(Object, String...)}.
47+
*
48+
* @param target 目标类型
49+
* @param ignoreProperties 忽略复制的属性
50+
* @param <T>类型
51+
* @return 复制结果
52+
*/
3953
default <T> T copyTo(Class<T> target, String... ignoreProperties) {
4054
return FastBeanCopier.copy(this, target, ignoreProperties);
4155
}
4256

57+
/**
58+
* 将当前实体类复制到其他对象中
59+
*
60+
* @param target 目标实体
61+
* @param ignoreProperties 忽略复制的属性
62+
* @param <T>类型
63+
* @return 复制结果
64+
*/
4365
default <T> T copyTo(T target, String... ignoreProperties) {
4466
return FastBeanCopier.copy(this, target, ignoreProperties);
4567
}
4668

69+
/**
70+
* 从其他对象复制属性到当前对象
71+
*
72+
* @param target 其他对象
73+
* @param ignoreProperties 忽略复制的属性
74+
* @param <T> 类型
75+
* @return 当前对象
76+
*/
4777
@SuppressWarnings("all")
4878
default <T> T copyFrom(Object target, String... ignoreProperties) {
4979
return (T) FastBeanCopier.copy(target, this, ignoreProperties);

hsweb-commons/hsweb-commons-api/src/main/java/org/hswebframework/web/api/crud/entity/PagerResult.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,42 @@
2828
import java.util.List;
2929
import java.util.Map;
3030

31+
/**
32+
* 分页查询结果,用于在分页查询时,定义查询结果.如果需要拓展此类,例如自定义json序列化,请使用spi方式定义拓展实现类型:
33+
* <pre>
34+
* ---resources
35+
* -----|--META-INF
36+
* -----|----services
37+
* -----|------org.hswebframework.web.api.crud.entity.PagerResult
38+
* </pre>
39+
*
40+
* @param <E> 结果类型
41+
* @author zhouhao
42+
* @since 4.0.0
43+
*/
3144
@Getter
3245
@Setter
3346
public class PagerResult<E> {
3447
private static final long serialVersionUID = -6171751136953308027L;
3548

49+
/**
50+
* 创建一个空结果
51+
*
52+
* @param <E> 结果类型
53+
* @return PagerResult
54+
*/
3655
public static <E> PagerResult<E> empty() {
3756
return of(0, new ArrayList<>());
3857
}
3958

59+
/**
60+
* 创建一个分页结果
61+
*
62+
* @param total 总数据量
63+
* @param list 当前页数据列表
64+
* @param <E> 结果类型
65+
* @return PagerResult
66+
*/
4067
@SuppressWarnings("all")
4168
public static <E> PagerResult<E> of(int total, List<E> list) {
4269
PagerResult<E> result;
@@ -46,6 +73,15 @@ public static <E> PagerResult<E> of(int total, List<E> list) {
4673
return result;
4774
}
4875

76+
/**
77+
* 创建一个分页结果,并将查询参数中的分页索引等信息填充到分页结果中
78+
*
79+
* @param total 总数据量
80+
* @param list 当前页数据列表
81+
* @param entity 查询参数
82+
* @param <E> 结果类型
83+
* @return PagerResult
84+
*/
4985
public static <E> PagerResult<E> of(int total, List<E> list, QueryParam entity) {
5086
PagerResult<E> pagerResult = of(total, list);
5187
pagerResult.setPageIndex(entity.getThinkPageIndex());

0 commit comments

Comments
 (0)