Skip to content

Commit 6d0a5a7

Browse files
committed
增加OAuth2
1 parent 62b20b9 commit 6d0a5a7

File tree

44 files changed

+1230
-72
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1230
-72
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import java.io.Serializable;
2424
import java.util.*;
25+
import java.util.function.BiPredicate;
26+
import java.util.function.Predicate;
2527
import java.util.stream.Collectors;
2628

2729
/**
@@ -207,4 +209,13 @@ default boolean hasPermission(String permissionId, Collection<String> actions) {
207209
*/
208210
Authentication merge(Authentication source);
209211

212+
/**
213+
* copy为新的权限信息
214+
*
215+
* @param permissionFilter 权限过滤
216+
* @param dimension 维度过滤
217+
* @return 新的权限信息
218+
*/
219+
Authentication copy(BiPredicate<Permission, String> permissionFilter,
220+
Predicate<Dimension> dimension);
210221
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ default <T extends DataAccessConfig> Optional<T> findDataAccess(DataAccessPredic
153153
* @see FieldFilterDataAccessConfig#getFields()
154154
*/
155155
default Optional<FieldFilterDataAccessConfig> findFieldFilter(String action) {
156-
return findDataAccess(conf -> FieldFilterDataAccessConfig.class.isInstance(conf) && conf.getAction().equals(action));
156+
return findDataAccess(conf -> conf instanceof FieldFilterDataAccessConfig && conf.getAction().equals(action));
157157
}
158158

159159
/**
@@ -164,7 +164,7 @@ default Optional<FieldFilterDataAccessConfig> findFieldFilter(String action) {
164164
*/
165165
default Set<String> findDenyFields(String action) {
166166
return findFieldFilter(action)
167-
.filter(conf -> DENY_FIELDS.equals(conf.getType()))
167+
.filter(conf -> DENY_FIELDS.equals(conf.getType().getId()))
168168
.map(FieldFilterDataAccessConfig::getFields)
169169
.orElseGet(Collections::emptySet);
170170
}
@@ -210,6 +210,8 @@ static Permission.DataAccessPredicate<ScopeDataAccessConfig> scope(String action
210210

211211
Permission copy();
212212

213+
Permission copy(Predicate<String> actionFilter,Predicate<DataAccessConfig> dataAccessFilter);
214+
213215
/**
214216
* 数据权限查找判断逻辑接口
215217
*

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
import org.hswebframework.web.authorization.simple.builder.DataAccessConfigConverter;
77
import org.hswebframework.web.authorization.simple.builder.SimpleAuthenticationBuilderFactory;
88
import org.hswebframework.web.authorization.simple.builder.SimpleDataAccessConfigBuilderFactory;
9-
import org.hswebframework.web.authorization.token.DefaultUserTokenManager;
10-
import org.hswebframework.web.authorization.token.UserTokenAuthenticationSupplier;
11-
import org.hswebframework.web.authorization.token.UserTokenReactiveAuthenticationSupplier;
12-
import org.hswebframework.web.authorization.token.UserTokenManager;
9+
import org.hswebframework.web.authorization.token.*;
1310
import org.hswebframework.web.authorization.twofactor.TwoFactorValidatorManager;
1411
import org.hswebframework.web.authorization.twofactor.defaults.DefaultTwoFactorValidatorManager;
1512
import org.hswebframework.web.convert.CustomMessageConverter;

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
import java.io.Serializable;
2525
import java.util.*;
26+
import java.util.function.BiPredicate;
2627
import java.util.function.Function;
28+
import java.util.function.Predicate;
2729
import java.util.stream.Collectors;
2830

2931
@Getter
@@ -40,9 +42,10 @@ public class SimpleAuthentication implements Authentication {
4042

4143
private Map<String, Serializable> attributes = new HashMap<>();
4244

43-
public static Authentication of(){
45+
public static Authentication of() {
4446
return new SimpleAuthentication();
4547
}
48+
4649
@Override
4750
@SuppressWarnings("unchecked")
4851
public <T extends Serializable> Optional<T> getAttribute(String name) {
@@ -77,4 +80,19 @@ public SimpleAuthentication merge(Authentication authentication) {
7780
}
7881
return this;
7982
}
83+
84+
@Override
85+
public Authentication copy(BiPredicate<Permission, String> permissionFilter,
86+
Predicate<Dimension> dimension) {
87+
SimpleAuthentication authentication = new SimpleAuthentication();
88+
authentication.setUser(user);
89+
authentication.setDimensions(dimensions.stream().filter(dimension).collect(Collectors.toList()));
90+
authentication.setPermissions(permissions
91+
.stream()
92+
.map(permission -> permission.copy(action -> permissionFilter.test(permission, action), conf -> true))
93+
.filter(per -> !per.getActions().isEmpty())
94+
.collect(Collectors.toList())
95+
);
96+
return authentication;
97+
}
8098
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.hswebframework.web.authorization.access.DataAccessConfig;
66

77
import java.util.*;
8+
import java.util.function.Predicate;
9+
import java.util.stream.Collectors;
810

911
/**
1012
* @author zhouhao
@@ -42,16 +44,22 @@ public Set<DataAccessConfig> getDataAccesses() {
4244
return dataAccesses;
4345
}
4446

45-
public Permission copy() {
47+
@Override
48+
public Permission copy(Predicate<String> actionFilter,
49+
Predicate<DataAccessConfig> dataAccessFilter) {
4650
SimplePermission permission = new SimplePermission();
4751

4852
permission.setId(id);
4953
permission.setName(name);
50-
permission.setActions(new HashSet<>(getActions()));
51-
permission.setDataAccesses(new HashSet<>(getDataAccesses()));
54+
permission.setActions(getActions().stream().filter(actionFilter).collect(Collectors.toSet()));
55+
permission.setDataAccesses(getDataAccesses().stream().filter(dataAccessFilter).collect(Collectors.toSet()));
5256
if (options != null) {
5357
permission.setOptions(new HashMap<>(options));
5458
}
5559
return permission;
5660
}
61+
62+
public Permission copy() {
63+
return copy(action -> true, conf -> true);
64+
}
5765
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ public interface ParsedToken {
1515
* @return 令牌类型
1616
*/
1717
String getType();
18+
19+
static ParsedToken of(String type, String token) {
20+
return SimpleParsedToken.of(type, token);
21+
}
1822
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.hswebframework.web.authorization.token;
2+
3+
import lombok.AllArgsConstructor;
4+
import org.hswebframework.web.authorization.Authentication;
5+
import org.hswebframework.web.authorization.ReactiveAuthenticationSupplier;
6+
import org.hswebframework.web.context.ContextKey;
7+
import org.hswebframework.web.context.ContextUtils;
8+
import org.hswebframework.web.logger.ReactiveLogger;
9+
import reactor.core.publisher.Mono;
10+
11+
@AllArgsConstructor
12+
public class ReactiveTokenAuthenticationSupplier implements ReactiveAuthenticationSupplier {
13+
14+
private final TokenAuthenticationManager tokenManager;
15+
16+
@Override
17+
public Mono<Authentication> get(String userId) {
18+
return Mono.empty();
19+
}
20+
21+
@Override
22+
public Mono<Authentication> get() {
23+
return ContextUtils.reactiveContext()
24+
.flatMap(context ->
25+
context.get(ContextKey.of(ParsedToken.class))
26+
.map(t -> tokenManager.getByToken(t.getToken()))
27+
.orElseGet(Mono::empty))
28+
.flatMap(auth -> ReactiveLogger.mdc("userId", auth.getUser().getId())
29+
.then(ReactiveLogger.mdc("username", auth.getUser().getName()))
30+
.thenReturn(auth));
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.hswebframework.web.authorization.token;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
@Getter
8+
@Setter
9+
@AllArgsConstructor(staticName = "of")
10+
public class SimpleParsedToken implements ParsedToken{
11+
12+
private String type;
13+
14+
private String token;
15+
16+
17+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ public interface TokenAuthenticationManager {
3131
*/
3232
Mono<Void> putAuthentication(String token, Authentication auth, Duration ttl);
3333

34+
/**
35+
* 删除token
36+
* @param token token
37+
* @return void
38+
*/
39+
Mono<Void> removeToken(String token);
3440
}

hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/redis/RedisTokenAuthenticationManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ public Mono<Authentication> getByToken(String token) {
3838
.get("token-auth:" + token);
3939
}
4040

41+
@Override
42+
public Mono<Void> removeToken(String token) {
43+
return operations
44+
.delete(token)
45+
.then();
46+
}
47+
4148
@Override
4249
public Mono<Void> putAuthentication(String token, Authentication auth, Duration ttl) {
4350
return ttl.isNegative()

hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/web/AuthorizationController.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717

1818
package org.hswebframework.web.authorization.basic.web;
1919

20-
import io.swagger.annotations.ApiOperation;
21-
import io.swagger.annotations.ApiParam;
2220
import io.swagger.v3.oas.annotations.Operation;
2321
import io.swagger.v3.oas.annotations.Parameter;
24-
import io.swagger.v3.oas.annotations.media.Schema;
2522
import io.swagger.v3.oas.annotations.tags.Tag;
2623
import lombok.SneakyThrows;
2724
import org.hswebframework.web.authorization.Authentication;
@@ -33,12 +30,10 @@
3330
import org.hswebframework.web.authorization.events.AuthorizationSuccessEvent;
3431
import org.hswebframework.web.authorization.exception.AuthenticationException;
3532
import org.hswebframework.web.authorization.exception.UnAuthorizedException;
36-
import org.hswebframework.web.authorization.simple.CompositeReactiveAuthenticationManager;
3733
import org.hswebframework.web.authorization.simple.PlainTextUsernamePasswordAuthenticationRequest;
3834
import org.hswebframework.web.logging.AccessLogger;
3935
import org.springframework.beans.factory.annotation.Autowired;
4036
import org.springframework.context.ApplicationEventPublisher;
41-
import org.springframework.data.repository.query.Param;
4237
import org.springframework.http.MediaType;
4338
import org.springframework.util.Assert;
4439
import org.springframework.web.bind.annotation.*;
@@ -71,7 +66,6 @@ public Mono<Authentication> me() {
7166
}
7267

7368
@PostMapping(value = "/login", consumes = MediaType.APPLICATION_JSON_VALUE)
74-
@ApiOperation("用户名密码登录,json方式")
7569
@Authorize(ignore = true)
7670
@AccessLogger(ignore = true)
7771
@Operation(summary = "登录",description = "必要参数:username,password.根据配置不同,其他参数也不同,如:验证码等.")

hsweb-authorization/hsweb-authorization-oauth2/src/main/java/org/hswebframework/web/oauth2/OAuth2Exception.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package org.hswebframework.web.oauth2;
22

33
import lombok.Getter;
4+
import org.hswebframework.web.exception.BusinessException;
45

56
@Getter
6-
public class OAuth2Exception extends RuntimeException {
7+
public class OAuth2Exception extends BusinessException {
78
private final ErrorType type;
89

910
public OAuth2Exception(ErrorType type) {
10-
super(type.message());
11+
super(type.message(), type.name(), type.code());
1112
this.type = type;
1213
}
1314

hsweb-authorization/hsweb-authorization-oauth2/src/main/java/org/hswebframework/web/oauth2/server/AccessToken.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import io.swagger.v3.oas.annotations.media.Schema;
5-
import lombok.Getter;
6-
import lombok.Setter;
5+
import lombok.*;
76

87
@Getter
98
@Setter
9+
@AllArgsConstructor
10+
@NoArgsConstructor
11+
@ToString
1012
public class AccessToken extends OAuth2Response {
1113

14+
private static final long serialVersionUID = -6849794470754667710L;
15+
1216
@Schema(name="access_token")
1317
@JsonProperty("access_token")
1418
private String accessToken;
1519

16-
@Schema(name="expires_in")
17-
@JsonProperty("expires_in")
18-
private int expiresIn;
19-
2020
@Schema(name="refresh_token")
2121
@JsonProperty("refresh_token")
2222
private String refreshToken;
2323

24+
@Schema(name="expires_in")
25+
@JsonProperty("expires_in")
26+
private int expiresIn;
27+
2428
}

hsweb-authorization/hsweb-authorization-oauth2/src/main/java/org/hswebframework/web/oauth2/server/AccessTokenManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ public interface AccessTokenManager {
77

88
Mono<Authentication> getAuthenticationByToken(String accessToken);
99

10-
Mono<AccessToken> createAccessToken(String clientId, Authentication authentication);
10+
Mono<AccessToken> createAccessToken(String clientId,
11+
Authentication authentication,
12+
boolean singleton);
1113

1214
Mono<AccessToken> refreshAccessToken(String clientId, String refreshToken);
1315

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package org.hswebframework.web.oauth2.server;
22

33

4-
import org.hswebframework.web.authorization.Authentication;
54
import org.hswebframework.web.oauth2.server.code.AuthorizationCodeGranter;
6-
import reactor.core.publisher.Mono;
75

86
public interface OAuth2GrantService {
97

10-
AuthorizationCodeGranter code();
8+
AuthorizationCodeGranter authorizationCode();
119

1210
ClientCredentialGranter clientCredential();
1311

14-
Mono<Authentication> grant(String scope, Authentication authentication);
1512
}

hsweb-authorization/hsweb-authorization-oauth2/src/main/java/org/hswebframework/web/oauth2/server/OAuth2Response.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import lombok.Getter;
66
import lombok.Setter;
77

8+
import java.io.Serializable;
89
import java.util.HashMap;
910
import java.util.Map;
1011

1112
@Getter
1213
@Setter
13-
public class OAuth2Response {
14+
public class OAuth2Response implements Serializable {
1415
@Hidden
1516
private Map<String,Object> parameters;
1617

0 commit comments

Comments
 (0)