SSM整合shiro框架相关配置文件
生活随笔
收集整理的這篇文章主要介紹了
SSM整合shiro框架相关配置文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.創建相關表
2.導入maven依賴
? ?<properties><java.version>1.8</java.version><spring.version>5.0.7.RELEASE</spring.version><aspectj.version>1.8.13</aspectj.version><cglib.version>3.2.6</cglib.version><mybatis.version>3.4.6</mybatis.version><mybatis-spring.version>1.3.2</mybatis-spring.version><mysql.version>5.1.46</mysql.version><c3p0.version>0.9.5.2</c3p0.version><jackson.version>2.9.3</jackson.version><slf4j.version>1.7.25</slf4j.version><shiro.version>1.3.0</shiro.version></properties><dependencies><!-- 緩存依賴 --><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-ehcache</artifactId><version>${shiro.version}</version></dependency> ?<!-- shiro jar包依賴 --><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><version>${shiro.version}</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-web</artifactId><version>${shiro.version}</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>${shiro.version}</version></dependency> ?<!-- spring 相關開始 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><!-- aop 相關 --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>${aspectj.version}</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>${aspectj.version}</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjtools</artifactId><version>${aspectj.version}</version></dependency><dependency><groupId>cglib</groupId><artifactId>cglib-nodep</artifactId><version>${cglib.version}</version></dependency><!-- jackson 相關 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>${jackson.version}</version></dependency><!-- mybatis 相關 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>${mybatis-spring.version}</version></dependency><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.5</version></dependency><!-- c3p0 相關 --><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>${c3p0.version}</version></dependency><!-- MySQL 驅動 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><!-- hibernate-validator 相關 --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>5.4.2.Final</version></dependency><!-- 日志相關 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>${slf4j.version}</version></dependency><!-- 單元測試 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!-- servlet-api 相關依賴開始 --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>javax.servlet.jsp-api</artifactId><version>2.3.1</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>1.1.2</version></dependency><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version><classifier>jdk15</classifier></dependency></dependencies>3.編寫配置文件
applicationContext-mybatis.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"><!-- 添加掃描所有的包 --> ? ?<context:component-scan base-package="com.demo.service" /> <!-- 加載資源文件 --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 配置c3p0連接池 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.user}" /><property name="password" value="${jdbc.password}" /><!-- 數據源個性化配置,根據實際項目需求進行: --><property name="initialPoolSize" value="3" /><property name="minPoolSize" value="3" /><property name="maxPoolSize" value="15" /><property name="maxConnectionAge" value="28800" /><!-- 最大閑置時間 單位秒 設置為6小時,主要目的避免mysql8小時陷阱 --><property name="maxIdleTime" value="21600" /></bean><!-- 配置SQLSessionFactoryBean --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 配置數據源 --><property name="dataSource" ref="dataSource" /><!-- 配置Mybatis配置文件位置 --><property name="configLocation" value="classpath:mybatis-config.xml" /><!-- 配置映射文件位置,該屬性也可以在Mybatis配置文件中進行 --><property name="mapperLocations" value="classpath:mapper/*.xml" /></bean><!-- 把sqlSessionFactory自動注入到mapper --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.demo.dao" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /></bean><!-- 配置事務管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- 定義事務通知以及事務屬性 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="save*" propagation="REQUIRED"/><tx:method name="add*" propagation="REQUIRED"/><tx:method name="create*" propagation="REQUIRED"/><tx:method name="insert*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/><tx:method name="edit*" propagation="REQUIRED"/><tx:method name="modify*" propagation="REQUIRED"/><tx:method name="merge*" propagation="REQUIRED"/><tx:method name="put*" propagation="REQUIRED"/><tx:method name="remove*" propagation="REQUIRED"/><tx:method name="delete*" propagation="REQUIRED"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"/><tx:method name="get*" propagation="SUPPORTS" read-only="true"/><tx:method name="count*" propagation="SUPPORTS" read-only="true"/><tx:method name="list*" propagation="SUPPORTS" read-only="true"/><tx:method name="*" propagation="SUPPORTS" read-only="true"/></tx:attributes></tx:advice><!-- 定義事務切面 --><aop:config><aop:pointcut expression="execution(* com.demo.service..*.*(..))" id="pc1"/><aop:advisor advice-ref="txAdvice" pointcut-ref="pc1"/></aop:config><!-- 開啟cglib代理方式 ,當目標類未實現接口時,利用cglib產生代理對象 --><aop:aspectj-autoproxy proxy-target-class="true"/></beans>applicationContext-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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"><!-- web.xml中shiro的filter對應的bean --><!-- Shiro 的Web過濾器 --><bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"><property name="securityManager" ref="securityManager" /><!-- loginUrl認證提交地址,如果沒有認證將會請求此地址進行認證,請求此地址將由formAuthenticationFilter進行表單認證 --><property name="loginUrl" value="/login.do" /><!-- 認證成功統一跳轉到first.action,建議不配置,shiro認證成功自動到上一個請求路徑 --><property name="successUrl" value="/index.do" /><!-- 通過unauthorizedUrl指定沒有權限操作時跳轉頁面 --><property name="unauthorizedUrl" value="/nofunc.jsp" /> ?<!-- 過慮器鏈定義,從上向下順序執行,一般將/**放在最下邊 --><property name="filterChainDefinitions"><value><!-- 對靜態資源設置匿名訪問 -->/static/* = anon/login.do = anon/toLogin.do=anon<!-- 過濾器對資源進行驗證 -->/index.do = authc<!-- 請求 logout地址,shiro去清除session -->/logout.do = logout<!-- 具有指定的權限才能訪問 -->/userManager.do = perms[user:manager]/roleManager.do = perms[role:manager]/toUserRole.do = perms[user:pression]<!-- /* = authc 所有url都必須認證通過才可以訪問 -->/* = authc</value></property></bean><!-- securityManager安全管理器 --><bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="realm" ref="authenRealm" /><property name="cacheManager" ref="cacheManager" /><property name="sessionManager" ref="sessionManager"></property></bean><!-- realm --><bean id="authenRealm" class="com.demo.realm.AuthenRealms"><!-- 將憑證匹配器設置到realm中,realm按照憑證匹配器的要求進行散列 --><property name="credentialsMatcher" ref="credentialsMatcher" /></bean><!-- 憑證匹配器 --><bean id="credentialsMatcher"class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"><property name="hashAlgorithmName" value="md5" /><property name="hashIterations" value="1" /></bean> ?<!-- 緩存的配置 --><bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"><property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml" /></bean> ?<!-- session管理器 --><bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"><!-- 配置session的失效時長,單位是毫秒 --><property name="globalSessionTimeout" value="100000"></property><!-- 刪除失效的session --><property name="deleteInvalidSessions" value="true"></property></bean> </beans>spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"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" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.2.xsd"><!-- 掃描基本包 --><context:component-scan base-package="com.demo.controller" /> ?<!-- 開啟aop,對類代理 --><aop:config proxy-target-class="true"></aop:config><!-- 開啟shiro注解支持 --><beanclass="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"><property name="securityManager" ref="securityManager"></property></bean> ?<!-- 異常統一處理 --><bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"><property name="defaultErrorView" value="index"></property><property name="exceptionAttribute" value="ex"></property></bean><!-- 添加注解驅動 --><mvc:annotation-driven /><!-- 視圖解析器 --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean><!-- 將springmvc不能處理的請求交給tomcat --><mvc:default-servlet-handler /> ? </beans>jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.user=root jdbc.password=root pool.initSize=3 pool.minSize=3 pool.maxSize=15 pool.MaxIdleTime=256800log4j.properties
log4j.rootCategory=INFO, stdout ? log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n ? log4j.category.org.springframework.beans.factory=DEBUG log4j.category.org.springframework=DEBUGmybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><settings><!-- 開啟二級緩存 --><setting name="cacheEnabled" value="true"/><setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/></settings> </configuration>shiro-ehcache.xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"><!--diskStore:緩存數據持久化的目錄 地址 --><diskStore path="E:\workspace\ehcache" /><defaultCache maxElementsInMemory="1000" maxElementsOnDisk="10000000"eternal="false" overflowToDisk="false" diskPersistent="false"timeToIdleSeconds="120"timeToLiveSeconds="120" diskExpiryThreadIntervalSeconds="120"memoryStoreEvictionPolicy="LRU"></defaultCache> </ehcache> ?web.xml <?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_3_0.xsd"id="WebApp_ID" version="3.0"><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- 加載spring的核心配置文件 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/applicationContext-*.xml</param-value></context-param><!-- 配置pringmvc的監聽器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 添加springmvc的核心控制器 --><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/spring-mvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping> ?<!-- 配置shiro框架的過濾器 --><filter><filter-name>shiroFilter</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> ?<!-- 設置true由servlet容器控制filter的生命周期 --><init-param><param-name>targetFilterLifecycle</param-name><param-value>true</param-value></init-param><!-- 設置spring容器filter的bean id,如果不設置則找與filter-name一致的bean --><init-param><param-name>targetBeanName</param-name><param-value>shiroFilter</param-value></init-param></filter><filter-mapping><filter-name>shiroFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 添加字符集過濾器 --><filter><filter-name>encoding</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>encoding</filter-name><url-pattern>/*</url-pattern></filter-mapping> ? </web-app>具體Controller層、Service層、Dao層代碼省略。
總結
以上是生活随笔為你收集整理的SSM整合shiro框架相关配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SSM整合shiro权限框架
- 下一篇: Docker简介以及mysql和redi