Shiro提供了与Web集成的支持,其通过一个ShiroFilter入口来拦截需要安全控制的URL,然后进行相应的控制,ShiroFilter类似于如Strut2/SpringMVC这种web框架的前端控制器,其是安全控制的入口点,其负责读取配置(如ini配置文件),然后判断URL是否需要登录/权限等工作。
而要在Spring中使用Shiro的话,可在web.xml中配置一个DelegatingFilterProxy,DelegatingFilterProxy作用是自动到Spring容器查找名字为shiroFilter(filter-name)的bean并把所有Filter的操作委托给它。
首先是在web.xml中配置DelegatingFilterProxy
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
配置好DelegatingFilterProxy后,下面只要再把ShiroFilter配置到Spring容器(此处为Spring的配置文件)即可:
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="successUrl" value="/user/list" />
<property name="unauthorizedUrl" value="/login" />
<property name="filters">
<map>
<entry key="sperm" value-ref="simplePermFilter"/>
</map>
</property>
<property name="filterChainDefinitions">
<value>
/Flat-UI-master/**=anon
/index.jsp* = anon
/test.jsp*=anon
/jsp/** = authc
/test/objT.do = sperm
</value>
</property>
</bean>
可以看到我们使用了ShiroFilterFactoryBean来创建shiroFilter,这里用到了Spring中一种特殊的Bean——FactoryBean。
1)shiroFilter 中 loginUrl 为登录页面地址,successUrl 为登录成功页面地址(如果首先访问受保护 URL 登录成功,则跳转到实际访问页面),unauthorizedUrl 认证未通过访问的页面(前面提到的“未经授权页面”)。
2)shiroFilter 中 filters 属性,formAuthenticationFilter 配置为基于表单认证的过滤器。
3)shiroFilter 中 filterChainDefinitions 属性,anon 表示匿名访问(不需要认证与授权),authc 表示需要认证,perms[SECURITY_ACCOUNT_VIEW] 表示用户需要提供值为“SECURITY_ACCOUNT_VIEW”Permission 信息。由此可见,连接地址配置为 authc 或 perms[XXX] 表示为受保护资源。
也可以定义单独的配置文件
<property name="filterChainDefinitions" ref="extensibleFilterChainDefinitions"/>
<bean id="extensibleFilterChainDefinitions" class="com.enation.app.base.security.domain.ExtensibleFilterChainDefinitions">
<property name="locations" value="classpath*:spring_cfg/*filter-chain-definitions.properties"/>
</bean>
DelegatingFilterProxy的作用:在spring-web.jar中,作为一个代理类,可以获取spring容器中的bean对象(名称由配置信息或者配置的filter的filter-name决定)
如果是一个filter,web.xml配置中,targetFilterLifecycle为true时,会调用filter的init和destroy方法,默认为false。
而要在Spring中使用Shiro的话,可在web.xml中配置一个DelegatingFilterProxy,DelegatingFilterProxy作用是自动到Spring容器查找名字为shiroFilter(filter-name)的bean并把所有Filter的操作委托给它。
首先是在web.xml中配置DelegatingFilterProxy
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
配置好DelegatingFilterProxy后,下面只要再把ShiroFilter配置到Spring容器(此处为Spring的配置文件)即可:
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="successUrl" value="/user/list" />
<property name="unauthorizedUrl" value="/login" />
<property name="filters">
<map>
<entry key="sperm" value-ref="simplePermFilter"/>
</map>
</property>
<property name="filterChainDefinitions">
<value>
/Flat-UI-master/**=anon
/index.jsp* = anon
/test.jsp*=anon
/jsp/** = authc
/test/objT.do = sperm
</value>
</property>
</bean>
可以看到我们使用了ShiroFilterFactoryBean来创建shiroFilter,这里用到了Spring中一种特殊的Bean——FactoryBean。
1)shiroFilter 中 loginUrl 为登录页面地址,successUrl 为登录成功页面地址(如果首先访问受保护 URL 登录成功,则跳转到实际访问页面),unauthorizedUrl 认证未通过访问的页面(前面提到的“未经授权页面”)。
2)shiroFilter 中 filters 属性,formAuthenticationFilter 配置为基于表单认证的过滤器。
3)shiroFilter 中 filterChainDefinitions 属性,anon 表示匿名访问(不需要认证与授权),authc 表示需要认证,perms[SECURITY_ACCOUNT_VIEW] 表示用户需要提供值为“SECURITY_ACCOUNT_VIEW”Permission 信息。由此可见,连接地址配置为 authc 或 perms[XXX] 表示为受保护资源。
也可以定义单独的配置文件
<property name="filterChainDefinitions" ref="extensibleFilterChainDefinitions"/>
<bean id="extensibleFilterChainDefinitions" class="com.enation.app.base.security.domain.ExtensibleFilterChainDefinitions">
<property name="locations" value="classpath*:spring_cfg/*filter-chain-definitions.properties"/>
</bean>
DelegatingFilterProxy的作用:在spring-web.jar中,作为一个代理类,可以获取spring容器中的bean对象(名称由配置信息或者配置的filter的filter-name决定)
如果是一个filter,web.xml配置中,targetFilterLifecycle为true时,会调用filter的init和destroy方法,默认为false。