webService验证客户端(HTTP Basic认证)

1. 修改web.xmlweb

在web.xml中添加以下配置:浏览器

<security-role>
  <description>Normal operator user</description>
  <role-name>WsOperator</role-name>
</security-role>
<security-constraint>
  <web-resource-collection>
    <web-resource-name>AxisServlet</web-resource-name>
    <url-pattern>/services/*</url-pattern>
  </web-resource-collection>
  <auth-constraint>
    <role-name>WsOperator</role-name>
  </auth-constraint>
</security-constraint>
<login-config>
  <auth-method>BASIC</auth-method>
</login-config>

其中<web-resource-name>与已配置的<servlet-name>“AxisServlet”保持一致 <url-pattern>配置须要验证的url,/services/* 表示services下的全部请求都要验证 <role-name>配置有权访问此webService资源的角色 <auth-method>配置服务器端的验证模式tomcat

2. 修改tomcat配置服务器

在${TOMCAT_HOME}/conf/tomcat-users.xml中添加以下配置:url

<role rolename="WsOperator"/>  
<user username="test" password="test" roles="WsOperator"/>

web.xml中的<role-name>与此处的rolename保持一致 启动tomcat,在浏览器地址栏中打开webService,会弹出basic验证框 身份验证提示框code

输入正确的用户名和密码便可正常访问 webService描述页面orm

3. 客户端调用xml

将webService资源设置为basic验证后,客户端直接调用会报401(无权限)错误,须要在客户端发起远程调用时设置服务器访问用户名及密码。 使用axis调用方式添加如下代码:ip

call.setProperty(Call.USERNAME_PROPERTY, "test"); 
call.setProperty(Call.PASSWORD_PROPERTY, "test");

若使用RPC远程调用方式添加如下代码便可:资源

options = serviceClient.getOptions();
HttpTransportProperties.Authenticator basicauth = new HttpTransportProperties.Authenticator();
basicauth.setUsername("test");  //服务器访问用户名   
basicauth.setPassword("test"); //服务器访问密码  
options.setProperty(HTTPConstants.AUTHENTICATE, basicauth);
相关文章
相关标签/搜索