shiro(二)——集成Spring

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Back_Light_F/article/details/81453977

1.导入spring、Springmvc与Shiro以及对日志支持的jar包

 在web.xml中配置spring、springmvc、以及shiro的相关配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>shiro-2</display-name>
 
 <context-param>
 		<param-name>contextConfigLocation</param-name>
 		<param-value>classpath:applicationContext.xml</param-value>
 </context-param>
 <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>
 
   <!-- ==================================================================
         Filters
         ================================================================== -->
    <!-- Shiro Filter is defined in the spring application context: -->
    <!-- 1.配置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>
 
 
</web-app>

创建spring的配置文件:applicationContext.xml.

创建springmvc配置文件:spring-servlet.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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


   <context:component-scan base-package="com.znx.shiro"/>
   <mvc:annotation-driven/>
   <mvc:default-servlet-handler/>
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   		<property name="prefix" value="/"></property>
   		<property name="suffix" value=".jsp"></property>	
   </bean> 
   
    
</beans>

在applicationContext.xml中配置shiro的相关信息:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
        
         <!--  
         1.配置SecurityManager      	
          -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="cacheManager" ref="cacheManager"/>
        <property name="realm" ref="jdbcRealm"/>
    </bean>

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

         <!-- 3.配置realm
         		3.1直接配置实现了realm接口的自定义bean-->
    <bean id="jdbcRealm" class="com.znx.shiro.realms.ShiroRealm"></bean>

            <!--  4. 配置LifecycleBeanPostProcessor
            可以自动的调用配置在spring IOC容器中shiro bean的生命周期方法-->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

         <!-- 5. 启用IOC容器中使用shiro的注解,但必须在配置了lifecycleBeanProcessor之后才可以使用
          -->
    <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
         	6.1:id:必须和web.xml文件中配置的DelegatingFilterProxy的name一致
         	6.2
          -->
         
    <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:必须认证(即登录)后才能访问的页面
        -->
        <property name="filterChainDefinitions">
            <value>
                /login.jsp = anon
                
                # everything else requires authentication:
                /** = authc
            </value>
        </property>
    </bean>

</beans>

创建一个类实现Realm接口,内容先不写:

package com.znx.shiro.realms;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.realm.Realm;

public class ShiroRealm implements Realm{

	public AuthenticationInfo getAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException {
		// TODO Auto-generated method stub
		return null;
	}

	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean supports(AuthenticationToken arg0) {
		// TODO Auto-generated method stub
		return false;
	}

}

创建与applicationContext.xml中响应的页面:

启动工程:

访问localhost:8080/shiro-2/login.jsp,访问成功:

扫描二维码关注公众号,回复: 3729992 查看本文章

<!--图片上传不上来是怎么回事:不正确的服务器响应-->

反正是可以访问成功的。这里就不贴图了。

访问list.jsp:因为没有权限,所以会自动跳转到login.jsp

到这里,shiro集成spring的基本工程就搭建好了

猜你喜欢

转载自blog.csdn.net/Back_Light_F/article/details/81453977