⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch13s78.html

📁 详细介绍了jboss3.0的配置等
💻 HTML
📖 第 1 页 / 共 5 页
字号:
            SessionHome home = (SessionHome) iniContext.lookup(example+"/StatefulSession");
            System.out.println("Found StatefulSessionHome");
            Session bean = home.create();
            System.out.println("Created StatefulSession");
            bean.noop();
            System.out.println("Bean.noop() called");
            System.out.println("Bean.echo('Hello') -> "+bean.echo("Hello"));
            bean.remove();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}
</pre></div><div class="figure"><p><a name="SecureEJBServlet.java"></a><b>Figure 13.19. The Secure Servlet</b></p><pre class="programlisting">
import java.io.IOException;
import java.io.PrintWriter;
import java.security.Principal;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Session;
import SessionHome;

/** A simple servlet that accesses the stateless session bean.
@author Scott_Stark@displayscape.com
*/
public class SecureEJBServlet extends HttpServlet
{
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        String echoMsg = null;
        try
        {
            InitialContext ctx = new InitialContext();
            SessionHome home = (SessionHome) ctx.lookup("java:comp/env/ejb/SecuredEJB");
            Session bean = home.create();
            echoMsg = bean.echo("Hello");
        }
        catch(Exception e)
        {
            throw new ServletException("Failed to call SecuredEJB.echo", e);
        }
        Principal user = request.getUserPrincipal();
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("&lt;html&gt;");
        out.println("&lt;head&gt;&lt;title&gt;SecureEJBServlet&lt;/title&gt;&lt;/head&gt;");
        out.println("&lt;h1&gt;SecureServlet Accessed&lt;/h1&gt;");
        out.println("&lt;body&gt;&lt;pre&gt;You have accessed this servlet as user: "+user);
        out.println("The SecuredEJB.echo('Hello') returned: "+echoMsg);
        out.println("&lt;/pre&gt;&lt;/body&gt;&lt;/html&gt;");
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        processRequest(request, response);
    }
}
</pre></div><div class="figure"><p><a name="web-app.xml"></a><b>Figure 13.20. The web-app Deployment Descriptor</b></p><pre class="programlisting">
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"&gt;

&lt;web-app&gt;
&lt;!-- ### Servlets --&gt;
    &lt;servlet&gt;
        &lt;servlet-name&gt;SecureServlet&lt;/servlet-name&gt;
        &lt;servlet-class&gt;SecureEJBServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;SecureServlet&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/restricted/SecureServlet&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;

