Acegi 读书笔记

最近闲来没事,看了看spring Acegi ,其中一些心得也顺便写下了。

Acegi是基于spring开发的安全框架,其中技术特点就是spring的依赖注入,AOP拦截,针对接口编程。

设计上是基于角色的权限控制系统,实现安全框架的可配置。

关于Acegi的配置,说白了就三大块。认证管理器,决策管理器,过滤器链。

访问任何受保护的资源,其过程是:认证管理器=》决策管理器=》过滤器链。

1.在web.xml里面配置相应的Listener和Filter。

先加入security.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/security.xml,
</param-value>
</context-param>

配置filter

/**其中targetClass参数指定,则在web应用程序初始化的时候,acegiFilterChain将从spring容器中查找类型为org.acegisecurity.util.FilterChainProxy的bean,然后将自身的任务委托给bean**/

<filter>
<filter-name>acegiFilterChain</filter-name>
<filter-class>org.acegisecurity.util.FilterToBeanProxy</filter-class>
<init-param>
<param-name>targetClass</param-name>
<param-value>org.acegisecurity.util.FilterChainProxy</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>acegiFilterChain</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

2.配置AuthenticationManager

AuthenticationManager绝顶用户是否有通过身份验证,其验证委托给一个或者多个AuthenticationProvider。

Acegi提供多个AuthenticationProvider来验证:AutheByAdapterProvider(通过web容器来验证用户身份),CasAuthenticationProvider(通过CAS来验证),DaoAuthenTicatinProvider(通过数据库用户名和密码来验证,最常用)JaasAuthenticationProvider(通过jaas来验证),PasswordDaoAuthenticationPrivider(通过数据库),RememberMeAuthenticationProvider(通过cookie来验证),RemoteAuthenticationProvider(通过远程服务器)。

这里配置了两个AuthenticationProvider:

<bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager">
<property name="providers">
<list>
<ref bean="daoAuthenticationProvider" />
<ref bean="rememberMeAuthenticationProvider" />
</list>
</property>
</bean>

<!-- 基于DAO验证的AuthenticationProvider -->
<bean id="daoAuthenticationProvider"
class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
<property name="userDetailsService" ref="userDetailsService" />
</bean>

<!-- userDetailsService是Acegi配置唯有需要手动配置的组件-->

<bean id="userDetailsService"
class="com.gzpost,lantou.security.JdbcUserDetailsService">
<property name="dataSource" ref="dataSource" />
</bean>

<!-- 基于RememberMe验证的AuthenticationProvider -->
<bean id="rememberMeAuthenticationProvider"
class="org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider">
<property name="key" value="RememberMeAtLiveBookstore" />
</bean>

<bean id="rememberMeServices" class="org.acegisecurity.ui.rememberme.TokenBasedRememberMeServices">
<property name="userDetailsService" ref="userDetailsService" />
<property name="parameter" value="j_remember_me" />
<property name="key" value="RememberMeAtLiveBookstore" />
</bean>

3.配置AccessDecisionManager

AccessDecisionManager决定用户是否允许访问末一受保护的资源。

它是基于投票的方式。
<bean id="accessDecisionManager"
class="org.acegisecurity.vote.AffirmativeBased">
<property name="decisionVoters">
<list>
<bean class="org.acegisecurity.vote.RoleVoter" />
</list>
</property>
<property name="allowIfAllAbstainDecisions" value="false" />
</bean>

4.配置FilterChain

FilterChain最终完成认证和授权。

Acegi已经提供了一系列非常游泳的Filter供我们使用,这里不一一列出来了。

注意,logoutFileter和AuthenticationProcessiongfilter用于实现用户的注销和登入功能,他们仅过滤特定的URL

<bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy">
<property name="filterInvocationDefinitionSource">
<value>
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/**=sessionIntegrationFilter,logoutFilter,authenticationFilter,rememberMeFilter,exceptionFilter,securityInterceptor
</value>
</property>
</bean>

<bean id="sessionIntegrationFilter"
class="org.acegisecurity.context.HttpSessionContextIntegrationFilter" />

<bean id="logoutFilter" class="org.acegisecurity.ui.logout.LogoutFilter">
<!-- URL redirected to after logout -->
<constructor-arg value="/" />
<constructor-arg>
<list>
<bean class="org.acegisecurity.ui.logout.SecurityContextLogoutHandler" />
<ref bean="rememberMeServices" />
</list>
</constructor-arg>
<property name="filterProcessesUrl" value="/logout.action" />
</bean>

<bean id="authenticationFilter"
class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
<property name="authenticationManager" ref="authenticationManager" />
<property name="authenticationFailureUrl" value="/login.jsp?login_error=Login%20failed." />
<property name="defaultTargetUrl" value="/main.jsp" />
<property name="filterProcessesUrl" value="/login.action" />
<property name="rememberMeServices" ref="rememberMeServices" />
</bean>

<bean id="rememberMeFilter" class="org.acegisecurity.ui.rememberme.RememberMeProcessingFilter">
<property name="authenticationManager" ref="authenticationManager" />
<property name="rememberMeServices" ref="rememberMeServices" />
</bean>

<!-- 处理登录异常或权限异常的Filter -->
<bean id="exceptionFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter">
<!-- 出现AuthenticationException时的登录入口 -->
<property name="authenticationEntryPoint">
<bean class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
<property name="loginFormUrl" value="/login.action" />
<property name="forceHttps" value="false" />
</bean>
</property>
<!-- 出现AccessDeniedException时的Handler -->
<property name="accessDeniedHandler">
<bean class="org.acegisecurity.ui.AccessDeniedHandlerImpl" />
</property>
</bean>

<!-- 基于URL的安全拦截器 -->
<bean id="securityInterceptor"
class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
<property name="authenticationManager" ref="authenticationManager" />
<property name="accessDecisionManager" ref="accessDecisionManager" />
<property name="objectDefinitionSource">
<value>
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/admin*=ROLE_ADMIN
/user*=ROLE_USER
</value>
</property>
</bean>

此外,针对业务逻辑组件的保护,关键是声明一个MethodSecurityInterceptor,使其有拦截组件的方法调用,然后根据用户角色来绝顶是否允许调用该方法。

<bean id="serviceSecurityInterceptor"
class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">
<property name="validateConfigAttributes" value="true" />
<property name="authenticationManager" ref="authenticationManager" />
<property name="accessDecisionManager" ref="accessDecisionManager" />
<property name="objectDefinitionSource">
<bean class="org.acegisecurity.intercept.method.MethodDefinitionAttributes">
<property name="attributes">
<bean class="org.acegisecurity.annotation.SecurityAnnotationAttributes" />
</property>
</bean>
</property>
</bean>

<!-- 利用Spring的自动代理功能实现AOP代理 -->
<bean id="autoProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<value>serviceSecurityInterceptor</value>
</list>
</property>
<property name="beanNames">
<list>
<value>businessService</value>
</list>
</property>
</bean>

再在相应的service用注释的方式实现方法级别的保护,如:@Secured({"ROLE_USER"})
CH341A编程器是一款广泛应用的通用编程设备,尤其在电子工程和嵌入式系统开发领域中,它被用来烧录各种类型的微控制器、存储器和其他IC芯片。这款编程器的最新版本为1.3,它的一个显著特点是增加了对25Q256等32M芯片的支持。 25Q256是一种串行EEPROM(电可擦可编程只读存储器)芯片,通常用于存储程序代码、配置数据或其他非易失性信息。32M在这里指的是存储容量,即该芯片可以存储32兆位(Mbit)的数据,换算成字节数就是4MB。这种大容量的存储器在许多嵌入式系统中都有应用,例如汽车电子、工业控制、消费电子设备等。 CH341A编程器的1.3版更新,意味着它可以与更多的芯片型号兼容,特别是针对32M容量的芯片进行了优化,提高了编程效率和稳定性。26系列芯片通常指的是Microchip公司的25系列SPI(串行外围接口)EEPROM产品线,这些芯片广泛应用于各种需要小体积、低功耗和非易失性存储的应用场景。 全功能版的CH341A编程器不仅支持25Q256,还支持其他大容量芯片,这意味着它具有广泛的兼容性,能够满足不同项目的需求。这包括但不限于微控制器、EPROM、EEPROM、闪存、逻辑门电路等多种类型芯片的编程。 使用CH341A编程器进行编程操作时,首先需要将设备通过USB连接到计算机,然后安装相应的驱动程序和编程软件。在本例中,压缩包中的"CH341A_1.30"很可能是编程软件的安装程序。安装后,用户可以通过软件界面选择需要编程的芯片类型,加载待烧录的固件或数据,然后执行编程操作。编程过程中需要注意的是,确保正确设置芯片的电压、时钟频率等参数,以防止损坏芯片。 CH341A编程器1.3版是面向电子爱好者和专业工程师的一款实用工具,其强大的兼容性和易用性使其在众多编程器中脱颖而出。对于需要处理25Q256等32M芯片的项目,或者26系列芯片的编程工作,CH341A编程器是理想的选择。通过持续的软件更新和升级,它保持了与现代电子技术同步,确保用户能方便地对各种芯片进行编程和调试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值