Skip to content

Commit 89b2c43

Browse files
committed
增加AuthorizationListener
1 parent 26ad72b commit 89b2c43

File tree

16 files changed

+485
-85
lines changed

16 files changed

+485
-85
lines changed

hsweb-authorization/hsweb-authorization-api/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,32 @@ _点击名称,查看源代码注释获得使用说明_
2323
| ------------- |:-------------:|
2424
| [`Authorization`](src/main/java/org/hswebframework/web/authorization/Authorization.java) | 用户的认证信息 |
2525
| [`AuthorizationHolder`](src/main/java/org/hswebframework/web/authorization/AuthorizationHolder.java) | 用于获取当前登录用户的认证信息 |
26+
27+
28+
### Listener
29+
api提供[AuthorizationListener](src/main/java/org/hswebframework/web/authorization/listener/AuthorizationListener.java)
30+
来进行授权逻辑拓展,在授权前后执行可自定义的操作.如rsa解密帐号密码,验证码判断等。
31+
32+
默认事件列表():
33+
34+
| 类名 | 说明 |
35+
| ------------- |:-------------:|
36+
| [`AuthorizationDecodeEvent`](src/main/java/org/hswebframework/web/authorization/listener/event/AuthorizationDecodeEvent.java) | 接收到请求参数时 |
37+
| [`AuthorizationBeforeEvent`](src/main/java/org/hswebframework/web/authorization/listener/event/AuthorizationBeforeEvent.java) | 验证密码前触发 |
38+
| [`AuthorizationFailedEvent`](src/main/java/org/hswebframework/web/authorization/listener/event/AuthorizationFailedEvent.java) | 授权验证失败时触发 |
39+
| [`AuthorizationSuccessEvent`](src/main/java/org/hswebframework/web/authorization/listener/event/AuthorizationSuccessEvent.java) | 授权成功时触发 |
40+
| [`AuthorizationExitEvent`](src/main/java/org/hswebframework/web/authorization/listener/event/AuthorizationExitEvent.java) | 用户注销时触发 |
41+
42+
例子:
43+
44+
```java
45+
@Component
46+
public class CustomAuthorizationSuccessListener implements AuthorizationListener<AuthorizationSuccessEvent>{
47+
@Override
48+
public void on(AuthorizationSuccessEvent event) {
49+
Authorization authorization=event.getAuthorization();
50+
//....
51+
System.out.println(authorization.getUser().getName()+"登录啦");
52+
}
53+
}
54+
```

hsweb-authorization/hsweb-authorization-api/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@
1111

1212
<artifactId>hsweb-authorization-api</artifactId>
1313

