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

Commit 0383f4b

Browse files
committed
Added Repeatable
1 parent d75b4a9 commit 0383f4b

8 files changed

Lines changed: 76 additions & 45 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.gewia.common.spring.auth;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Repeatable;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
@Retention(RetentionPolicy.RUNTIME)
10+
@Target(ElementType.METHOD)
11+
@Repeatable(Authentication.class)
12+
public @interface AuthScope {
13+
14+
String value() default "";
15+
16+
String scope() default "";
17+
18+
}

spring-auth/src/main/java/com/gewia/common/spring/auth/Authentication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
@Target(ElementType.METHOD)
1010
public @interface Authentication {
1111

12-
String scope() default "";
12+
AuthScope[] value();
1313

1414
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.gewia.common.spring.auth;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.METHOD)
10+
public @interface IgnoreServiceToken {
11+
}
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package com.gewia.common.spring.auth;
22

3-
import com.gewia.common.spring.auth.interceptor.AuthenticationInterceptor;
43
import java.util.ArrayList;
54
import java.util.List;
6-
import javax.annotation.PostConstruct;
5+
import lombok.AccessLevel;
76
import lombok.Getter;
7+
import org.springframework.beans.factory.InitializingBean;
8+
import org.springframework.context.annotation.ComponentScan;
9+
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
810

9-
public abstract class SpringAuthentication {
11+
@ComponentScan("com.gewia.common.spring.auth")
12+
public abstract class SpringAuthentication implements InitializingBean {
1013

11-
@Getter private static List<AuthenticationInterceptor> authenticationInterceptors = new ArrayList<>();
14+
@Getter(AccessLevel.PACKAGE) private static List<HandlerInterceptorAdapter> interceptors = new ArrayList<>();
1215

13-
@PostConstruct
14-
public void registerInterceptor() {
15-
authenticationInterceptors = this.addAuthenticationInterceptors(authenticationInterceptors);
16+
@Override
17+
public void afterPropertiesSet() throws Exception {
18+
interceptors = this.addAuthenticationInterceptors(interceptors);
1619
}
1720

18-
abstract public List<AuthenticationInterceptor> addAuthenticationInterceptors(List<AuthenticationInterceptor> authenticationInterceptors);
21+
abstract public List<HandlerInterceptorAdapter> addAuthenticationInterceptors(List<HandlerInterceptorAdapter> authenticationInterceptors);
1922

2023
}

spring-auth/src/main/java/com/gewia/common/spring/auth/SpringAuthenticationWebConfig.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.gewia.common.spring.auth;
22

33
import com.auth0.jwt.interfaces.DecodedJWT;
4-
import com.gewia.common.spring.auth.interceptor.AuthenticationInterceptor;
54
import java.util.List;
65
import javax.servlet.http.HttpServletRequest;
76
import org.springframework.context.annotation.Configuration;
@@ -13,6 +12,7 @@
1312
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
1413
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
1514
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
15+
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
1616

1717
@Configuration
1818
@EnableWebMvc
@@ -35,8 +35,8 @@ public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers)
3535

3636
@Override
3737
public void addInterceptors(InterceptorRegistry registry) {
38-
for (AuthenticationInterceptor authenticationInterceptor : SpringAuthentication.getAuthenticationInterceptors())
39-
registry.addInterceptor(authenticationInterceptor).addPathPatterns("/**/*");
38+
for (HandlerInterceptorAdapter interceptors : SpringAuthentication.getInterceptors())
39+
registry.addInterceptor(interceptors).addPathPatterns("/**/*");
4040
}
4141

4242
}

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

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

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,56 @@
33
import com.auth0.jwt.interfaces.Claim;
44
import com.auth0.jwt.interfaces.DecodedJWT;
55
import com.gewia.common.auth.jwt.JwtUtil;
6+
import com.gewia.common.spring.auth.AuthScope;
67
import com.gewia.common.spring.auth.Authentication;
78
import com.gewia.common.util.Pair;
89
import java.util.List;
910
import javax.servlet.http.HttpServletRequest;
1011
import javax.servlet.http.HttpServletResponse;
1112
import lombok.AllArgsConstructor;
1213
import org.springframework.http.HttpStatus;
14+
import org.springframework.web.method.HandlerMethod;
15+
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
1316

