Shiro学习(七)--spring集成Shiro

第一步:引入shiro依赖

    <!-- shiro依赖 -->
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-core</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-ehcache</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-spring</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-web</artifactId>
        <version>1.3.2</version>
    </dependency>
    <!-- end -->

第二步:配置web.xml,比如Shiro的过滤器

    <!-- shiro过滤器配置  DelegatingFilterProxy会去spring容器中找到shiroFilter-->
    <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>
    <!--shiroFilter拦截所有的请求-->
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

第三步:自定义realm(自行实现)

    public class MyReaml extends AuthorizingRealm {
    
        @Override
        public String getName() {
            return "MyReaml";
        }
    
        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
            return null;
        }
    
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
            return null;
        }
    }
    

第四步:配置sping-shiro.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
        <!--    开启aop对类进行代理-->
        <aop:config proxy-target-class="true"></aop:config>
    <!--    让授权注解生效,通过这个配置,可以让shiro的注解生效,可以标注到controller的方法上-->
        <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
            <property name="securityManager" ref="securityManager"></property>
        </bean>
    

        <bean id="myReaml" class="com.onlineStudySystem.realm.UserReaml">
            <property name="credentialsMatcher" ref="credentialsMatcher"></property>
        </bean>
  <!--    shiro密码加密器-->
        <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
    <!--        加密算法,自定义,注册的时候用的什么算法加密,这里就配置成什么-->
            <property name="hashAlgorithmName" value="md5"></property>
    <!--        散列次数,自定义,注册的时候散列多少次,这里就配置多少-->
            <property name="hashIterations" value="3"></property>
        </bean>

        <!-- 配置shiro的核心securityManager -->
        <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
            <!--指定自定义reaml的引用-->
            <property name="realm" ref="myReaml"></property>
            <property name="cacheManager" ref="mycacheManager"></property>
        </bean>

        <!-- 使用ehcache做本地缓存,也可以使用redis做,具体配置自行百度 -->
        <bean id="mycacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
            <property name="cacheManager" ref="ehCacheManager"></property>
         </bean>
        <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
            <property name="configLocation" value="classpath:shiro-ehcache.xml"></property>
            <property name="shared" value="true"></property>
        </bean>
        
    <!--    定制登出过滤器-->
        <bean id="logoutFilter" class="org.apache.shiro.web.filter.authc.LogoutFilter">
            <property name="redirectUrl" value="${adminPath}/login" />
        </bean>
    </beans>
    
    <!--    注意这里的名字(id)必须和web.xml中设定shiroFilter的名字一样-->
        <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
            <property name="securityManager" ref="securityManager"></property>
            <property name="loginUrl" value="/login">
            </property>
            <!--  登录成功后跳转到/main请求对应的逻辑处理,如果之前有目标地址,这个successUrl将会被覆盖!-->
            <property name="successUrl" value="/main"></property>
            <property name="filters">
                <map>
                    <entry key="logout" value-ref="logoutFilter"></entry>
                </map>
            </property>
            <property name="unauthorizedUrl" value="/nopermission.jsp"></property>
            <property name="filterChainDefinitions">
                <value>
                    /**=authc
                    /logout=logout
                </value>
            </property>
        </bean>
    

    <!--    让出现权限异常的时候,允许我们跳转到自定义的没有权限页面 /nopermission.jsp-->
        <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
            <property name="exceptionMappings">
                <props>
    <!--                如果抛出了UnauthorizedException这个异常,就跳转到nopermission.jsp-->
                    <prop key="org.apache.shiro.authz.UnauthorizedException">redirect:/nopermission.jsp</prop>
                </props>
            </property>
        </bean>

这个配置文件对应的脑图如下:
在这里插入图片描述
第六步、在spring-mvc.xml文件中引入spring-shiro.xml的配置文件


    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--    1.配置DI注解解析器-->
        <context:annotation-config/>
    <!--    2.配置IOC注解解析器-->
        <context:component-scan base-package="com.haizhang"></context:component-scan>
    <!--    3.配置mvc注解解析器-->
        <mvc:annotation-driven></mvc:annotation-driven>
    <!--    4.配置静态资源处理器-->
        <mvc:default-servlet-handler></mvc:default-servlet-handler>
    <!--    5.配置试图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    <!--    6.引入shiro的配置文件-->
        <import resource="classpath:spring-shiro.xml"></import>
    <!--    7.引入spring配置文件-->
        <import resource="classpath:spring.xml"></import>
    </beans>

最后给出完整的 web.xml配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <display-name>Archetype Created Web Application</display-name>
        <servlet>
            <servlet-name>myspring</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-mvc.xml</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>myspring</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <filter>
            <filter-name>EncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>EncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </context-param>
    
    
        <!-- shiro过滤器配置  DelegatingFilterProxy会去spring容器中找到shiroFilter-->
        <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>
        <!--shiroFilter拦截所有的请求-->
        <filter-mapping>
            <filter-name>shiroFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    </web-app>

至此,shiro整合spring的配置就已经完成了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值