Set the JSAPI authentication method

更新时间:
复制 MD 格式

Add access control for all JSAPI calls by configuring a custom plugin that intercepts and validates page URLs.

  1. Set up a custom access control plugin.

    1. Create a custom plugin to listen for JSAPI call events and intercept them.

    2. After the plugin intercepts an event, retrieve the current page URL and verify it by matching the host and scheme.

    @interface MPPlugin4WebView : NBPluginBase
    
    @end
    
    @implementation MPPlugin4WebView
    
    - (void)pluginDidLoad
    {
        self.scope = kPSDScope_Scene;
        
        // Intercept the information of the invoked JSAPI.
        [self.target addEventListener:kEvent_Invocation_Event_Start withListener:self useCapture:NO];
        [self.target addEventListener:kEvent_Invocation_Invoke withListener:self useCapture:NO];
            
        [super pluginDidLoad];
    }
    
    - (void)handleEvent:(PSDEvent *)event
    {
        [super handleEvent:event];
        
        if([kEvent_Invocation_Event_Start isEqualToString:event.eventType] ||
                 [kEvent_Invocation_Invoke isEqualToString:event.eventType]){
            PSDInvocationEvent *invocationEvent = (PSDInvocationEvent *)event;
            NSString *apiName = invocationEvent.invocationName;
            NSDictionary *data = invocationEvent.invocationData;
    
            // Get the URL of the current page and perform string match verification based on the scheme and host.
            NSURL *url = event.context.currentViewController.url;
            if (![url.host isEqualToString:@"xxx"] || ![url.scheme isEqualToString:@"xxx"]) {
                [event preventDefault];
                [event stopPropagation];
                return;
            }
            
        }
    }
    
    - (int)priority
    {
        return PSDPluginPriority_High+1;
    }
    Important

    Match the URL precisely. At a minimum, match the scheme and host of the URI. Avoid using regex matching. Strictly avoid imprecise functions such as contains, startsWith, endsWith, or indexOf.

  2. Register the plugin.

    1. During the mPaaS container initialization, specify the path to the custom plugin.

    2. In the plist file of the custom plugin's bundle, register the plugin from the previous step. For more information, see Register a plugin.

      image