3434import java .util .Map ;
3535import java .util .concurrent .CompletionStage ;
3636
37- import static com .jd .live .agent .core .util .StringUtils .isEmpty ;
38-
3937/**
4038 * AuthFilter
4139 *
@@ -53,21 +51,29 @@ public class AuthFilter implements InboundFilter {
5351 public <T extends InboundRequest > CompletionStage <Object > filter (InboundInvocation <T > invocation , InboundFilterChain chain ) {
5452 ServiceMetadata metadata = invocation .getServiceMetadata ();
5553 Service service = metadata .getService ();
56- if (service != null && service .authorized ()) {
57- AuthPolicy authPolicy = service .getAuthPolicy (metadata .getConsumer ());
58- if (authPolicy == null ) {
59- return Futures .future (FaultType .UNAUTHORIZED .reject ("the consumer is not authorized for service " + metadata .getServiceName ()));
60- }
61- String authType = authPolicy .getType ();
62- // check auth policy
63- Authenticate authenticate = isEmpty (authType ) ? null : authenticates .get (authType );
64- if (authenticate != null ) {
65- Permission permission = authenticate .authenticate (invocation .getRequest (), authPolicy ,
66- metadata .getServiceName (), metadata .getConsumer ());
67- if (permission != null && !permission .isSuccess ()) {
68- return Futures .future (FaultType .UNAUTHORIZED .reject (permission .getMessage ()));
69- }
70- }
54+ if (service == null || !service .authorized ()) {
55+ return chain .filter (invocation );
56+ }
57+ AuthPolicy authPolicy = service .getAuthPolicy (metadata .getConsumer ());
58+ if (authPolicy == null ) {
59+ return Futures .future (FaultType .UNAUTHORIZED .reject ("the consumer is not authorized for service " + metadata .getServiceName ()));
60+ }
61+ String authType = authPolicy .getType ();
62+ if (authType == null || authType .isEmpty ()) {
63+ return chain .filter (invocation );
64+ }
65+ Authenticate authenticate = authenticates .get (authType );
66+ if (authenticate == null ) {
67+ return chain .filter (invocation );
68+ }
69+ Permission permission = authenticate .authenticate (
70+ invocation .getRequest (),
71+ authPolicy ,
72+ metadata .getServiceName (),
73+ metadata .getConsumer ()
74+ );
75+ if (permission != null && !permission .isSuccess ()) {
76+ return Futures .future (FaultType .UNAUTHORIZED .reject (permission .getMessage ()));
7177 }
7278 return chain .filter (invocation );
7379 }
0 commit comments