多协议、性能稳定、丰富API的流媒体服务器软件
Wowza Streaming Engine 如何用数据库中的用户名密码对RTMP推流和RTSP推流请求做认证?
下面介绍了如何用AuthenticateUsernamePasswordProviderBase实现类以及ModuleRTMPAuthenticate模块来获得RTMP连接时的用户名/密码并到你自己的数据库中做认证。

<Module>
	<Name>ModuleRTMPAuthenticate</Name>
	<Description>ModuleRTMPAuthenticate</Description>
	<Class>com.wowza.wms.security.ModuleRTMPAuthenticate</Class>
</Module>
		
  1. 下载mySQL的JDBC驱动(http://dev.mysql.com/downloads/connector/j/5.0.html),然后拷贝适当的MySQL JDBC .jar 文件到Wowza Media Server 的/lib 文件夹中。

  2. 参考下面的代码,建立你自己的数据库认证模块:
    package com.wowza.wms.example.authenticate;
    
    import com.wowza.wms.authentication.*;
    import com.wowza.wms.logging.WMSLoggerFactory; 
    import java.sql.*;
    
    public class AuthenticateUsernamePasswordProviderExample extends AuthenticateUsernamePasswordProviderBase
    {
    	public String getPassword(String username)
    	{
    		// return password for given username		
    		String pwd = null;
    		
    		WMSLoggerFactory.getLogger(null).info("Authenticate getPassword username: " + username);
    		
    		Connection conn = null;
    		try 
    		{
    			conn = DriverManager.getConnection("jdbc:mysql://localhost/wowza?user=root&password=mypassword");
    
    			Statement stmt = null;
    			ResultSet rs = null;
    
    			try 
    			{
    				stmt = conn.createStatement();
    				rs = stmt.executeQuery("SELECT pwd FROM users where username = '"+username+"'");
    				while (rs.next())
    				{
    					pwd = rs.getString("pwd");
    				}
    
    			} 
    			catch (SQLException sqlEx) 
    			{
    				WMSLoggerFactory.getLogger(null).error("sqlexecuteException: " + sqlEx.toString());
    			} 
    			finally 
    			{
    				if (rs != null) 
    				{
    					try 
    					{
    						rs.close();
    					} 
    					catch (SQLException sqlEx) 
    					{
    
    						rs = null;
    					}
    				}
    
    				if (stmt != null) 
    				{
    					try 
    					{
    						stmt.close();
    					} 
    					catch (SQLException sqlEx) 
    					{
    						stmt = null;
    					}
    				}
    			}
    
    			conn.close();
    		} 
    		catch (SQLException ex) 
    		{
    			// handle any errors
    			System.out.println("SQLException: " + ex.getMessage());
    			System.out.println("SQLState: " + ex.getSQLState());
    			System.out.println("VendorError: " + ex.getErrorCode());
    		}
    		
    		return pwd;
    	}
    	public boolean userExists(String username)
    	{
    		// return true is user exists
    		return false;
    	}
    }
    		
  3. 部署这个Class:

    注意:在最新的Wowza Streaming Engine 4中下面的参数usernamePasswordProviderClass已经被改名为securityPublishUsernamePasswordProviderClass
    1. 针对RTMP推流的验证,请在[install-dir]/conf/[application]/Application.xml文件的<Properties>中添加下面的配置参数。
      <Property>
      	<Name>usernamePasswordProviderClass</Name>
      	<Value>com.wowza.wms.example.authenticate.AuthenticateUsernamePasswordProviderExample</Value>
      </Property>
      			
    2. 针对RTSP推流的验证,请在[install-dir]/conf/Authentication.xml /Digest Properties(或/Basic Properties)中添加usernamePasswordProviderClass参数:
      <Method>
      	<Name>digest</Name>
      	<Description>Digest Authentication</Description>
      	<Class>com.wowza.wms.authentication.AuthenticateDigest</Class>
      	<Properties>
      		<Property>
      			<Name>passwordFile</Name
      			<Value>${com.wowza.wms.context.VHostConfigHome}/conf/publish.password</Value>
      		</Property>
      		<Property>
      			<Name>realm</Name>
      			<Value>Streaming Server</Value>
      		</Property>
      		<Property>
      			<Name>usernamePasswordProviderClass</Name>
      			<Value>com.wowza.wms.example.authenticate.AuthenticateUsernamePasswordProviderExample</Value>
      		</Property>
      	</Properties>
      </Method>
      		
  4. 最后,重启Wowza Streaming Engine。