小试tomcat基本安全认证

背景:tomcat-5.5.12设置为远程服务器
服务端设置:
Step 1) 打开${tomcat_home}/conf/tomcat-users.xml
加入如下代码:
<rolerolename = " test " />
<userusername
= " a " password = " b " roles = " test " />
Step2)修改web.xml
< security-constraint >
< display-name > ExampleSecurityConstraint </ display-name >
< web-resource-collection >
< web-resource-name > ProtectedArea </ web-resource-name >
<!-- Definethecontext-relativeURL(s)tobeprotected -->
< url-pattern > /test/* </ url-pattern >
<!-- Ifyoulisthttpmethods,onlythosemethodsareprotected -->
< http-method > DELETE </ http-method >
< http-method > GET </ http-method >
< http-method > POST </ http-method >
< http-method > PUT </ http-method >
</ web-resource-collection >
< auth-constraint >
<!-- Anyonewithoneofthelistedrolesmayaccessthisarea -->
< role-name > test </ role-name >
</ auth-constraint >
</ security-constraint >

<!-- DefaultloginconfigurationusesBASICauthentication -->
< login-config >
< auth-method > BASIC </ auth-method >
< realm-name > WebServiceForm-BasedAuthenticationArea </ realm-name >

</ login-config >
对网站/test/下的任何请求采用基本安全认证
客户端编程:
客户端通过httpclient-2.0.2请求该url,方法如下:
HttpClienthttpClient = new HttpClient();

Credentialsdefaultcreds
= new UsernamePasswordCredentials( " a " , " b " );
httpClient.getState().setCredentials(
" WebServiceForm-BasedAuthenticationArea " , " www.cat.cn " ,defaultcreds);

Stringurl
= " http://www.cat.cn/test/index.do " ;

GetMethodmethod
= new GetMethod(url);

method.setDoAuthentication(
true );

httpClient.executeMethod(method);

Strings
= method.getResponseBodyAsString();

System.out.println(s);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值