Shiro系列之Shiro+Spring MVC整合(Integration)

Shiro系列之Shiro+Spring MVC整合第一步,Shiro Filter

在web.xml文件中增加以下代码,确保Web项目中需要权限管理的URL都可以被Shiro拦截过滤。

<!– Shiro Filter –><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>/*</url-pattern></filter-mapping>

通常将这段代码中的filter-mapping放在所有filter-mapping之前,以达到shiro是第一个对web请求进行拦截过滤之目的。这里的fileter-name应该要和第二步中配置的java bean的id一致。

第二步,配置各种Java Bean

在root-context.xml文件中配置Shiro

<?xml version="1.0" encoding="UTF-8"?><beans xmlns=""xmlns:xsi=""xsi:schemaLocation=" http://www.springframework.org/schema/beans/spring-beans.xsd"><!– Root Context: defines shared resources visible to all other web components –><!– dataSource –><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://127.0.0.1:3306/etao_java" /><property name="username" value="root" /><property name="password" value="cope9020" /></bean><bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean><!– 数据库保存的密码是使用MD5算法加密的,所以这里需要配置一个密码匹配对象 –><bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.Md5CredentialsMatcher"></bean><!– 缓存管理 –><bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"></bean><!–使用Shiro自带的JdbcRealm类指定密码匹配所需要用到的加密对象 指定存储用户、角色、权限许可的数据源及相关查询语句 –><bean id="jdbcRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm"><property name="credentialsMatcher" ref="credentialsMatcher"></property><property name="permissionsLookupEnabled" value="true"></property><property name="dataSource" ref="dataSource"></property><property name="authenticationQuery"value="SELECT password FROM sec_user WHERE user_name = ?"></property><property name="userRolesQuery"value="SELECT role_name from sec_user_role left join sec_role using(role_id) left join sec_user using(user_id) WHERE user_name = ?"></property><property name="permissionsQuery"value="SELECT permission_name FROM sec_role_permission left join sec_role using(role_id) left join sec_permission using(permission_id) WHERE role_name = ?"></property></bean><!– Shiro安全管理器 –><bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="realm" ref="jdbcRealm"></property><property name="cacheManager" ref="cacheManager"></property></bean><!–Shiro主过滤器本身功能十分强大,其强大之处就在于它支持任何基于URL路径表达式的、自定义的过滤器的执行Web应用中,Shiro可控制的Web请求必须经过Shiro主过滤器的拦截,Shiro对基于Spring的Web应用提供了完美的支持 –><bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"><!– Shiro的核心安全接口,这个属性是必须的 –><property name="securityManager" ref="securityManager"></property><!– 要求登录时的链接(登录页面地址),非必须的属性,默认会自动寻找Web工程根目录下的"/login.jsp"页面 –><property name="loginUrl" value="/security/login"></property><!– 登录成功后要跳转的连接(本例中此属性用不到,因为登录成功后的处理逻辑在LoginController里硬编码) –><!– <property name="successUrl" value="/" ></property> –><!– 用户访问未对其授权的资源时,所显示的连接 –><property name="unauthorizedUrl" value="/"></property><property name="filterChainDefinitions"><value>/security/*=anon/tag=authc</value></property></bean><!–开启Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro注解的类,并在必要时进行安全逻辑验证–><!–<beanclass="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean><beanclass="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"><property name="securityManager" ref="securityManager"></property></bean>–></beans>一切伟大的行动和思想,都有一个微不足道的开始

Shiro系列之Shiro+Spring MVC整合(Integration)

相关文章:

你感兴趣的文章:

标签云: