`

Spring Security Basic Authentication

 
阅读更多

原文地址:http://www.javaarch.net/jiagoushi/696.htm

 

Spring Security Basic Authentication

spring security 配置

	<?xml version="1.0" encoding="UTF-8"?>
	<beans:beans xmlns="http://www.springframework.org/schema/security"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:beans="http://www.springframework.org/schema/beans"
		xsi:schemaLocation="
			http://www.springframework.org/schema/security 
			http://www.springframework.org/schema/security/spring-security-3.1.xsd
			http://www.springframework.org/schema/beans 
			http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
	 
		<http use-expressions="true">
			<intercept-url pattern="/**" access="isAuthenticated()" />
	 
			<http-basic />
		</http>
	 
		<authentication-manager>
			<authentication-provider>
				<user-service>
					<user name="user1" password="user1Pass" authorities="ROLE_USER" />
				</user-service>
			</authentication-provider>
		</authentication-manager>
	 
	</beans:beans>
	
 <http-basic>打开basic验证,
 
 如果我们访问:curl -i http://localhost:8080/spring-security-basic-auth/homepage.html
 
 会返回401
 
	HTTP/1.1 401 Unauthorized
	Server: Apache-Coyote/1.1
	Set-Cookie: JSESSIONID=E5A8D3C16B65A0A007CFAACAEEE6916B; Path=/spring-security-basic-auth/; HttpOnly
	WWW-Authenticate: Basic realm="Spring Security Application"
	Content-Type: text/html;charset=utf-8
	Content-Length: 1061
	Date: Wed, 29 May 2013 15:14:08 GMT
	
如果我们使用下面的url访问:curl -i --user user1:user1Pass http://localhost:8080/spring-security-basic-auth/homepage.html

则返回200

	HTTP/1.1 200 OK
	Server: Apache-Coyote/1.1
	Set-Cookie: JSESSIONID=301225C7AE7C74B0892887389996785D; Path=/spring-security-basic-auth/; HttpOnly
	Content-Type: text/html;charset=ISO-8859-1
	Content-Language: en-US
	Content-Length: 90
	Date: Wed, 29 May 2013 15:19:38 GMT
	
我们也可以使用spring提供的扩展接口来实现验证结果自定义

	<http-basic entry-point-ref="myBasicAuthenticationEntryPoint" />
	
	@Component
	public class MyBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {
	 
		@Override
		public void commence
		  (HttpServletRequest request, HttpServletResponse response, AuthenticationException authEx) 
		  throws IOException, ServletException {
			response.addHeader("WWW-Authenticate", "Basic realm=\"" + getRealmName() + "\"");
			response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
			PrintWriter writer = response.getWriter();
			writer.println("HTTP Status 401 - " + authEx.getMessage());
		}
	 
		@Override
		public void afterPropertiesSet() throws Exception {
			setRealmName("Baeldung");
			super.afterPropertiesSet();
		}
	}



  • 大小: 96.9 KB
分享到:
评论

相关推荐

    SpringSecurity项目

    springsecurity是一个功能强大且高度可定制的身份验证和访问控制框架。springsecurity是一个...最核心的就是 Basic Authentication Filter 用来认证用户的身份,一个在spring security中一种过滤器处理一种认证方式。

    spring security3.2.0

    Spring Security 的前身是 Acegi Security ,是 Spring 项目组中用来提供安全认证服务的框架。 Spring Security 为基于J2EE企业应用软件提供了全面安全服务。特别是使用领先的J2EE解决方案-Spring框架开发的企业...

    spring security 参考手册中文版

    Spring Security 参考 1 第一部分前言 15 1.入门 16 2.介绍 17 2.1什么是Spring Security? 17 2.2历史 19 2.3版本编号 20 2.4获得Spring安全 21 2.4.1使用Maven 21 Maven仓库 21 Spring框架 22 2.4.2 Gradle 23 ...

    Spring Security Third Edition.pdf英文版

    This book is intended for Java Web and/or RESTful webservice developers and assumes a basic understanding of creating Java 8, Java Web and/or RESTful webservice applications, XML, and the Spring ...

    SpringSecurity 3.0.1.RELEASE.CHM

    1.1. Spring Security是什么? 1.2. 历史 1.3. 发行版本号 1.4. 获得Spring Security 1.4.1. 项目模块 1.4.1.1. Core - spring-security-core.jar 1.4.1.2. Web - spring-security-web.jar 1.4.1.3. Config -...

    Spring Security-3.0.1中文官方文档(翻译版)

    Spring Security-3.0.1 中文官方文档(翻译版) 这次发布的Spring Security-3.0.1 是一个bug fix 版,主要是对3.0 中存在的一些问题进 行修 正。文档中没有添加新功能的介绍,但是将之前拼写错误的一些类名进行...

    Spring Security 中文教程.pdf

    1.1. Spring Security是什么? 1.2. 历史 1.3. 发行版本号 1.4. 获得Spring Security 1.4.1. 项目模块 1.4.1.1. Core - spring-security-core.jar 1.4.1.2. Web - spring-security-web.jar 1.4.1.3. ...

    Spring Security3.pdf

    pring security 的核心功能主要包括: 认证 (你是谁) 授权 (你能干什么) ...最核心的就是 Basic Authentication Filter 用来认证用户的身份,一个在spring security中一种过滤器处理一种认证方式

    cas-security-spring-boot-starter:用于Apereo CAS客户端的Spring Boot Starter与Spring Security完全集成

    如果security.basic.enabled=true ,则与Basic身份验证集成,可让您使用标题Authorization: Basic ...以及CAS进行身份验证 RestTemplate集成 建立 将Spring Boot Starter添加到您的项目 &lt; groupId&gt;...

    spring-boot-basic-auth:使用基本身份验证的安全Spring Boot REST API

    弹簧启动基本认证 使用基本身份验证来保护Spring Boot REST API

    dropwizard-spring-security-sample:带有 Spring Security 示例的 Dropwizard

    jar 文件,使应用程序完全自包含注意:示例配置为使用Basic Authentication ,Spring Security 支持的其他认证方案可以在spring-context.xml指定构建并运行应用程序打包应用程序运行: mvn packag

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    9.4.2. Enabling HTTP Basic authentication 9.4.3. Enabling remember-me functionality 9.4.4. Logging out 9.5. Securing the view 9.5.1. Using Spring Security’s JSP tag library 9.5.2. Working with ...

    springboot-oauth2:Spring Boot Oauth2.0

    Spring Boot Oauth2.0 测试 1.运行工程 2.无token时,访问接口: POST 接口响应: { "error": "unauthorized", "error_description": "Full authentication is required to access this resource" } 3.获取token ...

    restful restful所需要的jar包

    * Supports HTTP Basic and Digest authentication (client and server side) * Supports Amazon S3 authentication (client side) * Supports OAuth authentication (server side) * Supports HTTPS ...

    cas:Apereo CAS-适用于所有人及以后的企业单一登录

    中央认证服务(CAS)介绍欢迎来到(通常称为CAS)的主页。 CAS是用于Web的企业多语言单点登录解决方案,它试图成为满足身份验证和授权需求的综合平台。 CAS是一个开放且...Trusted,BASIC,Apache Shiro,MongoD

    肯定性:一个简单高效的开源安全框架,专注于保护静态API

    Spring安全 多框架支持 支持 支持需要修改 不支持 宁静的api 支持 支持需要修改 支持 路径匹配 字典匹配树 蚂蚁比赛 蚂蚁比赛 注释支持 支持 支持 支持 小服务程序 支持 支持 支持 杰克斯 支持

    《activmq in action 》

    6.1. Introducing Basic Security Concepts ...................................... 158 6.2. Authentication ......................................................................... 159 6.2.1. Configuring...

    asp.net知识库

    ASP.NET 2.0 Security FAQs Asp.net 2.0功能体验,细节之Web控件(一) 隐藏控件 Asp.net 2.0功能体验,总体设计思想 Asp.net 2.0 WebPart使用经验点滴 革新:.NET 2.0的自定义配置文件体系初探 关于如何在ASP.NET ...

Global site tag (gtag.js) - Google Analytics