文档

JSAPI 鉴权方法设置

更新时间:

mPaaS 建议所有的 JSAPI 访问都需要添加访问控制,目前可以通过设置 Plugin 的方式添加访问控制。

  1. 设置自定义权限控制 Plugin。

    1. 自定义 Plugin,监听 JSAPI 调用的事件,进行拦截处理。

    2. 在 Plugin 中拦截到事件后,获取当前页面的 URL,建议根据 host、scheme 进行字符串匹配校验。

    @interface MPPlugin4WebView : NBPluginBase
    
    @end
    
    @implementation MPPlugin4WebView
    
    - (void)pluginDidLoad
    {
        self.scope = kPSDScope_Scene;
        
        // -- 拦截调用的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;
    
            // 获取到当前页面的url,根据scheme和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;
    }
    重要

    URL 要进行精准匹配,至少要匹配到 URI 类的 scheme 和 host 信息,慎用或不用正则匹配,严格避免使用 contains、startsWith、endsWith、indexOf 等不精准函数。

  2. 注册 Plugin。

    1. 在 mPaaS 容器初始化时,指定自定义 Plugin 的路径。

    2. 在自定义 Plugin 的 bundle 的 plist 文件中,注册上一步的 Plugin。详情请参考 注册 Plugin

      image
  • 本页导读 (0)
文档反馈