How to make apache2 authenticate against MS SQL 2000 Server - Page 4

Want to support HowtoForge? Become a subscriber!
 
Submitted by spats (Contact Author) (Forums) on Fri, 2007-01-12 19:26. ::

5.3. The new testConnection.java

import java.sql.*;
import java.io.*;
import java.text.*;
public class testConnection
{
      static InputStreamReader converter = new InputStreamReader (System.in);
      static BufferedReader      in = new BufferedReader (converter);
      // Read a String from standard system input
      public static String getString() {
        try {
            return in.readLine();
        } catch( Exception e ) {
            System.out.println("getString() exception, returning empty string");
            return "";
        }
      }
    public static void main(String[] args)
    {
      String userName;
      String userPass;
      // userName = args[0];
      userName = getString();
      // userPass = args[1];
      userPass = getString();
    DB db = new DB();
        db.dbConnect(
     "jdbc:jtds:sqlserver://xxx.xxx.xxx.xxx:1433/test",userName,userPass);
    }
}
class DB
{
    public DB() {}
    public void dbConnect(String db_connect_string,
  String db_userid, String db_password)
    {
        try
        {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            Connection conn = DriverManager.getConnection(
    db_connect_string, db_userid, db_password);
            System.out.println("connected");
          System.exit(0);
        }
        catch (Exception e)
        {
            //e.printStackTrace();
            System.out.println("failed");
          System.exit(1);
        }
    }
};

So after the ‘program’ was written I had to compile it and put in some place for apache. So there comes javac again

javac testConnection.java

and we pack it with jar

jar cfm tc.jar manifest DB.class testConnection.class

manifest file should tell the name of main class like that:

Main-Class: testConnection

I copied the tc.jar to /usr/local/testConn/

5.4. Final apache configuration

And after that configured apache to execute it as external authentication program by editing /etc/apache2/default-server.conf and adding following lines:

AddExternalAuth archive_auth "/usr/lib/java/bin/java -jar /usr/local/testConn/tc.jar"
SetExternalAuthMethod archive_auth pipe

Final thing – tell to use particular authentication method for our ‘restricted’ directory in httpd.conf

<Directory /srv/www/htdocs/secretarea>
   Options Indexes FollowSymLinks
    AllowOverride None
    AuthType Basic
    AuthName "Autorizacija!"
    AuthExternal archive_auth
    Require valid-user
</Directory>

After restart of apache if we try to access http://xxx.xxx.xxx.xxx/secretarea - apache prompts for username and password.
User supplies these and apache executes tc.jar and passes user credentials to it. tc.jar talks to MS SQL. MS SQL answers and depending on answer – apache denies or allows proceeding to secured site.


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.