Friday, January 21, 2011

Develop Web Service With Axis2 #9 - HTTP Basic Authentication in Weblogic

My Environment:
JDK v1.6.x
Axis2 V1.5.4
Weblogic v1.03

1) add 'user'/'user group' in weblogic

login weblogic server administration console

select 'Security Realms' under domain structure on the left of admin console page

click 'myrealm' which is the default realm name in weblogic,and open configuration 'Settings for myrealm'

click tab 'Users and Groups' to add user and user group

click sub-tab 'Groups' first to add one new group

click sub-tab 'Users', and click 'New' button to add new user and key in password here as well.

click the user name which was created above hyperlink, and select 'Groups' tab to assign the user to the user group created above

2) add configuration shown below in web.xml

<security-constraint>
   <web-resource-collection>
      <web-resource-name>mywsapi</web-resource-name>
      <url-pattern>/services/{your service name}</url-pattern>
   </web-resource-collection>
   <auth-constraint>
      <role-name>{user name configured in Weblogic}</role-name>
   </auth-constraint>
</security-constraint>
<login-config>
   <auth-method>BASIC</auth-method>
   <realm-name>myrealm</realm-name>   <-- this is the default realm name in weblogic
</login-config>
<security-role>
   <role-name>{user name configured in Weblogic}</role-name>
</security-role>

3) add configuration shown below in weblogic.xml

<security-role-assignment>
   <role-name>{user name configured in Weblogic}</role-name>
   <principal-name>{user group configured in Weblogic}</principal-name>
</security-role-assignment>

4) on stub side, you need to do more to pass user name and password as follows.

Options opt = _stub._getServiceClient().getOptions();
HttpTransportProperties.Authenticator mbAuth = new HttpTransportProperties.Authenticator();
mbAuth.setUsername("{user name configured in weblogic}");
mbAuth.setPassword("{password configured in weblogic}");
mbAuth.setPreemptiveAuthentication(true);
opt.setProperty(HTTPConstants.AUTHENTICATE, mbAuth);
_stub._getServiceClient().setOptions(opt);

No comments:

Post a Comment