1417
@AllArgsConstructor
15-
public class AuthenticationScopeInterceptor extends AuthenticationInterceptor {
18+
public class ScopeInterceptor extends HandlerInterceptorAdapter {
1619

1720
private JwtUtil jwtUtil;
1821

1922
@Override
2023
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
2124
response.setStatus(HttpStatus.FORBIDDEN.value());
2225

23-
List<Authentication> authentications = this.getAuthenticationAnnotations(handler);
26+
HandlerMethod method = (HandlerMethod) handler;
2427

25-
if (authentications.isEmpty()) {
26-
response.setStatus(HttpStatus.OK.value());
27-
return true;
28+
AuthScope[] authScopes;
29+
Authentication auth = method.getMethodAnnotation(Authentication.class);
30+
AuthScope methodAuthScope = method.getMethodAnnotation(AuthScope.class);
31+
if (auth != null) authScopes = auth.value();
32+
else {
33+
if (methodAuthScope == null) {
34+
response.setStatus(HttpStatus.OK.value());
35+
return true;
36+
}
37+
authScopes = new AuthScope[]{methodAuthScope};
2838
}
2939

40+
3041
String jwt = request.getHeader("Authorization");
42+
if (jwt == null || jwt.isBlank()) return false;
3143

3244
Pair<DecodedJWT, JwtUtil.VerificationResult> result = this.jwtUtil.verify(jwt);
33-
3445
if (result.getRight() != JwtUtil.VerificationResult.SUCCESS) return false;
3546

3647
Claim claim = result.getLeft().getClaim("scopes");
3748
List<String> userScopes = claim.asList(String.class);
38-
for (Authentication authentication : authentications) {
39-
if (!authentication.scope().isBlank()) {
49+
for (AuthScope authScope : authScopes) {
50+
String scope = authScope.scope();
51+
if (scope.isBlank()) scope = authScope.value();
52+
if (!scope.isBlank()) {
4053
boolean isPresent = false;
4154
for (String userScope : userScopes)
42-
if (userScope.equalsIgnoreCase(authentication.scope())) {
55+
if (userScope.equalsIgnoreCase(scope)) {
4356
isPresent = true;
4457
break;
4558
}

spring-auth/src/main/java/com/gewia/common/spring/auth/interceptor/AuthenticationServiceTokenInterceptor.java renamed to spring-auth/src/main/java/com/gewia/common/spring/auth/interceptor/ServiceTokenInterceptor.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
package com.gewia.common.spring.auth.interceptor;
22

3+
import com.gewia.common.spring.auth.IgnoreServiceToken;
34
import javax.servlet.http.HttpServletRequest;
45
import javax.servlet.http.HttpServletResponse;
56
import lombok.AllArgsConstructor;
67
import org.springframework.http.HttpStatus;
8+
import org.springframework.web.method.HandlerMethod;
9+
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
710

811
@AllArgsConstructor
9-
public class AuthenticationServiceTokenInterceptor extends AuthenticationInterceptor {
12+
public class ServiceTokenInterceptor extends HandlerInterceptorAdapter {
1013

1114
private String serviceToken;
1215

1316
@Override
1417
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
1518
response.setStatus(HttpStatus.FORBIDDEN.value());
1619

20+
HandlerMethod method = (HandlerMethod) handler;
21+
if (method.hasMethodAnnotation(IgnoreServiceToken.class)) {
22+
response.setStatus(HttpStatus.OK.value());
23+
return true;
24+
}
25+
1726
String serviceToken = request.getHeader("X-ServiceToken");
1827

1928
if (serviceToken == null) return false;

0 commit comments

Comments
 (0)