打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用 Spring Security 构建一个 HTTP 基本认证示例

HTTP基础认证(BA)是一种简单的认证机制。当一个web客户端需要保护任何web资源的时候,服务器会发送一个带有401状态码(未授权)的HTTP回应,还有类似WWW-Authenticate: Basic realm="realm here"WWW-Authenticate HTTP头。而浏览器这时候就会弹出一个登录对话框,提示输入用户名和密码。

这个示例展示了如何使用Spring Security框架配置HTTP基础认证。

在这篇文章中使用到的工具和技术有:

  1. Spring Framework 3.1.4
  2. Spring Security 3.1.4
  3. Spring Tool Suite 3.2
  4. JDK 1.6
  5. Tomcat 7

我们将修改我们前面发布的Spring Security 3 Hello World 示例,为其加入配置HTTP 基础认证的内容。

注意:HTTP基础认证并不是一个安全的用户认证方法,要是Web客户端和服务器之间的链接不安全的话。在传输的过程中,用户的认证凭据是用BASE64编码的,但没有采取加密或者散列化措施。因此如果凭据存在被截获的可能,那么绕过HTTPS的基础认证也能够被使用。

1. 修改Spring Security 配置

Spring Security xml配置文件中,仅仅添加<http-basic/> 就可配置HTTP基本认证。

File : WEB-INF/spring-security.xml
01<beans:beans xmlns="http://www.springframework.org/schema/security"
02    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
03    xsi:schemaLocation="http://www.springframework.org/schema/beans
04          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
05          http://www.springframework.org/schema/security
06          http://www.springframework.org/schema/security/spring-security-3.1.xsd">
07   
08    <http>
09        <intercept-url pattern="/secured/*" access="ROLE_USER" />
10           
11        <!-- Adds Support for basic authentication -->
12        <http-basic/>
13    </http>
14   
15    <authentication-manager>
16        <authentication-provider>
17            <user-service>
18                <user name="srccodes" password="password" authorities="ROLE_USER" />
19            </user-service>
20        </authentication-provider>
21    </authentication-manager>
22   
23</beans:beans>

2. 总体工程结构

3. 演示

启动服务端并部署web应用。尝试打开页面http://:/spring-security-http-basic-authentication/secured/mypage.

HTTP Response Header sent by the server


1HTTP/1.1 401 Unauthorized
2Server: Apache-Coyote/1.1
3WWW-Authenticate: Basic realm="Spring Security Application"
4Content-Type: text/html;charset=utf-8
5Content-Length: 981
6Date: Mon, 09 Sep 2013 10:47:14 GMT
浏览器会打开认证对话框提示输入用户名和密码。


对于错误的凭证,下面的认证失败的消息也会显示出来。

输入正确的用户名和密码,你可以访问受保护的页面。

发送给服务器的HTTP 请求头部
01GET /spring-security-http-basic-authentication/secured/mypage HTTP/1.1
02Host: localhost:8080
03User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0
04Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
05Accept-Language: en-US,en;q=0.5
06Accept-Encoding: gzip, deflate
07DNT: 1
08Connection: keep-alive
09Cookie: JSESSIONID=896331E26095C95449516FCBF2E0E93C; __atuvc=28%7C31%2C0%7C32%2C0%7C33%2C215%7C34%2C59%7C35
10Authorization: Basic c3JjY29kZXM6cGFzc3dvcmQ=
注意: 'c3JjY29kZXM6cGFzc3dvcmQ=' 是 'username:password'的BASE64的编码的版本。即 'srccodes:password'.
注意: 基本认证不提供登出功能。关闭浏览器就是登出。

下载源代码

spring-security-http-basic-authentication: GitHub or zip
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
spring security 3.1配置过程从简单到复杂详细配置
Spring Security笔记:使用数据库进行用户认证(form login using database)
JavaRoots: How to Secure Rest Services in Mule 3
CAS 与 Spring Security 3.1整合配置详解
activiti入门六(集成新版Activiti Modeler与Rest服务)
Unable to locate Spring NamespaceHandler for XML schema namespace
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服