shiro在springmvc裡面的集成使用【轉】,shirospringmvc
- <dependency>
- <groupId>commons-collections</groupId>
- <artifactId>commons-collections</artifactId>
- <version>3.2.1</version>
- </dependency>
-
- <dependency>
- <groupId>net.sf.ehcache</groupId>
- <artifactId>ehcache-core</artifactId>
- <version>2.6.9</version>
- </dependency>
-
- <dependency>
- <groupId>org.apache.shiro</groupId>
- <artifactId>shiro-spring</artifactId>
- <version>1.2.3</version>
- </dependency>
-
- <dependency>
- <groupId>org.apache.shiro</groupId>
- <artifactId>shiro-ehcache</artifactId>
- <version>1.2.3</version>
- </dependency>
-
- <dependency>
- <groupId>org.apache.shiro</groupId>
- <artifactId>shiro-quartz</artifactId>
- <version>1.2.3</version>
- </dependency>
如果項目是hibernate的,以前的時候ehcache可能不是單例的,因為shiro裡面也使用到了ehcache做緩存,和hibernate的ehcache緩存配置有沖突,所以需要對hibernate的ehcache部分做些調整,調整如下:
Xml代碼
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource"></property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.hbm2ddl.auto">update</prop>
- <!--
- <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</prop>
- -->
- <prop key="hibernate.cache.region.factory_class">
- org.hibernate.cache.SingletonEhCacheRegionFactory
- </prop>
- <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
- <prop key="hibernate.cache.use_second_level_cache">true</prop>
- <prop key="hibernate.cache.use_query_cache">true</prop>
- <prop key="hibernate.cache.use_structured_entries">true</prop>
- <prop key="hibernate.cache.provider_configuration_file_resource_path">WEB-INF/classes/ehcache.xml</prop>
- <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
- </props>
- </property>
- <property name="packagesToScan">
- <list>
- <value>com.xxx.entity</value>
- </list>
- </property>
- </bean>
上面紅色的文字部分是需要調整的內容。
既然用到了ehcache,ehcahce.xml文件裡面的配置內容如下:
Xml代碼
- <?xml version="1.0" encoding="UTF-8"?>
- <ehcache>
- <diskStore path="java.io.tmpdir" />
-
- <defaultCache maxElementsInMemory="10000" eternal="false"
- timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />
- <cache name="org.hibernate.cache.UpdateTimestampsCache"
- maxElementsInMemory="5000" eternal="true" overflowToDisk="true" />
- <cache name="org.hibernate.cache.StandardQueryCache"
- maxElementsInMemory="10000" eternal="false" timeToLiveSeconds="120"
- overflowToDisk="true" />
- <!-- 登錄記錄緩存 鎖定10分鐘 -->
- <cache name="passwordRetryCache"
- maxEntriesLocalHeap="2000"
- eternal="false"
- timeToIdleSeconds="3600"
- timeToLiveSeconds="0"
- overflowToDisk="false"
- statistics="true">
- </cache>
-
- <cache name="authorizationCache"
- maxEntriesLocalHeap="2000"
- eternal="false"
- timeToIdleSeconds="3600"
- timeToLiveSeconds="0"
- overflowToDisk="false"
- statistics="true">
- </cache>
-
- <cache name="authenticationCache"
- maxEntriesLocalHeap="2000"
- eternal="false"
- timeToIdleSeconds="3600"
- timeToLiveSeconds="0"
- overflowToDisk="false"
- statistics="true">
- </cache>
-
- <cache name="shiro-activeSessionCache"
- maxEntriesLocalHeap="2000"
- eternal="false"
- timeToIdleSeconds="3600"
- timeToLiveSeconds="0"
- overflowToDisk="false"
- statistics="true">
- </cache>
- </ehcache>
然後是web.xml文件裡面加過濾器,注意要寫在springmvc的filter前面
Xml代碼
- <!-- shiro 安全過濾器 -->
- <!-- The filter-name matches name of a 'shiroFilter' bean inside applicationContext.xml -->
- <filter>
- <filter-name>shiroFilter</filter-name>
- <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
- <async-supported>true</async-supported>
- <init-param>
- <param-name>targetFilterLifecycle</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
-
- <!-- Make sure any request you want accessible to Shiro is filtered. /* catches all -->
- <!-- requests. Usually this filter mapping is defined first (before all others) to -->
- <!-- ensure that Shiro works in subsequent filters in the filter chain: -->
- <filter-mapping>
- <filter-name>shiroFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
然後就是shiro相關的spring配置參數文件了
Xml代碼
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:util="http://www.springframework.org/schema/util"
- 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
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
-
- <!-- 緩存管理器 使用Ehcache實現-->
- <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
- <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
- </bean>
-
- <bean id="passwordHelper" class="com.shinowit.framework.security.PasswordHelper">
- </bean>
-
- <!-- 憑證匹配器 -->
- <bean id="credentialsMatcher"
- class="com.shinowit.framework.security.credentials.RetryLimitSimpleCredentialsMatcher">
- <constructor-arg ref="cacheManager"/>
- <property name="passwordHelper" ref="passwordHelper"/>
- </bean>
-
-
- <bean id="shiro_user_dao" class="com.shinowit.framework.security.dao.UserDAO">
- <property name="jt" ref="jdbcTemplate"/>
- </bean>
-
- <!-- Realm實現 -->
- <bean id="userRealm" class="com.shinowit.framework.security.realm.UserRealm">
- <property name="userDAO" ref="shiro_user_dao"/>
- <property name="credentialsMatcher" ref="credentialsMatcher"/>
- <!--密碼校驗接口-->
- <property name="cachingEnabled" value="true"/>
- <property name="authenticationCachingEnabled" value="true"/>
- <property name="authenticationCacheName" value="authenticationCache"/>
- <property name="authorizationCachingEnabled" value="true"/>
- <property name="authorizationCacheName" value="authorizationCache"/>
- </bean>
-
- <!-- 會話ID生成器 -->
- <bean id="sessionIdGenerator" class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator"/>
-
- <!-- 會話Cookie模板 -->
- <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
- <constructor-arg value="sid"/>
- <property name="httpOnly" value="true"/>
- <property name="maxAge" value="180000"/>
- </bean>
-
- <bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
- <constructor-arg value="rememberMe"/>
- <property name="httpOnly" value="true"/>
- <property name="maxAge" value="2592000"/>
- <!-- 30天 -->
- </bean>
-
- <!-- rememberMe管理器 -->
- <bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
- <property name="cipherKey"
- value="#{T(org.apache.shiro.codec.Base64).decode('4AvVhmFLUs0KTA3Kprsdag==')}"/>
- <property name="cookie" ref="rememberMeCookie"/>
- </bean>
-
- <!-- 會話DAO -->
- <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
- <property name="activeSessionsCacheName" value="shiro-activeSessionCache"/>
- <property name="sessionIdGenerator" ref="sessionIdGenerator"/>
- </bean>
-
- <!-- 會話驗證調度器 -->
- <bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.quartz.QuartzSessionValidationScheduler">
- <property name="sessionValidationInterval" value="1800000"/>
- <property name="sessionManager" ref="sessionManager"/>
- </bean>
-
- <!-- 會話管理器 -->
- <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
- <property name="globalSessionTimeout" value="1800000"/>
- <property name="deleteInvalidSessions" value="true"/>
- <property name="sessionValidationSchedulerEnabled" value="true"/>
- <property name="sessionValidationScheduler" ref="sessionValidationScheduler"/>
- <property name="sessionDAO" ref="sessionDAO"/>
- <property name="sessionIdCookieEnabled" value="true"/>
- <property name="sessionIdCookie" ref="sessionIdCookie"/>
- </bean>
-
- <!-- 安全管理器 -->
- <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
- <property name="realm" ref="userRealm"/>
- <property name="sessionManager" ref="sessionManager"/>
- <property name="cacheManager" ref="cacheManager"/>
- <property name="rememberMeManager" ref="rememberMeManager"/>
- </bean>
-
- <!-- 相當於調用SecurityUtils.setSecurityManager(securityManager) -->
- <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
- <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
- <property name="arguments" ref="securityManager"/>
- </bean>
-
- <!--下面的loginUrl有兩個必要條件,一個登陸校驗失敗以後會強制客戶端redirect到這個url,
- 另外一個是登陸的表單(含有用戶名及密碼)必須action到這個url-->
- <!-- 自定義的能夠接收校驗碼的身份驗證過濾器
- 跳轉問題太他媽詭異了,不用了,自己寫代碼控制如何跳轉了
- <bean id="formAuthenticationFilter" class="com.shinowit.framework.security.filter.ValidFormAuthenticationFilter">
- <property name="usernameParam" value="loginName"/>
- <property name="passwordParam" value="loginPass"/>
- <property name="loginUrl" value="/login/"/>
- </bean>
- -->
-
- <!-- Shiro的Web過濾器 -->
- <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
- <property name="securityManager" ref="securityManager"/>
- <property name="loginUrl" value="/login/"/>
- <property name="unauthorizedUrl" value="/unauthorized.jsp"/>
- <property name="filters">
- <map>
- <entry key="authc">
- <bean class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter"/>
- </entry>
- </map>
- <!--
- <util:map>
- <entry key="authc" value-ref="formAuthenticationFilter"/>
- </util:map>
- -->
- </property>
- <property name="filterChainDefinitions">
- <value>
- /index.jsp = anon
- /validcode.jsp = anon
- /login/ = anon
- /static/** = anon
- /js/** = anon
- /img/** = anon
- /unauthorized.jsp = anon
- #/login/checklogin = authc
- /login/checklogin = anon
- /login/logoutlogout = logout
- /** = user
- </value>
- </property>
- </bean>
-
- <!-- Shiro生命周期處理器-->
- <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
-
-
- </beans>
哦,對了,裡面那個fuck那個url是用來改密碼的,因為數據庫裡面的密碼是加密的,不這麼整總也不可能知道對的md5值是多少。
但願沒有忘記什麼內容,挺墨跡的,不過能跑起來以後後邊關於權限和安全的處理就簡單多了,寫寫注解或者標簽就搞定了,很爽。
核心技術:Maven,Springmvc mybatis shiro, Druid, Restful,
Dubbo, ZooKeeper,Redis,FastDFS,ActiveMQ,Nginx
1. 項目核心代碼結構截圖
項目模塊依賴
特別提醒:開發人員在開發的時候可以將自己的業務REST服務化或者Dubbo服務化
2. 項目依賴介紹
2.1 後台管理系統、Rest服務系統、Scheculer定時調度系統依賴如下圖:
2.2 Dubbo獨立服務項目依賴如下圖:
3. 項目功能部分截圖:
zookeeper、dubbo服務啟動
dubbo管控台
REST服務平台