Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
order-management-backend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ascend
order-management-backend
Commits
1bcc59e8
Commit
1bcc59e8
authored
May 11, 2021
by
Alex Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[AFP-91]
🎨
Create custom annotations & resolvers for 'ManagerController' (
@asegers
)
parent
10d911ad
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
132 additions
and
0 deletions
+132
-0
AccessToken.java
.../java/com/afp/ordermanagement/annotation/AccessToken.java
+11
-0
AccessTokenResolver.java
...m/afp/ordermanagement/annotation/AccessTokenResolver.java
+28
-0
AuthManagerController.java
...afp/ordermanagement/annotation/AuthManagerController.java
+11
-0
AuthManagerResponse.java
...m/afp/ordermanagement/annotation/AuthManagerResponse.java
+11
-0
ManagerPayload.java
...va/com/afp/ordermanagement/annotation/ManagerPayload.java
+13
-0
ManagerPayloadResolver.java
...fp/ordermanagement/annotation/ManagerPayloadResolver.java
+58
-0
No files found.
src/main/java/com/afp/ordermanagement/annotation/AccessToken.java
0 → 100644
View file @
1bcc59e8
package
com
.
afp
.
ordermanagement
.
annotation
;
import
java.lang.annotation.ElementType
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
@Target
(
ElementType
.
PARAMETER
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
public
@interface
AccessToken
{
}
src/main/java/com/afp/ordermanagement/annotation/AccessTokenResolver.java
0 → 100644
View file @
1bcc59e8
package
com
.
afp
.
ordermanagement
.
annotation
;
import
com.afp.ordermanagement.service.ManagerTokenVerifier
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.core.MethodParameter
;
import
org.springframework.web.reactive.BindingContext
;
import
org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver
;
import
org.springframework.web.server.ServerWebExchange
;
import
reactor.core.publisher.Mono
;
public
class
AccessTokenResolver
implements
HandlerMethodArgumentResolver
{
@Autowired
ManagerTokenVerifier
managerTokenVerifier
;
@Override
public
boolean
supportsParameter
(
MethodParameter
methodParameter
)
{
boolean
hasClassAnnotation
=
methodParameter
.
getDeclaringClass
().
isAnnotationPresent
(
AuthManagerController
.
class
);
boolean
hasMethodAnnotation
=
methodParameter
.
hasMethodAnnotation
(
AuthManagerResponse
.
class
);
boolean
hasParameterAnnotation
=
methodParameter
.
hasParameterAnnotation
(
AccessToken
.
class
);
return
(
hasClassAnnotation
||
hasMethodAnnotation
)
&&
hasParameterAnnotation
;
}
@Override
public
Mono
<
Object
>
resolveArgument
(
MethodParameter
methodParameter
,
BindingContext
bindingContext
,
ServerWebExchange
serverWebExchange
)
{
String
accessToken
=
managerTokenVerifier
.
getTokenHeader
(
serverWebExchange
);
return
Mono
.
just
(
accessToken
);
}
}
src/main/java/com/afp/ordermanagement/annotation/AuthManagerController.java
0 → 100644
View file @
1bcc59e8
package
com
.
afp
.
ordermanagement
.
annotation
;
import
java.lang.annotation.ElementType
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
@Target
(
ElementType
.
TYPE
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
public
@interface
AuthManagerController
{
}
src/main/java/com/afp/ordermanagement/annotation/AuthManagerResponse.java
0 → 100644
View file @
1bcc59e8
package
com
.
afp
.
ordermanagement
.
annotation
;
import
java.lang.annotation.ElementType
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
@Target
(
ElementType
.
METHOD
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
public
@interface
AuthManagerResponse
{
}
src/main/java/com/afp/ordermanagement/annotation/ManagerPayload.java
0 → 100644
View file @
1bcc59e8
package
com
.
afp
.
ordermanagement
.
annotation
;
import
java.lang.annotation.ElementType
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
@Target
(
ElementType
.
PARAMETER
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
public
@interface
ManagerPayload
{
String
value
()
default
""
;
String
pluck
()
default
""
;
}
src/main/java/com/afp/ordermanagement/annotation/ManagerPayloadResolver.java
0 → 100644
View file @
1bcc59e8
package
com
.
afp
.
ordermanagement
.
annotation
;
import
com.afp.ordermanagement.model.Manager
;
import
com.afp.ordermanagement.service.ManagerTokenVerifier
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.core.MethodParameter
;
import
org.springframework.web.reactive.BindingContext
;
import
org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver
;
import
org.springframework.web.server.ServerWebExchange
;
import
reactor.core.publisher.Mono
;
import
java.lang.reflect.Field
;
import
java.util.Arrays
;
import
java.util.List
;
public
class
ManagerPayloadResolver
implements
HandlerMethodArgumentResolver
{
@Autowired
ManagerTokenVerifier
managerTokenVerifier
;
@Override
public
boolean
supportsParameter
(
MethodParameter
methodParameter
)
{
boolean
hasClassAnnotation
=
methodParameter
.
getDeclaringClass
().
isAnnotationPresent
(
AuthManagerController
.
class
);
boolean
hasMethodAnnotation
=
methodParameter
.
hasMethodAnnotation
(
AuthManagerResponse
.
class
);
boolean
hasParameterAnnotation
=
methodParameter
.
hasParameterAnnotation
(
ManagerPayload
.
class
);
return
(
hasClassAnnotation
||
hasMethodAnnotation
)
&&
hasParameterAnnotation
;
}
@Override
public
Mono
<
Object
>
resolveArgument
(
MethodParameter
methodParameter
,
BindingContext
bindingContext
,
ServerWebExchange
serverWebExchange
)
{
String
accessToken
=
managerTokenVerifier
.
getTokenHeader
(
serverWebExchange
);
ManagerPayload
annotation
=
methodParameter
.
getParameterAnnotation
(
ManagerPayload
.
class
);
String
value
=
annotation
.
value
(),
pluck
=
annotation
.
pluck
(),
selectedField
;
List
<
Field
>
declaredFields
=
Arrays
.
asList
(
Manager
.
class
.
getDeclaredFields
());
Manager
manager
=
managerTokenVerifier
.
createManagerFromToken
(
accessToken
);
if
(
value
.
isEmpty
()
&&
pluck
.
isEmpty
())
{
return
Mono
.
just
(
manager
);
}
else
{
selectedField
=
value
.
isEmpty
()
?
pluck
:
value
;
if
(!
declaredFields
.
contains
(
selectedField
))
return
Mono
.
just
(
manager
);
try
{
Field
field
=
Manager
.
class
.
getDeclaredField
(
selectedField
);
field
.
setAccessible
(
true
);
return
Mono
.
just
(
field
.
get
(
manager
));
}
catch
(
Exception
ignore
)
{
return
Mono
.
just
(
manager
);
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment