2.Shiro基本项目搭建

在web环境下使用Shiro,大部分情况下使用Shiro集成Spring

  1. 加入需要的jar包
  2. 配置spring和springmvc

1.集成spring

  • 配置web.xml文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>

</context-param>

<!-- Spring提供ServletContentListener的一个实现类ContextLoaderListener监听器,该类可以作为Listener使用,在启动Tomcat容器的时候,该类的作用就是自动装载ApplicationContext的配置信息,如果没有设置contextConfigLocation的初始参数则会使用默认参数WEB-INF路径下的application.xml文件。 -->

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>

</servlet-mapping>


2./WEB-INF/spring-servlet.xml

<context:component-scan base-package="com.zgh.shiro"></context:component-scan>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>

</bean>

①. 两种标签都没有的时候,框架默认注册的有AnnotationMethodHandlerAdapter这个bean,所以能够处理@RequestMapping这个注解

②. 但是只配置了<mvc:default-servlet-handler/>时所注册的两个bean都不能处理@RequestMapping注解,因此无法找到相应的Controller,进而无法进行访问路径的映射,

③. 两种标签都有的时候,<mvc:annotation-driven/>会注册一个RequestMappingHandlerAdapter的bean,这个bean能够处理@RequestMapping这个注解。

<mvc:annotation-driven></mvc:annotation-driven>

<mvc:default-servlet-handler />


3.创建User.jsp

<body>

<h4>User Page</h4>

</body>

运行项目,访问User.jsp,不出现错误,Spring与SpringMVC搭建成功。


4.加入Shiro

  • 加入Shiro的jar包
  • 在web.xml中加入Shiro配置,DelegatingFilterProxy 实际上是 Filter 的一个代理对象. 默认情况下, Spring 会到 IOC 容器中查找和<filter-name> 对应的 filter bean. 也可以通过 targetBeanName 的初始化参数来配置 filter bean 的 id.

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

  • 配置application.xml

        <!-- 1. 配置 SecurityManager! -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="cacheManager" />

</bean>

<!-- 2. 配置 CacheManager. 2.1 需要加入 ehcache 的 jar 包及配置文件. -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml" />

</bean>

<!-- 3. 配置 Realm 3.1 直接配置实现了 org.apache.shiro.realm.Realm 接口的 bean -->
<bean id="jdbcRealm" class="com.zgh.shiro.realms.ShiroRealm">
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="MD5"></property>
<property name="hashIterations" value="1024"></property>
</bean>
</property>

</bean>

       <!-- 4. 配置 LifecycleBeanPostProcessor. 可以自定的来调用配置在 Spring IOC 容器中 shiro 
bean 的生命周期方法. -->

<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

<!-- 5. 启用 IOC 容器中使用 shiro 的注解. 但必须在配置了 LifecycleBeanPostProcessor 之后才可以使用. -->
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor" />
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />

</bean>

<!-- 6. 配置 ShiroFilter. 

               6.1 id 必须和 web.xml 文件中配置的 DelegatingFilterProxy的 <filter-name> 一致. 若不一致, 则会抛出: NoSuchBeanDefinitionException. 因为 Shiro 会来 IOC 容器中查找和 <filter-name> 名字对应的 filter bean. -->

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login.jsp" />//登录成功首页
<property name="successUrl" value="/list.jsp" />//登录成功后显示页面
<property name="unauthorizedUrl" value="/unauthorized.jsp" />//没有权限的页面

<!-- 配置哪些页面需要受保护. 以及访问这些页面需要的权限. 

                   1). anon 可以被匿名访问 

                   2). authc 必须认证(即登录)后才可能访问的页面. 

   3). logout 登出. 

                   4). roles 角色过滤器 -->

 <property name="filterChainDefinitions"> 

                              <value>

                                /login.jsp = anon 

                               #everything else requires authentication: 

                               /** = authc </value> //必须认证,即登录后可以访问

</property> 

</bean>

在原有的User.jsp基础上,分别创建list.jsp,login.jsp。完成以上代码,可实现基本的shiro功能,除login.jsp页面可匿名访问,其余都需要经过认证,且访问失败跳转回login.jsp

猜你喜欢

转载自blog.csdn.net/weixin_36439837/article/details/80287679