Http://www-900.ibm.com/developerWorks/cn/java/l-j2eeesecurity2/index.shtml
The first part of this series of articles introduced J2EE's security concept, verification model and authorization model, which is more important than the theory. The second part of this article will show the reader to the reader how to apply J2EE security services in the development through a specific example. The focus of this section is to apply and practice.
Note: The purpose of this article describes how to apply the security services provided by J2EE, and is not for specific products. Therefore, the author chooses Sun's J2EE reference implementation (J2SDKee) as a demonstration platform. Because J2SDKee is fully developed in accordance with J2EE specification, although it is unlike J2EE products such as IBM WebSphere, BEA WebLogic, it is definitely the ideal platform for learning J2EE. You can get the latest version of the Sun's J2EE reference implementation via http://java.sun.com/j2ee/. This article selection is Sun's J2SDKee1.3.1.
This article will include the following:
An example using HTTP basic verification An example of an EJB method authorized by a form-based authentication, an example of a programmable security and propagation caller identity
Examples of HTTP basic verification
HTTP Basic Verification is a kind of Web client verification, which controls access to protected resources together with the authorization mechanism of the system.
step:
1. Create a J2EE app
The Application menu item in the New submenu is selected in the FILE menu of the application deployment tool (see Figure 1). The new application dialog box will pop up. Fill in the application file name and application display name (see Figure 2).
figure 1
figure 2
2. Create a web component
In the FILE menu of the application deployment tool, select the Web Compent menu item in the New submenu, which will pop up the New Web Component Wizard dialog (see Figure 3). Select CREATE New WAR File in Application, select the application TEST created in step 1 in the drop-down box, fill in the WebAppTest in the War Display Name box. Click the EIDT button in the Content Bar to select the file containing this web component. There is only one webtest.jsp file in this example. Then click Next to enter the next dialog (see Figure 4). Since our web components are a JSP file, select JSP in the component type. Then press NEXT until the end. At this point we have created a web component containing only one JSP file. Next is the step of configuring security properties.
image 3
Figure 4
3. Configuring security properties
3.1 Creating a security role
The web component WebAppTest created in step 2 in the left navigation bar of the deployment tool, select the Roles property page (see Figure 5) in the property page (see Figure 5). Click the Add button to fill in the security role name User, the Description column in the Name column. The security role represents a collection of users with the same security permission.
Figure 5
3.2 Configuring Security Policy
After creating a security role, you should configure the appropriate security policy on the security role. Click the Security Properties page (see Figure 6).
Figure 6
First select the verification method you want, select Basic from the USER Authentication Method drop-down box. This means you will verify the user through the basic HTTP verification mode. Let's work on Web resources. Click on the Add button in the Security Constraint column to add a security constraint, and the constraint name can be customized. Next, add a web resource to the created constraint. First add a resource collection in Web Resource Collections, then select the resources contained in the resource collection. In this example, the WebTest.jsp file is included in the WRCOLLECTION resource collection, or various files belonging to this web component. Next, which web operations are selected to receive constraints. J2SDKee1.3.1 contains only two operations (GET and POST). Different product supports are different, and the development is the operation provided by the specific product. Now you should specify a security role. Click the Edit button in the Authorized Roles bar to pop up the Security Role List dialog box, and select the defined security role. This example is selected. To this, the security policy has been configured, the following steps are mapping the user and user group mapping in the actual environment with the security role. 4. Mapping
In the left navigation bar, select the application test on the right (see Figure 7), select the user in the Role Name Reference column, click the Add button below, will pop up the user and user group list dialog box, and select A user or group mapped into a safe role user. In this case we map the user J2EE to the security role user. This user J2EE will have access authorization assigned to the security role user.
Figure 7
5. Deployment application
Check the web context property page, fill in TEST in the Context root text box, right-click the application TEST of the left navigation bar, select Deploy to complete the application in the pop-up menu. At this time we have completed all the steps of the first example.
Deployment description file
This example uses the illustrative security service, so we do not need to write any security-related code, but is fully implemented through the deployment description file of the component. Here is the deployment description file of this web component.
XML Version = "1.0" encoding = "UTF-8"?>
'- // Sun microsystems, Inc.//dtd Web Application 2.3 // en'
'http://java.sun.com/dtd/web-app_2_3.dtd'>
servlet>
session-config>
Web-Resource-Collection>
auth-constraint>
user-data-connectiont>
security-connectiont>
login-config>
security-role>
web-app>
From the deployment description file, this is a web component called WebAppTest, which contains a file called WebTest.jsp, and only the user or user group that is given the user's security role, it has the right to GET or POST operation on WebTest.jsp. There is no connection to the actual user's mapping, and the DTD of the J2EE deployment description file is not defined for the elements of the mapping for security roles and actual users, because there are a variety of different user systems in the actual environment (such as relational databases, System file form and LDAP system, etc.). Therefore, the method of mapping the safety role and the actual user is made by J2EE product vendors.
Test run results
Open IE, enter http: // localhost: 8000 / test // localhost: 8000 / test / webtest.jsp, the verification dialog box will be placed, requiring the user to provide the username and password (see Figure 8), enter the username J2EE and password J2EE. After verifying the JSP file, WebTest.jsp prints "Hello!" (See Figure 9).
Figure 8
Figure 9
Note: Each step is described in detail in the first example, and there will be some operations as the first example in the next example, so only the steps different from the first example will only describe the first example.
Example of form-based verification
Based on form verification and basic HTTP verification, the basic HTTP verification provides the authentication information dialog provided by the browser to collect user authentication information, and the form-based authentication allows custom login pages to collect user authentication information. This example is substantially the same as the first example, and the different places are to provide the landing page and an error page. Login page login.html