1, 添加 HTTP/HTTPS 信道安全

<http>
     <intercept-url  pattern="/secure/**"  access="ROLE_USER" requires-channel="https"/>
     <intercept-url  pattern="/**"  access="ROLE_USER" requires-channel="any"/>
</http>

  使用了这个配置以后,如果用户通过 HTTP 尝试访问"/secure/**"匹配的网址,他们会先被重定向到 HTTPS 网址下。  可用的选项有"http",  "https"  或  "any"。  使用"any"意味着使用 HTTP 或 HTTPS 都可以。

  如果你的程序使用的不是 HTTP 或 HTTPS 的标准端口,你可以用下面的方式指定端口

<http>
...
  <port-mappings>
    <port-mapping http="9080" https="9443"/>
  </port-mappings>
</http>

2,会话管理
  1)检测超时  

  你可以配置 Spring Security 检测失效的 session ID, 并把用户转发到对应的 URL。这可以通过  session-management 元素配置:

<http> ... 

  <session-management invalid-session-url="/sessionTimeout.htm" />

</http>

  2)同步会话控制

  如果你希望限制单个用户只能登录到你的程序一次, Spring Security 通过添加下面简单的部分支持这个功能。  首先,你需要把下面的监听器添加到你的 web.xml 文件里,让 Spring  Security 获得 session 生存周期事件:

<listener>    <listener-class>
    org.springframework.security.web.session.HttpSessionEventPublisher
  </listener-class></listener>

   然后,在你的 application context 加入如下部分:

<http>
...
<session-management>
    <concurrency-control max-sessions="1" />
</session-management>
</http>

  这将防止一个用户重复登录好几次-第二次登录会让第一次登录失效。  通常我们更想防止第二次登录,这时候我们可以使用

<http>
...
<session-management>
  <concurrency-control  max-sessions="1" error-if-maximum-exceeded="true" />
</session-management>
</http>

  如果你希望使用一个错误页面替代,你可以在 session-management  中添加  session-authentication-error-url 属性。

  3)防止 Session 固定攻击

  Spring  Security 通过在用户登录时,创建一个新session 来防止这个问题。

  如果你不需要保护,或者它与其他一些需求冲突,你可以通过使用<http>中的 session-fixation-protection 属性来配置它的行为,它有三个选项

   migrateSession  -  创建一个新 session,把原来 session 中所有属性复制到新 session 中。这是默认值。
   none -  什么也不做,继续使用原来的 session。
  newSession -  创建一个新的“干净的”session,不会复制 session 中的数据。

3,对 OpenID 的支持

  命名空间支持 OpenID 登录,替代普通的表单登录,或作为一种附加功能,只需要进行简单的修改:

<http>
  <intercept-url pattern="/**" access="ROLE_USER" />
  <openid-login />
</http>

  你应该注册一个 OpenID 供应器(比如 myopenid.com),然后把用户信息添加到你的内存<user-service>中:

<user  name="http://jimi.hendrix.myopenid.com/" authorities="ROLE_USER" />

  1) 属性交换

  支持 OpenID 的  属性交换。  作为一个例子,下面的配置会尝试从 OpenID 提供器中获得 email 和全名,  这些会被应用程序使用到:

<openid-login>
  <attribute-exchange>
  <openid-attribute  name="email" type="http://axschema.org/contact/email" required="true" />
  <openid-attribute  name="name"  type="http://axschema.org/namePerson" /> </attribute-exchange></openid-login>

  每个 OpenID 的“type”属性是一个 URI,这是由特定的 schema 决定的,  在这个例子中是 http://axschema.org/。  如果一个属性必须为了成功认证而获取,可以设置required。  确切的 schema 和对属性的支持会依赖于你使用的 OpenID 提供器。属性值作为认证过程的一部分返回,  可以使用下面的代码在后面的过程中获得:

OpenIDAuthenticationToken  token  =  (OpenIDAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
List<OpenIDAttribute> attributes = token.getAttributes();

  2) 添加你自己的 filter

   标准过滤器假名和顺序

    

    

    

    

  你可以把你自己的过滤器添加到队列中,使用 custom-filter 元素,使用这些名字中的一个,来指定你的过滤器应该出现的位置:

<http>
  <custom-filter  position="FORM_LOGIN_FILTER"  ref="myFilter" />
</http>
<beans:bean  id="myFilter" class="com.mycompany.MySpecialAuthenticationFilter"/>

  你还可以使用 after  或  before 属性,如果你想把你的过滤器添加到队列中另一个过滤器的前面或后面。  可以分别在 position 属性使用"FIRST"  或  "LAST"来指定你想让你的过滤器出现在队列元素的前面或后面。

