Skip to content
This repository was archived by the owner on May 22, 2021. It is now read-only.

Commit d913fc8

Browse files
committed
Added scope merging feature (just request)
1 parent 2650a51 commit d913fc8

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

spring-auth/src/main/java/com/gewia/common/spring/auth/interceptor/ScopeInterceptor.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.gewia.common.spring.auth.AuthScope;
77
import com.gewia.common.spring.auth.Authentication;
88
import com.gewia.common.util.Pair;
9+
import java.util.ArrayList;
910
import java.util.List;
1011
import javax.servlet.http.HttpServletRequest;
1112
import javax.servlet.http.HttpServletResponse;
@@ -25,13 +26,18 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
2526

2627
HandlerMethod method = (HandlerMethod) handler;
2728

29+
AuthScope[] authScopes;
2830
Authentication auth = method.getMethodAnnotation(Authentication.class);
29-
if (auth == null) {
30-
response.setStatus(HttpStatus.OK.value());
31-
return true;
31+
AuthScope methodAuthScope = method.getMethodAnnotation(AuthScope.class);
32+
if (auth != null) authScopes = auth.value();
33+
else {
34+
if (methodAuthScope == null) {
35+
response.setStatus(HttpStatus.OK.value());
36+
return true;
37+
}
38+
authScopes = new AuthScope[]{methodAuthScope};
3239
}
3340

34-
AuthScope[] authScopes = auth.value();
3541

3642
String jwt = request.getHeader("Authorization");
3743
if (jwt == null || jwt.isBlank()) return false;
@@ -41,7 +47,16 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
4147
if (result.getRight() != JwtUtil.VerificationResult.SUCCESS) return false;
4248

4349
Claim claim = result.getLeft().getClaim("scopes");
44-
List<String> userScopes = claim.asList(String.class);
50+
List<String> userScopes = new ArrayList<>();
51+
for (String userScope : claim.asList(String.class)) {
52+
String[] splitUserScope = userScope.split("\\+");
53+
if (splitUserScope.length < 2) userScopes.add(userScope);
54+
else {
55+
for (int i = 1; i < splitUserScope.length; i++)
56+
userScopes.add(splitUserScope[0] + "." + splitUserScope[i]);
57+
}
58+
}
59+
4560
for (AuthScope authScope : authScopes) {
4661
String scope = authScope.scope();
4762
if (scope.isBlank()) scope = authScope.value();

0 commit comments

Comments
 (0)