shiro 與 SpringMVC的整合完美示例
想要整合Shiro和springmvc,在網(wǎng)上找了很多例子,感覺(jué)都有一點(diǎn)復(fù)雜。所以就自己寫了一個(gè)最簡(jiǎn)單整合項(xiàng)目,記錄在這里以備后面查看。
這個(gè)例子包含如下三個(gè)部分:
1.簡(jiǎn)單的頁(yè)面
2.shiro配置
3.springmvc配置
shiro可以直接和spring整合,但是這樣需要單獨(dú)配置spring用于整合shiro,在配置springmvc。配置文件看起來(lái)亂七八糟的。所以這里就shiro不采用spring來(lái)管理。因此這里的整合類似 shiro + servlet + springmvc。這樣配置相對(duì)簡(jiǎn)單好理解。但也是最簡(jiǎn)單的配置,只能用于學(xué)習(xí),用在實(shí)際項(xiàng)目中,還得改寫。
下面是項(xiàng)目結(jié)構(gòu)圖(realm那個(gè)文件夾可有可沒(méi)有,只是如果刪除了,就需要將shiro.ini文件中 myrealm = shirospringweb.realm xxxx 那行刪除掉)
下面是具體步驟
(1).添加依賴
既然是簡(jiǎn)單,那引用也是最少的,具體如下
dependencies { // Use JUnit Jupiter for testing. testImplementation 'org.junit.jupiter:junit-jupiter:5.7.1' // This dependency is used by the application. implementation 'com.google.guava:guava:30.1-jre' // ++ Adding Spring dependencies implementation 'org.springframework:spring-context:5.3.9' implementation 'org.springframework:spring-webmvc:5.3.9' // ++ Using Servlet for SpringMvc implementation 'javax.servlet:javax.servlet-api:4.0.1' // ++ Using shiro-web implementation 'org.apache.shiro:shiro-web:1.7.1' }
其中前面兩個(gè)是建立項(xiàng)目自帶的,所以只需要引用后面的四個(gè)包就可以咯
(2).Servlet中配置Shiro
在servlet整合shiro,只需要在web.xml中引用shiro的上下文監(jiān)聽(tīng)器(讀取shiro配置文件),并配置shiro過(guò)濾器即可,具體如下:
<!-- Shiro直接攔截,不經(jīng)過(guò)spring --> <listener> <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class> </listener> <!-- 對(duì)應(yīng)filter--> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class> </filter> <!-- filter Mapping--> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
在web.xml中配置好shiro后,需要在WEB-INF文件夾下書(shū)寫shiro.ini的配置文件,具體如下
[main] authc.loginUrl = /login authc.usernameParam = username authc.passwordParam = password authc.successUrl = / authc.failureKeyAttribute = shiroLoginFailure logout.redirectUrl = / myrealm = shirospringweb.realm.MyRealm [users] liang = 123, role1 wang = 123, role2 [urls] / = anon /test = anon /test2 = authc /login = authc /logout = logout /** = anon
這樣shiro就可以配合servlet工作了
(3).配置springMVC
最簡(jiǎn)單springmvc的配置,只需要配置一個(gè)DispatcherServlet,并在其配置文件(resources/springmvc-base.xml)中打開(kāi)包掃描,就好了,具體如下:
<!-- 配置SpringMVC --> <servlet> <servlet-name>springServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-base.xml</param-value> </init-param> </servlet> <!-- 對(duì)應(yīng)servlet mapping --> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
resources/springmvc-base.xml的配置文件如下(主要是打開(kāi)包掃描和注解驅(qū)動(dòng)):
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="shirospringweb.controller"/> <mvc:annotation-driven /> <!-- 用來(lái)處理當(dāng) DispatcherServlet 攔截全部請(qǐng)求的時(shí)候,它在RequestHandler處撿出靜態(tài)資源的請(qǐng)求 --> <mvc:default-servlet-handler /> </beans>
然后書(shū)寫上面shiro配置文件中的對(duì)應(yīng)controller和前端頁(yè)面就可以了。具體如下:
3.1 TestController 及其頁(yè)面
TestController的代碼,如下所示
package shirospringweb.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TestController{ @RequestMapping("/test") public String testPage(){ return "WEB-INF/pages/test.html"; } }
對(duì)應(yīng)前端頁(yè)面,如下所示(別看他挺多的樣子其實(shí)啥也沒(méi)有,這里主要是將了一個(gè)在springmvc頁(yè)面中也是中文的配置,可以當(dāng)做沒(méi)看見(jiàn))
<html> <head> <title>TestPage</title> </head> <body> TestPage, <br/> This Page only display English character. <br /> To display chineses character, you should add an encoding filter in web.xml, like this <br /> <br /> <br /> <filter><br /> <filter-name>encodingFilter</filter-name><br /> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><br /> <init-param><br /> <param-name>encoding</param-name><br /> <param-value>UTF-8</param-value><br /> </init-param><br /> <init-param><br /> <param-name>forceEncoding</param-name><br /> <param-value>true</param-value><br /> </init-param><br /> </filter><br /> <filter-mapping><br /> <filter-name>encodingFilter</filter-name><br /> <url-pattern>/*</url-pattern><br /> </filter-mapping><br /> </body> </html>
3.2 Test2Controller 及其頁(yè)面
Test2Controller的代碼
package shirospringweb.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class Test2Controller { @RequestMapping("/test2") public String getTest2Page(){ return "WEB-INF/pages/test2.html"; } }
其對(duì)應(yīng)的界面如下:
<html> <head> Test 2 Page </head> <body> This is the test 2 page <br/> (Can not display chinese Character, more information <a href="/app/test" rel="external nofollow" >click here</a>) </body> </html>
3.3 LoginController 及其對(duì)應(yīng)的界面
LoginController的代碼
package shirospringweb.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class LoginController { @RequestMapping("/login") public String getLogin(){ return "WEB-INF/pages/login.html"; } }
其對(duì)應(yīng)的登錄界面
<html> <head>Login Page</head> <body> This is Login Page With No Chinese Character <br> <form action="/app/login" method="POST"> username : <input name="username" /><br/> password : <input name="password" /><br/> <input type="submit" value="LOGIN"/> </form> </body> </html>
好了,代碼到這兒完事兒了,下面是運(yùn)行的效果圖
(4).運(yùn)行效果
4.1 首先訪問(wèn)/app/test頁(yè)面可以訪問(wèn)到(shiro未攔截)
頁(yè)面如下
4.2 其實(shí)訪問(wèn)/app/test2需要登錄,登錄后需要后跳轉(zhuǎn)(可能需要訪問(wèn)兩次,有一次請(qǐng)求URL會(huì)帶參數(shù),這個(gè)shiro會(huì)報(bào)錯(cuò),這是shiro的問(wèn)題,可以百度解決)具體如下:
登錄后跳轉(zhuǎn)
訪問(wèn)/logout會(huì)退出到主頁(yè)面,這個(gè)就不截圖了
結(jié)束了
到此這篇關(guān)于shiro 與 SpringMVC的整合完美示例的文章就介紹到這了,更多相關(guān)shiro 整合 SpringMVC 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis-plus更新對(duì)象時(shí)將字段值更新為null的實(shí)現(xiàn)方式
mybatis-plus在執(zhí)行更新操作,當(dāng)更新字段為 空字符串 或者 null 的則不會(huì)執(zhí)行更新,如果要將指定字段更新null,可以通過(guò)以下三種方式實(shí)現(xiàn),感興趣的小伙伴跟著小編一起來(lái)看看吧2023-10-10SpringBoot+devtools實(shí)現(xiàn)熱部署的示例代碼
在軟件項(xiàng)目的開(kāi)發(fā)過(guò)程中,不可避免的會(huì)經(jīng)常修改代碼,每次修改代碼,都需要手動(dòng)停止然后再啟動(dòng)服務(wù),最后驗(yàn)證代碼的正確性,今天通過(guò)這篇文章,我們一起來(lái)學(xué)習(xí)一下如何使用Spring?Boot?+?devtools?輕松搞定熱部署,需要的朋友可以參考下2024-08-08MyBatis代碼自動(dòng)生成器Mybatis-Generator的使用詳解
本文詳細(xì)介紹如何在SpringBoot項(xiàng)目中使用MyBatis-Generator進(jìn)行代碼生成,包括配置文件的添加、POM依賴配置、運(yùn)行配置等步驟,通過(guò)自動(dòng)生成代碼,可以簡(jiǎn)化MyBatis的繁瑣配置和SQL編寫,提高開(kāi)發(fā)效率,注意要考慮MySQL版本兼容性,以及確保路徑配置正確2024-10-10Java中的ReentrantLock、ReentrantReadWriteLock、StampedLock詳解
這篇文章主要介紹了Java中的ReentrantLock、ReentrantReadWriteLock、StampedLock詳解,讀寫鎖:一個(gè)資源能夠被多個(gè)讀線程訪問(wèn),或者被一個(gè)寫線程訪問(wèn)但是不能同時(shí)存在讀寫線程,需要的朋友可以參考下2024-01-01使用Lombok @Builder注解導(dǎo)致默認(rèn)值無(wú)效的問(wèn)題
這篇文章主要介紹了使用Lombok @Builder注解導(dǎo)致默認(rèn)值無(wú)效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08