Authenticator Class

This class is used to authenticate requests via the HttpServlet class. It supports both "BASIC" and "NTLM" authentication methods. Also includes a handleRequest() method that can be used to handle authentication workflows used in JavaXT Express web applications. Instances of this class are passed to the setAuthenticator() method in the javaxt.http.servlet.HttpServlet class. The following snippit can be used to perform "BASIC" authentication with a username and password. This example assumes that there is a "User" class that implements the java.security.Principal interface. The getUser() and setUser() methods are used to update an internal cache.
        setAuthenticator(new javaxt.express.Authenticator(){

            public java.security.Principal getPrinciple(){

                User user = (User) getUser();
                if (user!=null) return user;

                try{

                    String[] credentials = getCredentials();
                    String username = credentials[0];
                    String password = credentials[1];

                    if (username!=null && password!=null){

                        //TODO: Find user in the database
                    }
                }
                catch(Exception e){
                }

                setUser(user);
                return user;
            }

        });     

Constructors

There are no public constructors.

Public Methods

newInstance( HttpServletRequest request ) returns Authenticator
Creates a new instance of this class. This method is called whenever a new HTTP request is made to the server (see HttpServletRequest class)
getPrinciple( ) returns java.security.Principal
Returns the java.security.Principal associated with an HTTP request. Override this method!
getCredentials( ) returns String[]
Returns the credentials associated with an HTTP request. In the case of "BASIC" authentication, the credentials contain the username and password. In the case of "NTLM" authentication, the credentials only contain a username.
authenticate( ) returns void
Used to authenticate a client request. If the Authenticator fails to authenticate the client, this method throws a ServletException.
getAuthType( ) returns String
Returns the authentication scheme used to authenticate clients (e.g. "BASIC" or "NTLM").
isUserInRole( String role ) returns boolean
This method is a legacy feature from the Java Servlet API.
handleRequest( String service, HttpServletResponse response ) returns boolean
Used to process an authentication workflow. Returns true if a response was returned to the client. Example usage:
    public void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        Authenticator authenticator = (Authenticator) getAuthenticator(request);
        if (!authenticator.handleRequest(service, response)){

            //TODO: Send a response (file, json, text, etc)
        }
    }    

Static Methods

sendNTLMResponse( HttpServletRequest request, HttpServletResponse response ) returns boolean
Returns true if an NTLM response was returned to the client