spring mvc+Mybatis整合shiro 第一章 整體配置
前段時間研究了一下shiro,因為看不懂英文所以學習過程頗為曲折,後來整合shiro想寫一個sso結果這段時間又寫不下去了只好來寫寫部落格了。
最初學shiro是在百度搜教程,說實話那些教程除了開濤寫的其它人寫的確實不怎麼樣,而且開濤的教程也不是特別詳細的那種,所以我在這給大家寫一個整合後的完整教程。
shiro的設計思想我在這就不多說了,實話我說只明白一小半甚至一小半都沒有,要說思想開濤的部落格的前幾章講的很好,所以我在這不重複了這裡我只講些實用的東西能讓你快速上手的。
先上個Spring mvc的配置檔案,具體都是什麼配置請自行百度這個教程太多了我在這就不講了。
下面這個是重點shiro的spring配置:<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context" default-lazy-init="false"> <description>Spring公共配置 </description> <!-- 定義受環境影響易變的變數 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="ignoreResourceNotFound" value="true" /> <property name="locations"> <list> <!-- 標準配置 --> <value>classpath*:/application.properties</value> <value>classpath*:/config/shiro.properties</value> </list> </property> </bean> <!-- 啟用 @Required @Autowired,JSR 250's @PostConstruct, @PreDestroy and @Resource 等標註 --> <context:annotation-config /> <!-- 使用annotation 自動註冊bean,並保證@Required,@Autowired的屬性被注入 --> <context:component-scan base-package="com.sso" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 資料來源配置,使用應用內的DBCP資料庫連線池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- Connection Info --> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- Connection Pooling Info --> <property name="initialSize" value="${dbcp.initialSize}" /> <property name="maxActive" value="${dbcp.maxActive}" /> <property name="maxIdle" value="${dbcp.maxIdle}" /> <property name="defaultAutoCommit" value="false" /> </bean> <!-- 資料來源配置,使用應用伺服器的資料庫連線池 --> <!--<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/ExampleDB" />--> <!-- Mybatis配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value= "com.sso.entity,;" /> </bean> <!-- scan for mappers and let them be autowired --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value= "com.sso.dao,;" /> </bean> <!-- Transaction manager for a single JDBC DataSource --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 使用annotation定義事務 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <!-- SpringContext Holder --> <bean id="springContextHolder" class="com.sso.sys.SpringContextHolder" lazy-init="false"/> </beans>
從上到下依次給大家講解。<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-lazy-init="false"> <!-- shiroDbRealm --> <bean id="shiroDbRealm" class="com.sso.shiro.RealmManager"></bean> <!-- 安全管理器 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="shiroDbRealm" /> <property name="sessionManager" ref="sessionManager" /> <!-- 快取管理器 --> <property name="cacheManager" ref="shiroCacheManager" /> </bean> <!-- apache預設的session管理定時器 --> <bean name="sessionValidationScheduler " class="org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler"> </bean> <bean id="sessionManager" class="com.sso.shiro.MySessionManager"> <!-- 超時時間 --> <property name="globalSessionTimeout" value="${sessionTimeout}" /><!-- 這是shrio管理session的超時時間 --> <property name="sessionDAO" ref="shiroSessionDao" /><!-- 這裡對session的CURD操作 --> <property name="sessionIdCookie" ref="sharesession" /><!-- shiro自己的cookie管理器 --> <!-- 啟動shiro自己的session檢查定時器 定時檢查失效的session 預設半小時執行一次--> <property name="sessionValidationSchedulerEnabled" value="true" /> </bean> <bean id="sharesession" class="org.apache.shiro.web.servlet.SimpleCookie"> <property name="name" value="${domain}" /> </bean> <bean id="shiroSessionDao" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO" /> <!-- 單機session --> <bean id="shiroCacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" /> <!-- 自定義的許可權載入機制 --> <bean id="chainDefinitionSectionMetaSource" class="com.sso.shiro.DefaultChainDefinitionsFactoryBean" /> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager" /> <property name="loginUrl" value="/gotoLoginView" /> <property name="successUrl" value="/viewAllContacts.do" /> <property name="unauthorizedUrl" value="/meiquanxian" /> <property name="filterChainDefinitionMap" ref="chainDefinitionSectionMetaSource" /> <property name="filters"> <map> <entry key="perms"> <bean class="com.sso.shiro.MyPermsAuthorizationFilter"/> </entry> <entry key="authc"> <bean class="com.sso.shiro.MyFormAuthenticationFilter"/> </entry> <entry key="ws"> <bean class="com.sso.shiro.WebServiceAuthorizationFilter"/> </entry> </map> </property> </bean> <!-- 起效許可權註解,這個很少在web專案中用到,一般是控制url的訪問,不是在controller中宣告許可權註解 --> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /> </beans>
1、shiroDbRealm:這是一個自定義的realm,realm的作用我的理解就是用來給使用者登陸和設定許可權的。具體講解請看第二章。
2、securityManager:安全管理器,從它的注入屬性來看,他管理著使用者的realm、session、cache,我的理解是可以將他看作是一箇中心,所有的操作都要經過它的排程。3、sessionValidationScheduler:他是shiro自己的一個session管理器,從原始碼可以看出它其實就是一個執行緒,這個東西不需要手動注入,這裡列出來是因為我覺得預設30分鐘一次的重新整理頻率太低了,當初想通過這種方式來改掉他的預設時間,大家從我只寫了一行程式碼可以看出結果當然是失敗的。
4、sessionManager:這是對session所有操作的一個管理類。
5、sharsession:用來設定當前會話的cookie設定,屬性包括cookie所的屬性。
6、shiroCacheMnanager:chche管理器,這裡面是一個單機版的管理器,如果是叢集據說要重寫sessionDAO這個元件,具體的沒有研究過,過後再研究一下。
7、chainDefinitionSectionMetaSource:這個是我自己寫資源載入器,大家都知道shiro的標準配置方式是在xml裡面配置資源地址而在xml裡面配置地址會有很多的不便,經過研究最終寫出了通過專案啟動時訪問資料庫來載入地址的方式。具體的實現方式在後面會講到。
8、shiroFilter:這是shiro攔截器的入口位置,用來初始化所有需要的元件及引數。
9、lifecycleBeanPostProcessor:說實話不知道這是幹嘛的,看註釋意思是使用註解來控制權限。
下面是web.xml的一個設定:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<display-name>member</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext*.xml</param-value>
</context-param>
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<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>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
前半部分是對編碼的一個設定,中間部分是shiro的設定,在這裡要說一下shiro攔截器的載入方法與普通攔截器不一樣具體原理沒明白但有一點filter-name必須要與xml裡面定義的bean id一樣否則會報錯。在shiro的後面就是spring自己的配置了跟正常的一樣沒什麼好說的。
在這要說一句,shiro裡面接管了session預設的應該是30分鐘,如果在這裡設定的話應該就不起作用了需要在shiro的配置項裡面去設定globalSessionTimeOut。
相關推薦
spring mvc+Mybatis整合shiro 第一章 整體配置
前段時間研究了一下shiro,因為看不懂英文所以學習過程頗為曲折,後來整合shiro想寫一個sso結果這段時間又寫不下去了只好來寫寫部落格了。 最初學shiro是在百度搜教程,說實話那些教程除了開濤寫的其它人寫的確實不怎麼樣,而且開濤的教程也不是特別詳細的那種,所以我在這給
spring mvc+Mybatis整合shiro 第三章 SessionManager
sessionManager是shiro用來獲取對應session的一個管理類。他的作用是代替預設的方法來管理會話。 public class MySessionManager extends De
spring mvc+Mybatis整合shiro 第四章 SessionDAO
shiro的session都是存在快取中的,所有會有一個sessionDAO的類來做CRUD操作,這個類就是org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO。 它繼承了CachingSessi
Spring4 + Spring MVC + MyBatis 整合思路
ack ans bean shape 特性 smu locale serial shmat 1、Spring框架的搭建這個很簡單,只需要web容器中註冊org.springframework.web.context.ContextLoaderListener,並指定spri
Spring+Spring MVC+Mybatis整合配置AOP不生效的解決方案以及Bean初始化重複載入兩次(疑難雜症)
之前上班做spring+spring mvc +hibernate開發, 2年之久不做想複習一下aop的使用,結果配置遇見aop不生效,解決而記錄! 先上程式碼直接看反例效果會明顯: 首先看一下我的程式碼的包路徑: 接下來看Spring-MVC的配置檔案部分程式碼:
SSM(Spring + Spring MVC +Mybatis)的Redis快取,JedisPool配置
在SSM(Spring + Spring MVC +Mybatis)專案中使用到Redis快取,在網上查找了一些方法和文件後總結出自己可用的一些配置如下: 1、pom.xml檔案中引入需要的依賴 <!-- redis客戶端:jedis --> <d
spring mvc+mybatis ios android整合cms內容發布平臺
減少 連接 jquer 登錄 jdk版本 sso href com 分享圖片 開發語言: java、ios、android 部署平臺: linux、window jdk版本:JDK1.7以上版本 開發工具: eclipse、idea等 服務器中間件:Tomcat
基於Vue+Spring MVC+MyBatis+Shiro+Dubbo開發的分布式後臺管理系統
java dubbo shiro vue 分布式 最近項目中使用了shiro做權限管理,在開發過程中也踩了一些坑,於是便有了開發個應用鞏固一下所學知識的想法,正好在開發的過程裏學習一下Vue開發。技術棧方面,現在前後端分離大行其道,於是也采用了前後端分離的模式,前端基於Vue+Elemen
通過idea建立Maven專案整合Spring+spring mvc+mybatis
寫這篇文章是為了整理一下idea下整合maven專案的步驟,也為了以後讓室友們參考 建立專案 File→new→project
SSM整合配置(Spring+Spring MVC+Mybatis)
一、配置準備 通過Maven工程,在eclipse中整合SSM,並在Tomcat伺服器上執行 在進行配置前,先理清楚要配置哪些檔案,如圖,除web.xml外,其餘三個配置檔名稱均可自定義: 如圖所示,一共有四個需要手動配置的檔案: 1、web.
[轉]基於Vue+Spring MVC+MyBatis+Shiro+Dubbo開發的分散式後臺管理系統
最近專案中使用了shiro做許可權管理,在開發過程中也踩了一些坑,於是便有了開發個應用鞏固一下所學知識的想法,正好在開發的過程裡學習一下Vue開發。 技術棧方面,現在前後端分離大行其道,於是也採用了前後端分離的模式,前端基於Vue+Element,後端Web基於Spri
SSM框架整合配置詳解(spring,spring mvc,mybatis)
當今SSM框架已經成為了一種主流,其中spring,spring mvc和mybatis框架的功能很強大,給我們程式設計師節省了很多力氣,可以說這三種框架簡直就是我們程式設計師的福音,但是我們都知道,框架在自身帶來便捷的同時,也存在很多的配置檔案,更別說當三個框架整合的時候那就更加的困難了,
【 專欄 】- Spring+Spring mvc+mybatis+easyui整合開發例項
Spring+Spring mvc+mybatis+easyui整合開發例項 筆者打算從今天起每天抽出點時間整合下 spring+spring mvc+mybatis+easyui的例項,這裡將記錄從開始的配置到後來一步步的整合、
Java web 之 Spring+Mybatis+Spring MVC框架整合(下)
前兩篇,我們主要講了聚合工程Maven的建立(通過Maven本地倉庫,集中管理我們專案中的jar包),子Maven Project ssm-web 繼承父Maven Project ssm-par
spring mvc高階篇(八):Spring+SpringMVC+Mybatis整合(採用泛型和註解優化)
8.1整合思路分析 用Spring對MyBatis進行整合,將使得采用MyBatis操作資料庫更加方便和簡單。由於Spring 3的開發在MyBatis 3官方釋出前就結束了,Spring開發團隊不想釋出一個基於非釋出版本的MyBatis的整合支援,因此Spring 3沒有
spring mvc + mybatis + mysql 整合的一個簡單的登入例子
今天用spring跟mybatis整合寫了一個簡單的登入例子,第一次整合,給自己做個筆記,可能註釋寫的有點少,做的不足的地方謝謝指出,也分享給需要的朋友,下面給出登入失敗和成功的效果圖: 這個登入例子用的工具是myeclipse8.6+mysql5.1,使用到
mybatis+spring mvc 完美整合方案 查詢,保存,更新,刪除自動生成
esp mys null java ted 簡單 easyu make 主流框架 Jeecg-Mybatis版本代碼生成器演示視頻 代碼下載:JEECG-mybatis參考學習版本 簡要說明 JEECG[J2EE Code Generation] 代碼生成:根據表生成對應
SSM(Spring+Spring MVC+MyBatis) 框架整合
SSM(Spring+Spring MVC+MyBatis)是媲美於 ssm 框架的輕量級 Java EE 框架。在實際專案中,我們採用 SSM 框架進行開發,Spring MVC 用於 Web 層,相當於 Controller,處理請求並作出響應;MyBati
一步一步完成SSM框架整合(Spring+Spring MVC + Mybatis)
新建maven工程 eclipse中選擇new Maven Project項,制定archetype為webapp,填寫好groupId之類的,然後finish,這時可能工程有一些錯誤,通過修改Java Build Path,修復錯誤: 然後new一個s
spring.xml跟spring-mvc.xml跟Mybatis整合!以及log4j的配置
sprin跟Mybatis整合配置!!! <?xml version="1.0" encoding="UTF-8"?>