当前页面: 开发资料首页 → Netbeans 专题 → Building Secure Enterprise Beans in NetBeans IDE
摘要: NetBeans IDE 4.1 does not contain support for the security features of the enterprise tier. This means that the deployment descriptor visual editors for enterprise applications do not contain fields to automatically configure security settings. These features are coming in future releases, but in the meantime you have to configure the deployment descriptors by hand. This is really not as complicated as it seems.
This tutorial shows you how to do the following:
Before you begin, you have to install Sun Java System (SJS) Application Server Platform Edition 8.1 (download) on your computer. You cannot deploy enterprise applications to the bundled Tomcat server. You also have to register the application server in the IDE by choosing Tools > Server Manager.
In our example, only users from the bank_users can access our enterprise bean. We'll create the bank_users group's users in the file security realm on the SJS Application server.
Our enterprise application consists of a simple session bean and a web client that attempts to access it.
Our session bean doesn't do anything fancy. It just takes a name and returns it with a sample balance amount.
private String amount = "250";
public String getStatus(String user) {
    return "In account " + user + " is " + amount + "$";
}
    Now let's create a web application that will attempt to access the session bean.
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
   response.setContentType("text/html");
   PrintWriter out = response.getWriter();
   out.println("<html>");
   out.println("<head>");
   out.println("<title>Servlet customerDetail</title>");
   out.println("</head>");
   out.println("<body>");
   out.println("<h1>Servlet customerDetail at " + request.getContextPath () + "</h1>");
   out.println(lookupAccountStatusBean().getStatus(request.getUserPrincipal().getName()));
   out.println("</body>");
   out.println("</html>");
   out.close();
}
    Now we have to edit the deployment descriptors by hand to set up the enterprise bean's security.
...
  <assembly-descriptor>
    <security-role>
      <role-name>USERS</role-name>
    </security-role>
    <method-permission>
      <role-name>USERS</role-name>
      <method>
        <ejb-name>AccountStatusBean</ejb-name>
        <method-name>*</method-name>
      </method>
    </method-permission>
    <container-transaction>
...
      This code sets up a security role named USERS, then gives anyone in the USERS role access to all of the methods in the bean whose ejb-name is AccountStatusBean.
...
  </welcome-file-list>
  <security-role>
    <description>Bank's users</description>
    <role-name>USERS</role-name>
  </security-role>
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Bank-security</web-resource-name>
      <description>Account information</description>
      <url-pattern>/AccountState</url-pattern>
      <http-method>GET</http-method>
      <http-method>POST</http-method>
    </web-resource-collection>
    <auth-constraint>
      <role-name>USERS</role-name>
    </auth-constraint>
  </security-constraint>
  <login-config>
    <auth-method>BASIC</auth-method>
  </login-config>
<ejb-local-ref>
...
      The security-role element declares a USERS security role just like in the EJB deployment descriptor. The security-constraint element limits access to the resources defined in web-resource-collection (in this case, everything matching the URL pattern /AccountState) to the group defined in auth-constraint (USERS). The login-config element specifies that the user will be asked in a standard dialog box to enter their user name and password.
Now we just have to declare the USERS role in the enterprise application and map it to the bank_users security group on the application server.
...
</module>
  <security-role>
    <role-name>USERS</role-name>
  </security-role> 
</application>
...
    ...
  </web>
  <security-role-mapping>
    <role-name>USERS</role-name>
    <group-name>bank_users</group-name>
  </security-role-mapping>
</sun-application>
...
    
In account manager is 250$