梦相随1006

版权归 梦相随1006 所有,未经 https://www.cnblogs.com/xin1006 作者许可,严禁转载

导航

Spring Security研究(2)-高级web特性

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"来指定你想让你的过滤器出现在队列元素的前面或后面。

 

posted on 2013-12-02 16:30  梦相随1006  阅读(536)  评论(0编辑  收藏  举报