KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > filters > PasswordFilter


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29 package com.caucho.filters;
30
31 import javax.servlet.Filter JavaDoc;
32 import javax.servlet.FilterChain JavaDoc;
33 import javax.servlet.FilterConfig JavaDoc;
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.ServletRequest JavaDoc;
36 import javax.servlet.ServletResponse JavaDoc;
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import java.io.IOException JavaDoc;
39
40 /**
41  * Save a password that the user submits as the session attribute
42  * 'java.naming.security.credentials'.
43  * <p>
44  * Enable with:
45  * <pre>
46  * &lt;filter filter-name='password'
47  * filter-class='com.caucho.filters.PasswordFilter'/&gt;
48  *
49  * &lt;filter-mapping filter-name='password'
50  * url-pattern='j_security_check'/&gt;
51  * </pre>
52  *
53  * Test with this in a JSP:
54  * <pre>
55  * &lt;% if (request.getUserPrincipal() != null) { %&gt;
56  * username: &lt;%= request.getRemoteUser() %&gt;
57  * password: &lt;%= session.getAttribute("java.naming.security.credentials") %&gt;
58  * &lt;% } %&gt;
59  * </pre>
60  * This will work with a <b>form</b> based login.
61  */

62
63 public class PasswordFilter implements Filter JavaDoc {
64     
65   public void init(FilterConfig JavaDoc config)
66   {
67   }
68     
69   public void doFilter(ServletRequest JavaDoc request,
70                ServletResponse JavaDoc response,
71                FilterChain JavaDoc next)
72     throws IOException JavaDoc, ServletException JavaDoc
73   {
74     HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) request;
75     
76     String JavaDoc password = req.getParameter("j_password");
77     req.getSession().setAttribute("java.naming.security.credentials", password);
78     
79     next.doFilter(request, response);
80   }
81     
82   public void destroy()
83   {
84   }
85 }
86
Popular Tags