国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

springboot 整合apache shiro

2019-11-15 00:58:23
字體:
來源:轉載
供稿:網友
sPRingboot 整合apache shiro

這幾天因為項目需要,學習了下shiro,由此留下一些記錄,也希望對初學shiro的朋友有幫助。

springboot 是這兩年新興起來的一個項目,它的出現是為了減少springmvc開發過程中需要引入各種的jar包,各種xml配置文件,它充分利用了javaConfig的配置模式以及“約定優于配置”的理念,幫開發者配置大部分需要的東西,在github上的springboot項目里面,提供了很多列子,

而apache shiro 是一個輕量級的身份驗證與授權框架,與spring security 相比較,簡單易用,靈活性高,springboot本身是提供了對security的支持,畢竟是自家的東西。springboot暫時沒有集成shiro,這得自己配。

網上找了一些資料,配置shiro的,有很多需要在web.xml、application.xml里面各種配置,然而springboot 并沒有這些,而且springboot提倡無xml,使用javaconfig的配置方式,對這個不是很熟悉,但有人使用javaconfig的方式配置了shiro,參見這位csdn里面一位同學的博客spring boot 集成shiro的配置,下載了demo,然后模仿著成功配置了下。但習慣了xml的配置方式,感覺javaconfig的方式并不是很直觀,于是自己又把它換成了xml的方式。以下是主要的配置過程

首先是spring-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:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">    <!-- =========================================================                    Shiro Components         ========================================================= -->    <!-- 緩存管理器 使用Ehcache實現 -->    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">        <property name="cacheManagerConfigFile" value="classpath:app/config/ehcache-shiro.xml" />    </bean>    <!-- 虛需要自己寫的realm實現類 充當shiro和應用的安全數據的橋梁  -->    <bean id="MonitorRealm" class="com.test.MonitorRealm"></bean>        <!-- 安全管理器 -->    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">        <property name="realms">            <list>                <ref bean="MonitorRealm" />            </list>        </property>        <property name="cacheManager" ref="cacheManager" />    </bean>    <!-- Shiro生命周期處理器 -->    <!-- 官方對其的解釋是  http://shiro.apache.org/static/1.2.1/apidocs/org/apache/shiro/spring/LifecycleBeanPostProcessor.html    This post processor makes it easier to configure Shiro beans in Spring,     since the user never has to worry about whether or not    if they have to specify init-method and destroy-method bean attributes.             大意是使shiro bena 注入更加方便  -->    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />    <!-- Shiro的Web過濾器 -->    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">        <property name="securityManager" ref="securityManager" />        <property name="loginUrl" value="/user-web/login" />        <property name="unauthorizedUrl" value="/unauthorized  " />        <property name="filters">            <util:map>                <entry key="authc">                <!-- 身份驗證攔截器,默認為FormAuthenticationFilter,但 PassThruAuthenticationFilter功能相對強大,詳情見                https://shiro.apache.org/static/1.2.1/apidocs/org/apache/shiro/web/filter/authc/PassThruAuthenticationFilter.html-->                    <bean class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter" />                </entry>            </util:map>        </property>                <!-- shiro的強大的攔截器鏈,可以匹配全部的url,并根據配置進行攔截-->        <property name="filterChainDefinitions">            <value>                # 無需認證便可以訪問的的文件放在前面                /js/* = anon                /CSS/* = anon                /img/* = anon                /images/* = anon                /user-web/login = anon                /logout = logout                                /user-web/* = authc                /backend-web/* = authc            </value>        </property>    </bean>    <!-- 開啟Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP掃描使用Shiro注解的類,并在必要時進行安全邏輯驗證 -->    <!-- 這里要配置以下兩個bean,在這之前要配置好lifecycleBeanPostProcessor-->      <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">     <!--  加上下面這一句是為了解決If the controller requires proxying (e.g. due to @Transactional), please use class-based proxying  的報錯-->     <!-- 參考http://m.survivalescaperooms.com/digdeep/p/4624998.html 會發現上面錯誤是 Spring AOP 不同配置方式產生的沖突問題 -->       <property name="proxyTargetClass" value="true"/>        </bean>    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">        <property name="securityManager" ref="securityManager" />    </bean>        <!-- 異常攔截 -->    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">        <property name="exceptionMappings">            <props>                <prop key="org.apache.shiro.authz.UnauthorizedException">                       /unauthorized                                <!-- 未授權處理頁 -->                        </prop>                <prop key="org.apache.shiro.authz.UnauthenticatedException">                    /user-web/login                             <!-- 身份沒有驗證 -->                </prop>            </props>        </property>    </bean></beans>  
接著是Ehcache.xml文件。

ehcache是一個純Java的進程內緩存框架,相關介紹可以看這里

<ehcache updateCheck="false" name="shiroCache">    <defaultCache            maxElementsInMemory="10000"            eternal="false"            timeToIdleSeconds="120"            timeToLiveSeconds="120"            overflowToDisk="false"            diskPersistent="false"            diskExpiryThreadIntervalSeconds="120"            /></ehcache>
springboot加載xml配置文件
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@SpringBootApplication@ComponentScan@EnableAutoConfigurationpublic class Application {public static void main(String[] args) {SpringApplication.run(new String[] {"classpath*:app/config/spring-*.xml","classpath*:app/config/spring-session-redis.xml","classpath*:/user/captcha.xml"http://....}, args);}}

這樣。spingboot以xml形式配置shiro就完成了,后面在controller的方法上面使用注解的的方式,就可以進行權限控制。

這里沒有提供MonitorRealm類,里面要實現doGetAuthorizationInfo(授權)和doGetAuthenticationInfo(認證)兩個方法,還有就是loginController里面要做一些改動,有需要的朋友可以參考這篇SpringMVC整合Shiro博文。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 益阳市| 辽阳县| 旌德县| 寻甸| 屯留县| 萝北县| 新邵县| 台湾省| 布拖县| 襄垣县| 库伦旗| 始兴县| 皋兰县| 瓦房店市| 布尔津县| 齐齐哈尔市| 阿拉善右旗| 宁阳县| 玉山县| 资阳市| 河南省| 尚志市| 香港 | 青海省| 尚义县| 承德市| 日喀则市| 南丹县| 萝北县| 雷波县| 房产| 梨树县| 临颍县| 屏东县| 大同县| 女性| 万安县| 墨江| 高尔夫| 肇源县| 东安县|