<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/apache-tomcat-http-basic-and-digest-authen/</link>
<title><![CDATA[tomcat http Basic 和 Digest 认证 ]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[Web开发]]></category>
<pubDate>Wed, 03 Aug 2011 14:43:10 +0000</pubDate> 
<guid>https://www.heckjj.com/apache-tomcat-http-basic-and-digest-authen/</guid> 
<description>
<![CDATA[ 
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-family: 微软雅黑;">本文主要讲述如何在tomcat中配置Basic认证以及工作流程： <br/>Tomcat配置: <br/>1 在tomcat的webapps下新建一个目录authen，再建立子目录subdir,下面放一个index.jsp <br/><br/>2 在authen目录下建立WEB-INF目录，下放web.xml文件，内容如下:</span> <br/><textarea name="code" class="xml" rows="15" cols="100">
&lt;security-constraint&gt;
&nbsp;&nbsp;&lt;web-resource-collection&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;web-resource-name&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;My App
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/web-resource-name&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;url-pattern&gt;/subdir/*&lt;/url-pattern&gt;
&nbsp;&nbsp;&lt;/web-resource-collection&gt;
&nbsp;&nbsp;&lt;auth-constraint&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;role-name&gt;manager&lt;/role-name&gt;
&nbsp;&nbsp;&lt;/auth-constraint&gt;
&lt;/security-constraint&gt;

&lt;login-config&gt;
&nbsp;&nbsp;&lt;auth-method&gt;BASIC&lt;/auth-method&gt; &lt;!-- 如果是DIGEST认证这里填DIGEST --&gt;&nbsp;&nbsp;
&nbsp;&nbsp;&lt;realm-name&gt;My Realm&lt;/realm-name&gt;
&lt;/login-config&gt;
</textarea><br/><span style="font-family: 微软雅黑;">3 在tomcat的tomcat-users.xml文件中添加一个用户名manager密码为admin，manager的用户，角色manager。 <br/>客户端访问： <br/>访问http://localhost:port/authen/subdir/index.jsp <br/>会弹出对话框提示认证，输入manager admin可以登录。</span><span style="font-family: 微软雅黑;"><br/><br/>工作流程（通过firebug可以查看请求头） <br/>1 客户端先发请求（不知道要认证，头里不包含任何特殊信息） <br/><br/>2 服务器发一个401返回，并含有下面的头 <br/>WWW-Authenticate Basic realm=&quot;My Realm&quot; <br/> <br/>3 客户端认证，含有下面的头 <br/>Authorization Basic dGVzdDp0ZXN0 <br/>“dGVzdDp0ZXN0”是&quot;test:test&quot;的Base64编码。 (可以通过php函数base64_encode()验证) <br/><br/>缺点: <br/>密码明文传输，非常不安全。 <br/><br/>httpclient中的实现:<br/>查看org.apache.commons.httpclient.auth包的BasicScheme类</span><br/><textarea name="code" class="java" rows="15" cols="100">
&nbsp;&nbsp; // Copy from the httpclient source code
&nbsp;&nbsp;&nbsp;&nbsp;// Omit some codes
&nbsp;&nbsp;&nbsp;&nbsp;public static String authenticate(UsernamePasswordCredentials credentials, String charset) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringBuffer buffer = new StringBuffer();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer.append(credentials.getUserName());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer.append(&quot;:&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer.append(credentials.getPassword());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &quot;Basic &quot; + EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getBytes(buffer.toString(), charset)));
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
</textarea><br/>Tags - <a href="https://www.heckjj.com/tags/tomcat/" rel="tag">tomcat</a> , <a href="https://www.heckjj.com/tags/http/" rel="tag">http</a> , <a href="https://www.heckjj.com/tags/basic%25E5%2592%258Cdigest%25E8%25AE%25A4%25E8%25AF%2581/" rel="tag">basic和digest认证</a>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/apache-tomcat-http-basic-and-digest-authen/#blogcomment</link>
<title><![CDATA[[评论] tomcat http Basic 和 Digest 认证 ]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/apache-tomcat-http-basic-and-digest-authen/#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>