您的位置:首页 > 运维架构 > Tomcat

AXIS2访问权限控制利用TOMCAT用户

2011-09-08 17:35 375 查看
1.找到TOMCAT安装目录,找到CONFIG文件夹,查找到Tomcat_Home\conf\tomcat-users.xml文件,添加角色

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>… <role rolename="department-manager"/>

<user username="test" password="test" roles="department-manager"/>


</tomcat-users>

上面配置代码在tomcat配置文件中添加了一个department-manager角色,并且在此角色中添加了一个名为hellking的用户。要使tomcat-users.xml中配置的角色和用户生效,需要配置tomcat使用UserDatabaseRealm。打开Tomcat_Home\conf\server.xml配置文件,在GlobalNamingResources中添加以下描述:

2.在tomcat中添加UserDatabaseRealm

<GlobalNamingResources>...

<Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database

that can be updated and saved"> </Resource> <ResourceParams name="UserDatabase">

<parameter>

<name>factory</name>

<value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>

</parameter><parameter><name>pathname</name> <value>conf/tomcat-users.xml</value> 

</parameter>

</ResourceParams>

</GlobalNamingResources>

然后再web应用的部署描述符中指定Web服务资源的访问控制,如下所示:

3.
<security-constraint>

<web-resource-collection>

<web-resource-name>Tax Web service </web-resource-name>

<url-pattern>/services/PersonalTaxService</url-pattern>

</web-resource-collection>

<auth-constraint>

<role-name>department-manager</role-name> <
/auth-constraint>
</security-constraint>
<login-config>

<auth-method>BASIC</auth-method>

<realm-name>Axis Basic Authentication Area</realm-name>
</login-config>
<security-role>

<role-name>department-manager</role-name>
</security-role>

url-pattern指定了需要通过角色验证的URL样式,在这里是"/services/PersonalTaxService";role-name是能够访问制定URL的角色,这里是department-manager。以上配置的意思是只有角色类型是"department-manager"的用户才能访问URL样式为"/services/PersonalTaxService"Web服务。

4.客户端调用:
public String ClientAccount(CDto abDto){

RPCServiceClient serviceClient = null;

String para=null;

String xmlString = null;

try {

serviceClient = new RPCServiceClient();

Options options = serviceClient.getOptions();

EndpointReference targetEPR = new EndpointReference(ClientReadProperties.getInstence().getProperty("Select"));

options.setTo(targetEPR);

HttpTransportProperties.Authenticator authenticator = new HttpTransportProperties.Authenticator();

authenticator.setPreemptiveAuthentication(true);

authenticator.setUsername("test");

authenticator.setPassword("test");

options.setProperty(HTTPConstants.AUTHENTICATE, authenticator);

QName qname = new QName(ClientReadProperties.getInstence().getProperty("Qurl"),"Select");

para=ObjectToXML.ObjectToXMLString(abDto);//封装成XML格式字符串

xmlString = (String) serviceClient.invokeBlocking(qname,new Object[] { para },new Class[] { String.class })[0];

} catch (Exception e) {

e.getStackTrace();

}

return xmlString;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: