Tuesday, May 6, 2008

Basic Authentication in Sun AppServer

Follow the below steps to configure a particular user with https enabled web app:
1. In the sun-web.xml of the webapp:

<sun-web-app>
<context-root>/test</context-root>
<security-role-mapping>
<role-name>my-role</role-name>
<principal-name>user1</principal-name>
<group-name>my-group</group-name>
</security-role-mapping>
</sun-web-app>


2. In the web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xsi="http://www.w3.org/2001/XMLSchema-instance" schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

<display-name>Archetype Created Web Application</display-name>

<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>

<security-constraint>
<web-resource-collection>
<web-resource-name>Entire Site</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>my-role</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>

<login-config>
<auth-method>BASIC</auth-method>
<realm-name>file</realm-name>
</login-config>

<security-role>
<role-name>my-role</role-name>
</security-role>

</web-app>

3. In Sun App Server admin console:
Under Configuration-->Security-->Realms-->File --> Manage Users
User: user1
Password: xxxx
Confirm Password: xxxx
Group List: my-role

Under Configuration-->Security-->Realms-->Certificate
Property: assign-groups : my-role

Under Configuration-->HTTP Listeners
Add new https listener with port 443 and enable all authentication and ssl'S in that.

Now launch your web app. It should pop up a username and pwd dialog box for basic authentication.

No comments: