spring Namespace和核心组件简介



spring security2.0起已经支持Namespace xml schema方式。其大体包含以下方面的内容。
    1.  Web/HTTP Security——最复杂部分。设置filter和其他应用于认证机制、保护URLS、导向登陆页面和错误页面等等的服务bean。
    2.  Business Object (Method) Security  - options for securing the service layer
     3) AuthenticationManager  - 处理来自框架其他部分的认证请求
     4) AccessDecisionManager  -为web和方法访问提供决策. 通常会注册一个默认decisionManager,但我们可以自定义一个。
    5) AuthenticationProvider s - 认证管理器,提供实现认证用户入口
    6) UserDetailsService  - 与认证管理器最相关的服务类

进行web/http 安全保护时首先要在web.xml中配置spring的拦截器
   
   
 
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
和使用spring其他模块一样,需要加入ContextLoadListener
   
   
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
 
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

spring security的配置文件中applicationContext-security.xml中引入security Namespace
   
   
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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">
...
</beans>
之后可以参阅security xml schema语法,了解具体使用Namespace中的相关特性。

需要对web/http进行保护需要使用http标签,来限制访问匹配url的角色等信息
   
   
<http>
<intercept-url pattern="/secure/**" access="ROLE_USER" requires-channel="https"/>
<intercept-url pattern="/**" access="ROLE_USER" requires-channel="any"/>
...
</http>

除此之外,如果我们需要进行方法级别的保护,需要启动相关服务,直接在利用Namespace中的如下特性。
   
   
<global-method-security secured-annotations="enabled" />
Java代码中可以使用如下注解
   
   
public interface BankService {
@Secured("IS_AUTHENTICATED_ANONYMOUSLY")
public Account readAccount(Long id);
@Secured("IS_AUTHENTICATED_ANONYMOUSLY")
public Account[] findAccounts();
@Secured("ROLE_TELLER")
public Account post(Account account, double amount);
}

我们发现无论是保护url级别,还是方法级别保护,都需要指定相关角色,而角色前面加入了ROLE_。这是早期语法,需要利用ROLE_+角色名称。security4.0后可以直接使用角色名称。

有了这些配置后应用即得知什么角色可以访问什么资源了,但是登陆用户具体是什么角色,又是怎么确认的呢。这就涉及security的以下核心组件。

SecurityContextHolder—— 使用 ThreadLocal存储当前应用凭证详情
2. SecurityContext
3.  Authentication Objects—— Authentication对象代表当前与应用交互的凭证详情
以上三者是什么的关系,可以从如下代码段中了解。
   
   
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal instanceof UserDetails) {
String username = ((UserDetails)principal).getUsername();
} else {
String username = principal.toString();
}
其中的 principal就是 Authentication Objects, 通常它仅仅是一个对象,我们常常将其转换为UserDetail, UserDetail 以一种可扩展的和具体应用相关的方式代表着 principal (凭证)。
4.  UserDetailsService—— 提供了 loadUserByUsername 方法来加载UserDetails。
前面3个组件能够帮助我们获取用户凭证,当然也可以利用 SecurityContextHolder . getContext (). getAuthentication (). getAuthorities( ) 可以帮助我们获取具体给principal赋予的权限。但用户凭证信息和这些授权来自哪里呢,这就是第4个组件 loadUserByUsername方法所要做的事情。

在了解这些概念后,我们便可以加载用户信息,根据用户角色访问具体的资源。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值