加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice tomcat7.0.27 基本用户认证配置及客户端代码调用

发布时间:2020-12-17 00:18:13 所属栏目:安全 来源:网络整理
导读:webservice tomcat7.0.27 基本用户认证配置 1.tomcat-users.xml文件xml元素tomcat-users中间配置访问的角色和用户如下: role rolename="webservice"/ user username="webservice_user" password="webservice_user" roles="webservice"/ 2.hysh web项目配置

webservice tomcat7.0.27 基本用户认证配置
1.tomcat-users.xml文件xml元素tomcat-users中间配置访问的角色和用户如下:
<role rolename="webservice"/>
<user username="webservice_user" password="webservice_user" roles="webservice"/>
2.hysh web项目配置文件web.xml配置如下片段
<security-constraint>
?<web-resource-collection>
??<web-resource-name>secured services</web-resource-name>
??<url-pattern>/services/*</url-pattern>
?</web-resource-collection>
?<auth-constraint>
??<role-name>webservice</role-name>
?</auth-constraint>
</security-constraint>

<login-config>
?<auth-method>BASIC</auth-method>
?<realm-name>webservice</realm-name>
</login-config>
3.web浏览器输入符合url-pattern对应路径时会弹出登录认证对话框,输入tomcat-users.xml文件中设置的username,password便能正常显示。
路径如:http://www.helloworlddemo.com:8080/test/services/WebService?wsdl。
如输入的username,password不正确,将不能登录。
4.Axis2 webservice客户端调用代码加入基本验证
ServiceClient client = null;
Options options = null;
OMElement response = null;
Authenticator authenticator = null;

authenticator = new Authenticator();

List<String> auth = new ArrayList<String>();
auth.add(Authenticator.BASIC);
authenticator.setAuthSchemes(auth);

// Add user creadentials to the transport header
authenticator.setUsername("webservice_user");
authenticator.setPassword("webservice_user");
authenticator.setRealm("webservice");
authenticator.setPreemptiveAuthentication(true);

options.setProperty(HTTPConstants.AUTHENTICATE,authenticator);
client.setOptions(options);

如果没有加入options.setProperty(HTTPConstants.AUTHENTICATE,authenticator); 代码访问将会抛出异常如下: ?Exception in thread "main" org.apache.axis2.AxisFault: Transport error: 401 Error: Unauthorized

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读