red5(三) 防止盗链

优化red5(二) 防止盗链,只修改一个地方就可以了

package first;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.stream.IServerStream;

import com.cqa.platform.util.SystemGlobals;

public class Application extends ApplicationAdapter{

  private IScope appScope;
  private IServerStream serverStream;

  public Application()
  {
	  System.out.println("Application构造函数  ");
  }
  //appStart将在链接开始时自动触发
  public boolean appStart(IScope app)
  {
    return true;
  }
  //链接时触发的函数
  public boolean appConnect(IConnection conn, Object[] params)
  {
	  Map<String, Object> hm = conn.getConnectParams();
	  String host =   (String) hm.get("pageUrl");
	  String allowHost = "http://localhost:18082/red5/";
	  boolean ret = false;
	  if(allowHost == null || StringUtils.isBlank(allowHost )){
		  ret = true;
	  }else {
			 String[] args = allowHost.split(",");
			 ret = false;
			 if(host != null && StringUtils.isNotBlank(host) ){
				 for(int i=0;i<args.length;++i){
					 if(host.indexOf( args[i])>=0){
						 ret = true;
						 break;
					 }
				 }
			 }
	 }
	 if(ret){
		 return super.appConnect(conn, params);
	 }else {
		 return false;
	 }

  }

  public void appDisconnect(IConnection conn)
  {

    if ((this.appScope == conn.getScope()) && (this.serverStream != null)) {
      this.serverStream.close();
    }
    super.appDisconnect(conn);
  }
}

猜你喜欢

转载自01jiangwei01.iteye.com/blog/1809945