KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > web > security > authenticators > HeaderAuthenticator


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.web.security.authenticators;
23
24 import java.io.IOException JavaDoc;
25 import java.security.Principal JavaDoc;
26
27 import org.apache.catalina.Realm;
28 import org.apache.catalina.Session;
29 import org.apache.catalina.authenticator.AuthenticatorBase;
30 import org.apache.catalina.authenticator.Constants;
31 import org.apache.catalina.connector.Request;
32 import org.apache.catalina.connector.Response;
33 import org.apache.catalina.deploy.LoginConfig;
34 import org.jboss.logging.Logger;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 //$Id: HeaderAuthenticator.java 41810 2006-03-07 05:17:41Z asaldhana $
38

39 /**
40  * Test Authenticator that can authenticate based on headers.
41  * username = JBOSS_TEST_USER_NAME
42  * credential = JBOSS_TEST_CREDENTIAL
43  * @author <a HREF="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
44  * @since Mar 6, 2006
45  * @version $Revision: 41810 $
46  */

47 public class HeaderAuthenticator extends AuthenticatorBase
48 {
49    private static Logger log = Logger.getLogger(HeaderAuthenticator.class);
50    
51    /**
52     * Create a new HeaderAuthenticator.
53     */

54    public HeaderAuthenticator()
55    {
56       super();
57    }
58
59    /**
60     * Authenticate the user making this request, based on the specified
61     * login configuration. Return <code>true</code> if any specified
62     * constraint has been satisfied, or <code>false</code> if we have
63     * created a response challenge already.
64     *
65     * @param request Request we are processing
66     * @param response Response we are creating
67     * @param config Login configuration describing how authentication
68     * should be performed
69     *
70     * @exception IOException if an input/output error occurs
71     */

72    protected boolean authenticate(Request request,
73                                            Response response,
74                                            LoginConfig config)
75        throws IOException JavaDoc
76    {
77       Realm realm = context.getRealm();
78       /**
79        * You can get the userid/credential from the header
80        */

81       Session session = request.getSessionInternal(true);
82       String JavaDoc username = request.getHeader("JBOSS_TEST_USER_NAME");
83       String JavaDoc password = request.getHeader("JBOSS_TEST_CREDENTIAL");
84       log.debug("Test UserName =" + username);
85       log.debug("Test cred present?:" + (password != null));
86       Principal JavaDoc principal = realm.authenticate(username,password);
87       if(principal == null)
88       {
89          response.sendError(HttpServletResponse.SC_FORBIDDEN);
90          return false;
91       }
92          
93       //Save the authenticated Principal in our session
94
session.setNote(Constants.SESS_USERNAME_NOTE, principal);
95       request.setUserPrincipal(principal);
96       return true;
97    }
98
99 }
100
Popular Tags