shiro 与 SpringMVC的整合完美示例

网友投稿 281 2022-12-18

shiro 与 SpringMVC的整合完美示例

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

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

1.简单的页面

2.shiro配置

3.springmvc配置

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

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

下面是具体步骤

(1).添加依赖

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

dependencies {

// Use JUnit Jupiter for testing.

testImplementation 'org.junit.jupiter:junit-jupiter:5.7.1'

// This deflJNcLImpendency 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'

}

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

(2).Servlet中配置Shiro

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

org.apache.shiro.web.env.EnvironmentLoaderListener

shiroFilter

org.apache.shiro.web.servlet.ShiroFilter

shiroFilter

/*

在web.xml中配置好shiro后,需要在WEB-INF文件夹下书写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

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

springServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc-base.xml

springServlet

/*

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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.1.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.1.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.1.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.1.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd">

然后书写上面shiro配置文件中的对应controller和前端页面就可以了。具体如下:

3.1 TestController 及其页面

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";

}

}

对应前端页面,如下所示(别看他挺多的样子其实啥也没有,这里主要是将了一个在springmvc页面中也是中文的配置,可以当做没看见)

TestPage,


This Page only display English character.


To display chineses character, you should add an encoding filter in web.xml, like this




<filter>

<filter-name>encodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

     <param-name>encoding</param-name>

   http://  <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>

3.2 Test2Controller 及其页面

Test2Controller的代码

package shirospringweflJNcLImb.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";

}

}

其对应的界面如下:

Test 2 Page

This is the test 2 page

(Can not display chinese Character, more information click here)

3.3 LoginController 及其对应的界面

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";

}

}

其对应的登录界面

This is Login Page With No Chinese Character


username :

password :

好了,代码到这儿完事儿了,下面是运行的效果图

(4).运行效果

4.1 首先访问/app/test页面可以访问到(shiro未拦截)

页面如下

4.2 其实访问/app/test2需要登录,登录后需要后跳转(可能需要访问两次,有一次请求URL会带参数,这个shiro会报错,这是shiro的问题,可以百度解决)具体如下:

登录后跳转

访问/logout会退出到主页面,这个就不截图了

结束了

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java遍历Map四种方式讲解
下一篇:Spring Boot中如何使用Convert接口实现类型转换器
相关文章

 发表评论

暂时没有评论,来抢沙发吧~