Http认证之Basic认证
2016-05-11 10:04
736 查看
文章主要讲如何在tomcat中配置Basic认证以及工作流程:
Tomcat配置:
1 在tomcat的webapps下新建一个目录authen,再建立子目录subdir,下面放一个index.jsp
2 在authen目录下建立WEB-INF目录,下放web.xml文件,内容如下
Xml代码
<security-constraint>
<web-resource-collection>
<web-resource-name>
My App
</web-resource-name>
<url-pattern>/subdir/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>test</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>My Realm</realm-name>
</login-config>
3 在tomcat的tomcat-users.xml文件中添加一个用户名密码为test,test的用户,角色test。
客户端访问:
访问http://localhost:port/authen/subdir/index.jsp
会弹出对话框提示认证,输入test test可以登录。
工作流程(通过firebug可以查看请求头)
1 客户端先发请求(不知道要认证,头里不包含任何特殊信息)
2 服务器发一个401返回,并含有下面的头
WWW-Authenticate Basic realm="My Realm"
3 客户端认证,含有下面的头
Authorization Basic dGVzdDp0ZXN0
“dGVzdDp0ZXN0”是"test:test"的Base64编码。 (可以通过php函数base64_encode()验证)
缺点:
密码明文传输,非常不安全。
httpclient中的实现
Java代码
查看org.apache.commons.httpclient.auth包的BasicScheme类
// Copy from the httpclient source code
// Omit some codes
public static String authenticate(UsernamePasswordCredentials credentials, String charset) {
...
StringBuffer buffer = new StringBuffer();
buffer.append(credentials.getUserName());
buffer.append(":");
buffer.append(credentials.getPassword());
return "Basic " + EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getBytes(buffer.toString(), charset)));
}
Tomcat配置:
1 在tomcat的webapps下新建一个目录authen,再建立子目录subdir,下面放一个index.jsp
2 在authen目录下建立WEB-INF目录,下放web.xml文件,内容如下
Xml代码
<security-constraint>
<web-resource-collection>
<web-resource-name>
My App
</web-resource-name>
<url-pattern>/subdir/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>test</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>My Realm</realm-name>
</login-config>
<security-constraint> <web-resource-collection> <web-resource-name> My App </web-resource-name> <url-pattern>/subdir/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>test</role-name> </auth-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> <realm-name>My Realm</realm-name> </login-config>
3 在tomcat的tomcat-users.xml文件中添加一个用户名密码为test,test的用户,角色test。
客户端访问:
访问http://localhost:port/authen/subdir/index.jsp
会弹出对话框提示认证,输入test test可以登录。
工作流程(通过firebug可以查看请求头)
1 客户端先发请求(不知道要认证,头里不包含任何特殊信息)
2 服务器发一个401返回,并含有下面的头
WWW-Authenticate Basic realm="My Realm"
3 客户端认证,含有下面的头
Authorization Basic dGVzdDp0ZXN0
“dGVzdDp0ZXN0”是"test:test"的Base64编码。 (可以通过php函数base64_encode()验证)
缺点:
密码明文传输,非常不安全。
httpclient中的实现
Java代码
查看org.apache.commons.httpclient.auth包的BasicScheme类
// Copy from the httpclient source code
// Omit some codes
public static String authenticate(UsernamePasswordCredentials credentials, String charset) {
...
StringBuffer buffer = new StringBuffer();
buffer.append(credentials.getUserName());
buffer.append(":");
buffer.append(credentials.getPassword());
return "Basic " + EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getBytes(buffer.toString(), charset)));
}
相关文章推荐
- BP神经网络
- 测试本地网络到云端网络延迟
- 网络是影响开发速度和心情的重要因素
- HTTP 状态码
- Spring Remoting: HTTP Invoker--转
- HTTPS的误解(一)
- 错误:HttpServlet was not found on the Java
- 网络编程知识(10)--CDN
- HTTPS的误解(一)
- Mozilla新特性只支持https网站,再次推动SSL证书普及
- Mozilla新特性只支持https网站,再次推动SSL证书普及
- iOS网络连接优化
- 《TCP/IP详解》学习笔记-第2章 链路层
- HTTP 常见错误代码
- 不要过分相信虚拟机,特别是网络连接方面
- Apache apr & httpd 源代码安装
- 社交网络中基于张量分解的好友推荐
- 网络编程基础知识
- 资源加载和网络栈
- macbook 网络及使用技巧