shiro与SpringMVC的整合用法

  介绍

这篇文章主要讲解了“shiro与SpringMVC的整合用法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“shiro与SpringMVC的整合用法”吧!

想要整合shiro和SpringMVC,在网上找了很多例子,感觉都有一点复杂,所以就自己写了一个最简单整合项目,记录在这里以备后面查看。

这个例子包含如下三个部分:

1。简单的页面

2. shiro配置

3。springmvc配置

shiro可以直接和春天整合,但是这样需要单独配置弹簧用于整合shiro,在配置springmvc。配置文件看起来乱七八糟的,所以这里就shiro不采用弹簧来管理。因此这里的整合类似shiro + servlet + springmvc。这样配置相对简单好理解。但也是最简单的配置,只能用于学习,用在实际项目中,还得改写。

下面是项目结构图(领域那个文件夹可有可没有,只是如果删除了,就需要将shiro。ini文件中myrealm=shirospringweb。领域xxxx那行删除掉)

 shiro与SpringMVC的整合用法

下面是具体步骤

(1)。添加依赖

既然是简单,那引用也是最少的,具体如下

dependencies  {   ,,,//,Use  JUnit  Jupiter  for 测试。   ,,,testImplementation  & # 39; org.junit.jupiter: junit-jupiter: 5.7.1& # 39;      ,,,//,却;能够dependency  is  used  by 从而应用程序。   ,,,implementation  & # 39; com.google.guava:番石榴:30.1 -jre& # 39;//,+ +,Adding  Spring 依赖关系   implementation  & # 39; org.springframework: spring上下文:5.3.9& # 39;   implementation  & # 39; org.springframework: spring-webmvc: 5.3.9& # 39;//,+ +,Using  Servlet  for  SpringMvc   implementation  & # 39; javax.servlet: javax.servlet-api: 4.0.1 # 39;//,+ +,Using  shiro-web   implementation  & # 39; org.apache.shiro: shiro-web: 1.7.1上# 39;   }

其中前面两个是建立项目自带的,所以只需要引用后面的四个包就可以咯

(2) .Servlet中配置Shiro

在servlet整合Shiro,只需要在网络上。xml中引用shiro的上下文监听器(读取shiro配置文件),并配置shiro过滤器即可,具体如下:

& lt; !——, shiro直接拦截,不经过spring ——比;   & lt; listener>   & lt; listener-class> org.apache.shiro.web.env.EnvironmentLoaderListener   & lt;/listener>   & lt; !——,对应过滤——比;   & lt; filter>   & lt; filter-name> shiroFilter   & lt; filter-class> org.apache.shiro.web.servlet.ShiroFilter   & lt;/filter>   & lt; !——, filter 映射——比;   & lt; filter-mapping>   & lt; filter-name> shiroFilter   & lt; url-pattern>/* & lt;/url-pattern>   & lt;/filter-mapping>

在web . xml中配置好shiro后,需要在web - inf文件夹下书写shiro。ini的配置文件,具体如下

[主要]   authc.loginUrl =/登录   时间=authc.usernameParam 用户名   时间=authc.passwordParam 密码   authc.successUrl =/authc.failureKeyAttribute =shiroLoginFailure   logout.redirectUrl =/myrealm =shirospringweb.realm.MyRealm      (用户)   时间=liang  123年,role1的角色   时间=wang  123年,role2      [url]/,=,不久/test =不久/test2 =authc/login =authc/logout =注销/* *,=,立刻

这样shiro就可以配合servlet工作了

(3)。配置springMVC

最简单springMVC的配置,只需要配置一个DispatcherServlet,并在其配置文件(资源/springmvc-base.xml)中打开包扫描,就好了,具体如下:

& lt; !——,配置SpringMVC ——比;   & lt; servlet>   & lt; servlet-name> springServlet   & lt; servlet-class> org.springframework.web.servlet.DispatcherServlet   & lt; init-param>   & lt; param-name> contextConfigLocation   & lt; param-value>类路径:springmvc-base.xml   & lt;/init-param>   & lt;/servlet>   & lt; !——,对应servlet  mapping ——比;   & lt; servlet-mapping>   & lt; servlet-name> springServlet   & lt; url-pattern>/* & lt;/url-pattern>   & lt;/servlet-mapping>

/springmvc-base资源。xml的配置文件如下(主要是打开包扫描和注解驱动):

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   http://www.springframework.org/schema/beans" & lt; beans  xmlns=?;   ,,,xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance"   ,,,xmlns:上下文=癶ttp://www.springframework.org/schema/context"   ,,,xmlns: mvc=癶ttp://www.springframework.org/schema/mvc"   xmlns: aop=癶ttp://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"的在         & lt;上下文:component-scan 基础包=皊hirospringweb.controller"/比;      & lt; mvc: annotation-driven /比;      & lt; !——,用来处理当,DispatcherServlet 拦截全部请求的时候,它在RequestHandler处捡出静态资源的请求,——比;   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

shiro与SpringMVC的整合用法