Arch-03-24- Spring Security 应用

Spring Security 用了几次,还没有在集群环境中使用的,这次需要在集群中使用了。

(一)认证的基本术语

  • User - 用户
  • Fecerated User - 联合用户
  • External User - 外部用户
  • Authentication - 验证
  • Credentials - 凭据
  • Application Security Layer - 应用安全层,过渡器检查每个 http 请求。
  • Application Layer - 应用层,每个 http 请求访问应用层都必须通过应用安全层。

(二)URI mappings

  • /admin/** - 管理控制台
  • /rpc/xmlrpc - 基于 XML 的 XML-RPC 
  • /rpc/rest - RESTful HTTP services
  • /rpc/soap - SOAP HTTP services
  • /** - 全部需要验证
(三)Filter Chains
  • Session 集成 - 访客进入或者 Http 请求访问安全上下文 -  httpSessionContextIntegrationFilter
  • 验证 - Spring Security 默认实现 FormAuthenticationProcessingFilter -  formAuthenticationFilter
  • Cookie 验证 - 处理“记住我”cookies,长效 Http cookies 用于验证 session 用户 - rememberMeProcessingFilter
  • Feed 基本认证 - Rss/Atom feeds 阅读器验证,通常是专门的阅读器,不是基于浏览器 - feedBasicAuthenticationFilter
  • 异常事务 - 各种安全相关的异常重定向,通常在 struts 层处理 -exceptionTranslationFilter
  • 事务验证 - 在应用安全屋和代码间强制验证 - pAuthenticationTranslationFilter 
(四)验证约定
  • pAuthenticationTranslationFilter 会检查 SecurityContext 的有效性
  • SecurityContextHolder.getContext().setAuthentication(new UserAuthentication(new UserTemplate()))
  • DWR 和 webservice 的认证检查使用代理层,DWR 使用主 web 应用的 session 验证,REST 使用基础 HTTP 认证,通常每个 web services 请求都会进行验证
(五)验证
  • 权限 - 管理控制台提供页面用于分配用户和用户组权限
  • 分组 - 合并用户的系统权限。权限可以分配给组和组成员,重复的权限会被覆盖
  • 代理 - 代理应用层对象是透明的,不影响安全层代码
(六)实现类
  • PermissionsManager
  • GroupManager
  • Security 子类 PAuthentication
(七)Webservice 安全入门
  1. 客户端登录连接 web service 使用 username token
  2. WSSE 规范,用于 SOAP 用户认证,也能扩展到 HTTP Service。如许多 Atom 和 RESTful 服务使用简单的验证还不够,这些简单的验证包括 HTTP 基础验证,HTTP SSL,HTTP Digest, Hash-Based 认证。
  3. 工作方式,每一个 SOAP 请求都包含安全报头,报头包含 username 和 password,符合 HTTP 认证规范。
  4. Username Token Profile Specification 的 PasswordText 选项是文本方式传输,须使用 SSL 加密。 PasswordDigest 使用 WSS4j interceptor 实现。 PasswordDigest 工作方式:
  • 客户端由两段信息:用户名和密码
  • 客户端创建一个随机数,加密随机字串
  • 客户端根据当前时间创建时间戳
  • PasswordDigest = Base64 \ (SHA1 (随机数 + 时间戳 + 密码)
  • 结合随机数和时间戳是为了避免重用的报头
  • 客户端给每个请求设置用户名和摘要
(八)Spring security 怎样进行盐值加密
     1. 以前的md5原理是
密码密文=md5算法(密码明文);
这样明文与密文其实还是一一对应的
那么人家就可以用字典攻击(就是一个一个的试)来探测密码
加盐(盐值加密)的算法很多
     
     2. Spring security用的是:
密码密文=md5算法(密码明文{盐值});
这个盐值就可以自己随便设置了,弄一个静态字符串或者用用户的登录名
举个例子:
用户名:thr
密码:fou
用用户名作为盐值
打开网页:http://www.md5.org.cn/md5/Encrypt.asp
输入:thr{fou}
得到密文:5dbae131e3eea6ce50068aab9292c8c3

(九)附件是最简单配置可运行的 war 包。
<?xml version="1.0" encoding="UTF-8"?>
<b:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:b="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

	<!--无需权限-->
	<http pattern="/css/**" security="none"/>
	<http pattern="/images/**" security="none"/>

	<http auto-config='true' access-denied-page="/403.jsp">
		<!--匿名用户-->
		<intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY" />
		<!--需要权限-->
		<intercept-url pattern="/user/**" access="ROLE_USER" />
		<intercept-url pattern="/eman/**" access="ROLE_EMAN" />
		<intercept-url pattern="/admin/**" access="ROLE_ADMIN" />
		<intercept-url pattern="/**" access="ROLE_AUTH" />
		<!--登录界面-->
    <form-login login-page='/login.jsp' />
	</http>

	<authentication-manager>
		<!--假设用户-->
		<authentication-provider>
			<user-service>
				<user name="user1" password="111111" authorities="ROLE_AUTH,ROLE_USER" />
				<user name="eman1" password="111111" authorities="ROLE_AUTH,ROLE_EMAN" />
				<user name="admin" password="111111" authorities="ROLE_AUTH,ROLE_ADMIN" />
			</user-service>
		</authentication-provider>
	</authentication-manager>

</b:beans>
 

猜你喜欢

转载自cnmqw.iteye.com/blog/1243596