`
Wind_ZhongGang
  • 浏览: 260376 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring Security 之 Basic Authentication

 
阅读更多

       Spring Security实现Basic Authentication

 

       配置Security文件

   

      

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security.xsd">

    <security:http auto-config="true">
        <security:http-basic entry-point-ref="basicAuthenticationEntryPoint"/>

        <security:logout invalidate-session="true" logout-url="/logout" logout-success-url="/welcome.jsp"/>

        <security:intercept-url pattern="/welcome.jsp" filters="none"/>
        <security:intercept-url pattern="/*" access="ROLE_ADMIN"/>
    </security:http>

    <bean id="basicAuthenticationEntryPoint"
          class="org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint">
        <property name="realmName" value="Voter"/>
    </bean>

    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="ZhongGang" authorities="ROLE_ADMIN" password="123"/>
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>
</beans>

   

    这里为了简便,专注于Basic Authentication,这里使用的用户是通过配置文件配置的方式, 用户最终是放置在内存中的。

 

     这里有一点需要特别注意,通过Basic Authentication登录的方式,如果用户想要退出, 要么关闭浏览器,要么就需要在退出时向客户端发送一个HttpServletResponse.SC_UNAUTHORIZED 401错误才可以实现用户的退出功能。这点可以在BasicAuthenticationEntryPoint的源码注释中看到,原文如下:

 

    

/**
 * Used by the <code>ExceptionTraslationFilter</code> to commence authentication via the {@link BasicAuthenticationFilter}.
 * <p>
 * Once a user agent is authenticated using BASIC authentication, logout requires that
 * the browser be closed or an unauthorized (401) header be sent. The simplest way of achieving the latter is to call
 * the {@link #commence(HttpServletRequest, HttpServletResponse, AuthenticationException)} method below. This will indicate to
 * the browser its credentials are no longer authorized, causing it to prompt the user to login again.
 *
 * @author Ben Alex
 */

  

    与Basic Authentication主要相关的两个类是BasicAuthenticationFilter 和 BasicAuthenticationEntryPoint, BasicAuthenticationEntryPoint负责当用户访问一个需要授权的链接时,如果当前没有登录, 向用户展示BasicAuthenticationForm认证表单, BasicAuthenticationFilter负责处理用户的认证请求。

 

    当用户登录成功后, 访问任意链接地址时, 都可以发现在请求头中包括Authorization这个属性, 里面的加密字符串就是用户名:密码的加密后的字符串,加密方式是Base64。

  • 大小: 8 KB
  • 大小: 22.5 KB
分享到:
评论

相关推荐

    SpringSecurity项目

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

    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.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 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 ...

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

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

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

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

    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 ...

    《activmq in action 》

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

Global site tag (gtag.js) - Google Analytics