SpringSecurity如何定义自己的Provider?如何限制指定的IP?
<!-- http安全配置 --><s:http auto-config="true" use-expressions="true" lowercase-comparisons="false" create-session="ifRequired" > 。。。。<s:intercept-url pattern="/pages/**" access="isAuthenticated()" /> </s:http> <bean id="loggerListener" ref="userDetailsService"></property><property name="ehrUserDAO" ref="ehrUserDAO"/></bean><!-- 项目实现的用户查询服务 --><bean id="userDetailsService" ref="securityService" /></bean>
????
1、为什么没有使用userCache?
??? 以上是springsecurity的配置,考虑到维护userCache的繁杂性,userCache 并没有进行配置。主要原因是角色资源的关联,一旦角色资源关联关系发生变化,Cache还是需要清空,重新插入。
?
? 2、?如果需要定义自己的provider?
???
<s:authentication-manager alias="authenticationManager"><s:authentication-provider ref="bobkpiAuthenticationProvider"></s:authentication-provider></s:authentication-manager>
?
?
?3、use-expressions="true"这个配置什么作用??
? 主要是用于isAuthenticated()这样的表达式解析
?
4、springsecurity的源码中HttpConfigurationBuilder中,定义了多达数配置项。
?
5、SecurityExpressionRoot中定义了很多表达式可用于配置的方法名称。
?
默认采用的是WebSecurityExpressionRoot,这个类中有hasIpAddress,可以指定某些IP进行访问。
?
/** * Takes a specific IP address or a range using the IP/Netmask (e.g. 192.168.1.0/24 or 202.24.0.0/14). * * @param ipAddress the address or range of addresses from which the request must come. * @return true if the IP address of the current request is in the required range. */ public boolean hasIpAddress(String ipAddress) { return (new IpAddressMatcher(ipAddress).matches(request)); }?
public final boolean isAuthenticated() { return !isAnonymous(); }? ?指的是必须为非匿名用户。?
?
?? <s:intercept-url pattern="/pages/**" access="hasIpAddress('10.168.166.125')" />
?
? access表达式必须只有一个
?
?关于springsecurity的配置,很多人仍然觉得比较复杂,我是从acegi开始学起的,虽然acegi的配置更复杂,但是对于大多数的关于配置的代码,我看了一遍,对于所有配置还是比较熟悉的,而且很容易进行扩展,新的配置有了新的schema,但是如果和acegi以前的学习成果结合起来学习,印象还是比较深刻的,毕竟springsecurity只是acegi的升级,很多类只是换了个名字。建议将acegi的配置对照起来,原来怎么配置,现在怎么配置,学起来比较快了。
?
? 最好看下schema DTD的说明,这样方便你自己的扩展。