14-
1514
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
package org.hswebframework.web.authorization.listener;
3+
4+
5+
import org.hswebframework.web.authorization.listener.event.AuthorizationEvent;
6+
7+
/**
8+
* 授权监听器,用于监听授权过程,以及自定义授权逻辑
9+
*
10+
* @author zhouhao
11+
* @see AuthorizationEvent
12+
* @since 3.0
13+
*/
14+
public interface AuthorizationListener<E extends AuthorizationEvent> {
15+
void on(E event);
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
19+
package org.hswebframework.web.authorization.listener;
20+
21+
import org.hswebframework.web.authorization.listener.event.AuthorizationEvent;
22+
23+
import java.util.*;
24+
25+
/**
26+
* @author zhouhao
27+
*/
28+
public class AuthorizationListenerDispatcher {
29+
30+
private Map<Class<? extends AuthorizationEvent>, List<AuthorizationListener>> listenerStore = new HashMap<>();
31+
32+
public <E extends AuthorizationEvent> void addListener(Class<E> eventClass, AuthorizationListener<E> listener) {
33+
listenerStore.computeIfAbsent(eventClass, (k) -> new LinkedList<>())
34+
.add(listener);
35+
}
36+
37+
@SuppressWarnings("unchecked")
38+
public <E extends AuthorizationEvent> void doEvent(Class<E> eventType, E event) {
39+
List<AuthorizationListener<E>> store = (List) listenerStore.get(eventType);
40+
if (null != store) {
41+
store.forEach(listener -> listener.on(event));
42+
}
43+
}
44+
45+
@SuppressWarnings("unchecked")
46+
public <E extends AuthorizationEvent> void doEvent(E event) {
47+
doEvent((Class<E>) event.getClass(), event);
48+
}
49+
}

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

Lines changed: 0 additions & 27 deletions
This file was deleted.

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

Lines changed: 0 additions & 43 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
19+
package org.hswebframework.web.authorization.listener.event;
20+
21+
22+
import java.util.Optional;
23+
import java.util.function.Function;
24+
25+
/**
26+
* 抽象授权事件,保存事件常用的数据
27+
*
28+
* @author zhouhao
29+
* @since 3.0
30+
*/
31+
public abstract class AbstractAuthorizationEvent implements AuthorizationEvent {
32+
protected String username;
33+
34+
protected String password;
35+
36+
private Function<String, Object> parameterGetter;
37+
38+
/**
39+
* 带参构造方法,所有参数不能为null
40+
*
41+
* @param username 用户名
42+
* @param password 密码
43+
* @param parameterGetter 参数获取函数,用户获取授权时传入的参数
44+
*/
45+
public AbstractAuthorizationEvent(String username, String password, Function<String, Object> parameterGetter) {
46+
if (username == null || password == null || parameterGetter == null) throw new NullPointerException();
47+
this.username = username;
48+
this.password = password;
49+
this.parameterGetter = parameterGetter;
50+
}
51+
52+
@SuppressWarnings("unchecked")
53+
protected <T> Optional<T> getParameter(String name) {
54+
return Optional.ofNullable((T) parameterGetter.apply(name));
55+
}
56+
57+
public String getUsername() {
58+
return username;
59+
}
60+
61+
public String getPassword() {
62+
return password;
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
19+
package org.hswebframework.web.authorization.listener.event;
20+
21+
import java.util.function.Function;
22+
23+
/**
24+
* 授权前事件
25+
*
26+
* @author zhouhao
27+
* @since 3.0
28+
*/
29+
public class AuthorizationBeforeEvent extends AbstractAuthorizationEvent {
30+
31+
public AuthorizationBeforeEvent(String username, String password, Function<String, Object> parameterGetter) {
32+
super(username, password, parameterGetter);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
19+
package org.hswebframework.web.authorization.listener.event;
20+
21+
import java.util.function.Function;
22+
23+
/**
24+
* 在进行授权时的最开始,触发此事件进行用户名密码解码,解码后请调用{@link #setUsername(String)} {@link #setPassword(String)}重新设置用户名密码
25+
*
26+
* @author zhouhao
27+
* @since 3.0
28+
*/
29+
public class AuthorizationDecodeEvent extends AbstractAuthorizationEvent {
30+
31+
public AuthorizationDecodeEvent(String username, String password, Function<String, Object> parameterGetter) {
32+
super(username, password, parameterGetter);
33+
}
34+
35+
public void setUsername(String username) {
36+
super.username = username;
37+
}
38+
39+
public void setPassword(String password) {
40+
super.username = password;
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
19+
package org.hswebframework.web.authorization.listener.event;
20+
21+
/**
22+
* 授权事件
23+
*
24+
* @author zhouhao
25+
* @see AuthorizationSuccessEvent
26+
* @see AuthorizationFailedEvent
27+
* @see AuthorizationBeforeEvent
28+
* @see AuthorizationDecodeEvent
29+
* @see AuthorizationExitEvent
30+
* @since 3.0
31+
*/
32+
public interface AuthorizationEvent {
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
19+
package org.hswebframework.web.authorization.listener.event;
20+
21+
import org.hswebframework.web.authorization.Authorization;
22+
23+
/**
24+
* 退出登录事件
25+
*
26+
* @author zhouhao
27+
*/
28+
public class AuthorizationExitEvent implements AuthorizationEvent {
29+
private Authorization authorization;
30+
31+
public AuthorizationExitEvent(Authorization authorization) {
32+
this.authorization = authorization;
33+
}
34+
35+
public Authorization getAuthorization() {
36+
return authorization;
37+
}
38+
}

0 commit comments

Comments
 (0)