利用user agent把某些用户拒绝门外
利用user agent把某些用户拒绝门外
Sandy最近写api但是又不想让PC端访问。网上找了一些方案总是没什么用,自己结合网上的办法和COO大仙给的建议。做成如下限制
1 | location /app/ {if ($http_user_agent !~* "firefox 自己定于,sandy是MD5生成32位") {rewrite ^/(.*) http://blog.e9china.net/ redirect;}} |
把firefox换成你自己的浏览器头中包含的字符串
如果不包含就跳转到Sandy博客
将地址改成你的app下载地址就行了
IOS下设置:
找到AppDelegate
在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions加入
1 | //get the original user-agent of webviewUIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];NSString *oldAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];NSLog(@"old agent :%@", oldAgent);//add my info to the new agentNSString *newAgent = [oldAgent stringByAppendingString:@"你的user_agent"];NSLog(@"new agent :%@", newAgent);//regist the new agentNSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil];[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary]; |
android 加入// 修改ua使得web端正确判断String ua = mWebView.getSettings().getUserAgentString();mWebView.getSettings().setUserAgentString(ua + ";你的user_agent");
如果有可能的话,在做个api获取验证。具体方法可以参考wordpress 的json官方接口