Spring Security研究(2)-高级web特性的更多相关文章

  1. SPRING SECURITY JAVA配置:Web Security

    在前一篇,我已经介绍了Spring Security Java配置,也概括的介绍了一下这个项目方方面面.在这篇文章中,我们来看一看一个简单的基于web security配置的例子.之后我们再来作更多的 ...

  2. Spring Security研究(1)

      1, 获取Spring Security的Jar包 :从Spring网站下载页下载或者从Maven中央仓库下载.一个好办法是参考实例应用中包含的依赖库. 2,项目模块: Core - spring ...

  3. 使用 Spring Security 保护 Web 应用的安全

    安全一直是 Web 应用开发中非常重要的一个方面.从安全的角度来说,需要考虑用户认证和授权两个方面.为 Web 应用增加安全方面的能力并非一件简单的事情,需要考虑不同的认证和授权机制.Spring S ...

  4. Spring Security 4 新增特性

    1.概述 a) 特性 以下是Spring Security 4.0的新特性 Web Socket 支持 测试支持 整合Spring Data CSRF令牌参数解析 更安全的默认设置 role权限不再必 ...

  5. web应用安全框架选型:Spring Security与Apache Shiro

    一. SpringSecurity 框架简介 官网:https://projects.spring.io/spring-security/ 源代码: https://github.com/spring ...

  6. Spring Security Web应用入门环境搭建

    在使用Spring Security配置Web应用之前,首先要准备一个基于Maven的Spring框架创建的Web应用(Spring MVC不是必须的),本文的内容都是基于这个前提下的. pom.xm ...

  7. Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】

    源码请移步至:https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc 版 ...

  8. SpringMVC 3.1集成Spring Security 3.1

    这篇算是一个入门文章,昨天看见有网友提问,spring mvc集成spring security 的时候出错,揣测了一下问题木有解决.我就帮忙给搭建了一个集成框架他说可以,他告诉我这样的文章网上少.今 ...

  9. Spring Security(09)——Filter

    目录 1.1     Filter顺序 1.2     添加Filter到FilterChain 1.3     DelegatingFilterProxy 1.4     FilterChainPr ...

随机推荐

  1. mysql为什么范围查询(>,<,between,%like,like%)之后的索引无效

    因为使用了范围索引,所以会使用满足范围的所有的值,也就是说存储引擎在这个时候会提取出满足之后条件的所有值,并遍历获取满足之后条件的值. http://www.itpub.net/thread-1901 ...

  2. Entity Framework : The model backing the '' context has changed since the database was created

    1.采用code first 做项目时,数据库已经生成,后期修改数据库表结构.再次运行时出现一下问题: Entity Framework : The model backing the '' cont ...

  3. Python读写文件的路径,关于os.chdir(path)位置对程序的影响,

    关于os.chdir(path)位置对程序的影响,import os import time#直接把path放到open()里面 def fu0(): star = time.time() for i ...

  4. registry key 'Java Runtime Environment' has value'1.8',but '1.7' is requaired(转)

    当更新jdk后,运行java命令可能会提示类似这样registry key 'Java Runtime Environment' has value'1.8',but '1.7' is requair ...

  5. 【Linux】系统版本信息

    查看操作系统版本信息 dream361@master:~$ cat /etc/issue Ubuntu 16.04.2 LTS \n \l dream361@master:~$ lsb_release ...

  6. Win7系统如何复制CMD命令提示符框中的内容

    Win7系统如何复制CMD命令提示符框中的内容.. Win7系统如何复制CMD命令提示符框中的内容右键命令提示符窗口的标题栏,选择属性. 选择“编辑选项”里的“快速编辑模式”,并确定: 鼠标左键按下选 ...

  7. Linux常见命令(五)——rmdir

      前  言 JRedu 今天我们来介绍第五个命令:rmdir . 命令英文原意:remove empty directories 命令用途: rmdir:删除空目录,非空的目录不能删除 本章内容将详 ...

  8. 修改Tomcat服务器默认端口

  9. 标准的Java编码规范手册

    编码规范体现出一个开发者的基本素质,良好的编码规范可以提高团队编码的效率,避免很多不必要的问题.今天分享一个标准的Java编码规范给大家,希望对于大家今后的开发工作带来帮助. 编码规范的意义      ...

  10. Android事件传递机制详解及最新源码分析——ViewGroup篇

    版权声明:本文出自汪磊的博客,转载请务必注明出处. 在上一篇<Android事件传递机制详解及最新源码分析--View篇>中,详细讲解了View事件的传递机制,没掌握或者掌握不扎实的小伙伴 ...