webService验证客户端(HTTP Basic认证)
1. 修改web.xml 在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>配置服务器端的验证模式 2. 修改tomcat配置 在${TOMCAT_HOME}/conf/tomcat-users.xml中添加如下配置: <role rolename="WsOperator"/> <user username="test" password="test" roles="WsOperator"/> web.xml中的<role-name>与此处的rolename保持一致 启动tomcat,在浏览器地址栏中打开webService,会弹出basic验证框 输入正确的用户名和密码即可正常访问 3. 客户端调用 将webService资源设置为basic验证后,客户端直接调用会报401(无权限)错误,需要在客户端发起远程调用时设置服务器访问用户名及密码。 使用axis调用方式添加以下代码: 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); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |