shiro-spring permission management

在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> <filter> Configure the shiro proxy filter in web.xml
  </servlet-mapping>


  <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
">


Set the scanned package
<context:component-scan base-package="com"></context:component-scan>
Enable annotation support
<mvc: annotation-driven/>

configures scanned page properties
<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&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 id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"></bean> Configure shiro's cache management
</bean>




Configure shiro's security management
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
   <property name="cacheManager" ref="cacheManager"></property>
   <property name=" realm" ref="jdbcrealm"></property>
</bean>
Configure Shiro's filter
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
   <property name="securityManager " ref="securityManager"></property>
Configure the default login interface
   <property name="loginUrl" value="login.jsp"></property>
Configure the interface to go to when not authorized to access
    <property name="unauthorizedUrl" value="noqx.jsp"></property>

Configure the pages that each permission can access
  <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>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326296919&siteId=291194637