&lt;!-- ### Security --&gt;
    &lt;security-constraint&gt;
        &lt;web-resource-collection&gt;
            &lt;web-resource-name&gt;Restricted&lt;/web-resource-name&gt;
            &lt;description&gt;Declarative security tests&lt;/description&gt;
            &lt;url-pattern&gt;/restricted/*&lt;/url-pattern&gt;
            &lt;http-method&gt;HEAD&lt;/http-method&gt;
            &lt;http-method&gt;GET&lt;/http-method&gt;
            &lt;http-method&gt;POST&lt;/http-method&gt;
            &lt;http-method&gt;PUT&lt;/http-method&gt;
            &lt;http-method&gt;DELETE&lt;/http-method&gt;
        &lt;/web-resource-collection&gt;
        &lt;auth-constraint&gt;
            &lt;role-name&gt;Echo&lt;/role-name&gt;
        &lt;/auth-constraint&gt;
        &lt;user-data-constraint&gt;
            &lt;description&gt;no description&lt;/description&gt;
            &lt;transport-guarantee&gt;NONE&lt;/transport-guarantee&gt;
        &lt;/user-data-constraint&gt;
    &lt;/security-constraint&gt;

    &lt;login-config&gt;
        &lt;auth-method&gt;BASIC&lt;/auth-method&gt;
        &lt;realm-name&gt;JAAS Tutorial Servlets&lt;/realm-name&gt;
    &lt;/login-config&gt;

    &lt;security-role&gt;
        &lt;description&gt;A user allowed to invoke echo methods&lt;/description&gt;
        &lt;role-name&gt;Echo&lt;/role-name&gt;
    &lt;/security-role&gt;

&lt;!-- ### EJB References (java:comp/env/ejb) --&gt;
    &lt;ejb-ref&gt;
        &lt;ejb-ref-name&gt;ejb/SecuredEJB&lt;/ejb-ref-name&gt;
        &lt;ejb-ref-type&gt;Session&lt;/ejb-ref-type&gt;
        &lt;home&gt;SessionHome&lt;/home&gt;
        &lt;remote&gt;Session&lt;/remote&gt;
    &lt;/ejb-ref&gt;

&lt;/web-app&gt;
</pre></div><div class="figure"><p><a name="jboss-web.xml"></a><b>Figure 13.21. The jboss-web.xml Deployment Descriptor</b></p><pre class="programlisting">
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;jboss-web&gt;
    &lt;security-domain&gt;java:/jaas/example1&lt;/security-domain&gt;

    &lt;ejb-ref&gt;
        &lt;ejb-ref-name&gt;ejb/SecuredEJB&lt;/ejb-ref-name&gt;
        &lt;jndi-name&gt;example1/StatelessSession&lt;/jndi-name&gt;
    &lt;/ejb-ref&gt;
&lt;/jboss-web&gt;
</pre></div><p>The session beans are trivial and both the stateless and stateful bean use the
same home and remote interfaces. The client is also trivial except for the use
of a JAAS LoginContext and CallbackHandler implementation. This is how a client
establishes the username and password that is sent to JBoss. The servlet looks up
the home interface of the StatelessSession bean and creates a Session instance.
It then invokes echo on this and displays the user principal that accessed the
servlet and the output of the Session.echo method.
Now, finally let's put everything together and deploy the session beans
and servlet as a J2EE ear.</p></div><div class="section"><a name="jaas5"></a><div class="titlepage"><div><h3 class="title"><a name="jaas5"></a>Deploying a Secured J2EE Ear</h3></div></div><p>This section details the procedure for building, deploying and testing
a secured J2EE ear made up of the EJBs and servlet shown in the preceeding figures. 
I'll walk you through the build of two different versions of the ears using Ant
and the build.xml file contained in the tutorial files bundle.
The files bundle is available from <a href="javascript:if(confirm('http://www.jboss.org/doco_files/jaas-howto.zip  \n\nThis file was not retrieved by Teleport Pro, because it did not meet the project\'s file type specifications.  \n\nDo you want to open it from the server?'))window.location='http://www.jboss.org/doco_files/jaas-howto.zip'" tppabs="http://www.jboss.org/doco_files/jaas-howto.zip" target="_top">JAAS-Howto Files</a>.</p><div class="procedure"><p><a name="d0e11043"></a><b>Procedure 13.3. Deployment Steps</b></p><ol type="1"><li><a name="d0e11046"></a><p>Download the JBoss/Tomcat bundle. Go to<a href="javascript:if(confirm('http://sourceforge.net/projects/jboss/  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://sourceforge.net/projects/jboss/'" tppabs="http://sourceforge.net/projects/jboss/" target="_top">JBoss @ SourceForge</a>and download the JBoss-2.2.2/Tomcat-3.2.2 bundle or later version. The 2.2.2 version of
JBoss is the first to ship with the integrated ejb/web security that is required for this
tutorial example. Unpack the archive to create a JBoss-2.2.2_Tomcat-3.2.2 directory
that contains jboss and tomcat subdirectories. I have downloaded and unpacked the
archive into /tmp on my machine so the directory structure looks like that shown
in <a href="ch13s78.html#jaas.bundle.structure" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s78.html#jaas.bundle.structure" title="Figure 13.22. JBoss-2.2.2 Tomcat-3.2.2 Bundle Structure">Figure 13.22</a>. The jboss.dist and servlet.jar labels
in red will be used below to configure the Ant build script.</p><div class="figure"><p><a name="jaas.bundle.structure"></a><b>Figure 13.22. JBoss-2.2.2 Tomcat-3.2.2 Bundle Structure</b></p><div class="mediaobject"><img src="jaas_bundle_structure.jpg" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/jaas_bundle_structure.jpg"></div></div></li><li><a name="d0e11060"></a><p>Download the tutorial files bundle. If you have not already, download
the tutorial files from here: <a href="javascript:if(confirm('http://www.jboss.org/doco_files/jaas-howto.zip  \n\nThis file was not retrieved by Teleport Pro, because it did not meet the project\'s file type specifications.  \n\nDo you want to open it from the server?'))window.location='http://www.jboss.org/doco_files/jaas-howto.zip'" tppabs="http://www.jboss.org/doco_files/jaas-howto.zip" target="_top">JAAS-Howto Files</a>.
Unpack the archive to create a jaas directory with the contents shown in <a href="ch13s78.html#jaas.files" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s78.html#jaas.files" title="Figure 13.23. JAAS Tutorial Files">Figure 13.23</a>.<div class="figure"><p><a name="jaas.files"></a><b>Figure 13.23. JAAS Tutorial Files</b></p><div class="mediaobject"><img src="jaas_files.jpg" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/jaas_files.jpg"></div></div>
				</p></li><li><a name="d0e11075"></a><p>Build and deploy the tutorial ears.</p><ol type="a"><li><a name="d0e11079"></a><p>Edit the build.xml to set the correct location of
the jboss.dist and servlet.jar variables to the corresponding paths of your
JBoss/Tomcat bundle distribution installation. See <a href="ch13s78.html#jaas.bundle.structure" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s78.html#jaas.bundle.structure" title="Figure 13.22. JBoss-2.2.2 Tomcat-3.2.2 Bundle Structure">Figure 13.22</a>for the items in the distribution to which jboss.dist and servlet.jar refer. For
my installation of the distribution under /tmp as shown above, the correct settings
are given in <a href="ch13s78.html#build.xml.paths" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s78.html#build.xml.paths" title="Figure 13.24. JAAS Tutorial Files">Figure 13.24</a>.<div class="figure"><p><a name="build.xml.paths"></a><b>Figure 13.24. JAAS Tutorial Files</b></p><pre class="programlisting">
&lt;project name="JAAS Howto Build Script" default="ears" basedir="."&gt;
...
    &lt;!-- Override with your JBoss server dist location --&gt;
    &lt;property name="jboss.dist" value="/tmp/JBoss-2.2.2_Tomcat-3.2.2/jboss"/&gt;
    &lt;!-- Override with your web server servlet jar location --&gt;
    &lt;property name="servlet.jar" value="/tmp/JBoss-2.2.2_Tomcat-3.2.2/tomcat/lib/servlet.jar"/&gt;
</pre></div>    
						</p></li><li><a name="d0e11092"></a><p>Download and install Ant1.3 or later. If you don't
have a copy of Ant on your system download version 1.3 or later from the Apache
site <a href="javascript:if(confirm('http://jakarta.apache.org/ant/index.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://jakarta.apache.org/ant/index.html'" tppabs="http://jakarta.apache.org/ant/index.html" target="_top">here</a>.</p></li><li><a name="d0e11098"></a><p>Build and install the tutorial ears. From within
the jaas directory that contains the Ant build.xml file, execute the following
command:<div class="literallayout">&nbsp;&nbsp;&nbsp;&nbsp;<br>
								<b>bash-2.04$&nbsp;ant&nbsp;ears<br>
Searching&nbsp;for&nbsp;build.xml&nbsp;...<br>
Buildfile:&nbsp;/tmp/jaas/build.xml<br>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -