shiro-spring 权限管理

在web.xml中配置spring
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring.xml</param-value>
  </context-param>
 
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
在web.xml中配置springMvc
<servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
 
  <servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
在web.xml中配置shiro代理过滤器
<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>






spring.xml中文件配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
">


设置扫描的包
<context:component-scan base-package="com"></context:component-scan>
开启注解支持
<mvc:annotation-driven/>

配置扫描的页面属性
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/"></property>
  <property name="suffix" value=".jsp"/>
</bean>
配置连接到数据库
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://localhost:3306/shiro01?useUnicode=true&amp;characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="mmmm"></property>
</bean>

配置shiro的jdbccrealm
<bean id="jdbcrealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
   <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="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"></bean>
配置shiro的安全管理
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
   <property name="cacheManager" ref="cacheManager"></property>
   <property name="realm" ref="jdbcrealm"></property>
</bean>
配置shiro的过滤器
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
   <property name="securityManager" ref="securityManager"></property>
配置默认登录界面
   <property name="loginUrl" value="login.jsp"></property>
配置无权访问时去的界面
    <property name="unauthorizedUrl" value="noqx.jsp"></property>

配置各个权限能访问的页面
  <property name="filterChainDefinitions">
   <value>
   /index.jsp=anon
   /result.jsp=authc,roles[admin]
   </value>
       
       
   </property>
  
</bean>



配置识别注解时各个权限访问
<bean class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">

<property name="proxyTargetClass" value="true"></property>
</bean>

<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">

<property name="securityManager" ref="securityManager"></property>
</bean>
配置各个异常时访问的页面
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

<property name="exceptionMappings">

<props>
<prop key="org.apache.shiro.authz.UnauthorizedException">
             redirect:/noqx.jsp
          </prop>
</props>
</property>

</bean>
</beans>

猜你喜欢

转载自imoosen.iteye.com/blog/2361287