KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > jee > taglib > TagUtils


1 /*
2 jGuard is a security framework based on top of jaas (java authentication and authorization security).
3 it is written for web applications, to resolve simply, access control problems.
4 version $Name$
5 http://sourceforge.net/projects/jguard/
6
7 Copyright (C) 2004 Charles GAY
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library 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. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 jGuard project home page:
25 http://sourceforge.net/projects/jguard/
26
27 */

28 package net.sf.jguard.jee.taglib;
29
30 import java.io.IOException JavaDoc;
31
32 import javax.security.auth.Subject JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import javax.servlet.http.HttpSession JavaDoc;
36 import javax.servlet.jsp.JspTagException JavaDoc;
37 import javax.servlet.jsp.PageContext JavaDoc;
38
39 import net.sf.jguard.ext.SecurityConstants;
40 import net.sf.jguard.jee.authentication.http.HttpAuthenticationUtils;
41 import net.sf.jguard.jee.authentication.http.HttpConstants;
42
43 public class TagUtils {
44
45     
46     /**
47      * grab the Subject from the user's session and authenticate
48      * it if Subject is null.
49      * @param pageContext
50      * @return subject
51      * @throws JspTagException
52      */

53     protected static Subject JavaDoc getSubject(PageContext JavaDoc pageContext) throws JspTagException JavaDoc {
54         HttpSession JavaDoc session = pageContext.getSession();
55         HttpAuthenticationUtils httpUtils = (HttpAuthenticationUtils)session.getAttribute(HttpConstants.AUTHN_UTILS);
56         Subject JavaDoc subject = null;
57         if(httpUtils!=null){
58             subject = httpUtils.getSubject();
59         }
60
61         boolean local = true;
62         String JavaDoc authenticationScope = (String JavaDoc)pageContext.getServletContext().getAttribute(SecurityConstants.AUTHENTICATION_SCOPE);
63         if(SecurityConstants.JVM_SCOPE.equalsIgnoreCase(authenticationScope)){
64             local = false;
65         }
66         if(subject==null ||session.getAttribute(HttpConstants.AUTHN_UTILS)==null){
67             try {
68                 HttpAuthenticationUtils.authenticate((HttpServletRequest JavaDoc)pageContext.getRequest(),(HttpServletResponse JavaDoc)pageContext.getResponse(),false,local);
69                 //we grab the subject authenticated
70
subject = ((HttpAuthenticationUtils)session.getAttribute(HttpConstants.AUTHN_UTILS)).getSubject();
71             } catch (IOException JavaDoc e) {
72                 throw new JspTagException JavaDoc(e);
73             }
74         }
75         return subject;
76     }
77
78 }
79
Popular Tags