Shiro安全框架学习05 - 集成Web
Posted 麦田
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Shiro安全框架学习05 - 集成Web相关的知识,希望对你有一定的参考价值。
新建基于maven的web项目工程,工程结构如下:
pom.xml导入需要的包
<!-- shiro核心包 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 添加shiro web支持 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 添加log4j日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
Web.xml添加对Shiro的支持
<!-- 添加shiro支持 -->
<listener>
<listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>
<filter>
<filter-name>ShiroFilter</filter-name>
<filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
<init-param>
<param-name>configPath</param-name>
<param-value>/WEB-INF/shiro.ini</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>ShiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
web.xml中其他配置就是servlet的映射配置了,不同的servlet对应不同的请求url
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.itmyhome.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.itmyhome.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
配置一个 ini 文件 例如 shiro.ini
[users]
itmyhome=123456
login.jsp
<body>
<form action="$pageContext.request.contextPath /login" method="post">
username:
<input type="text" name="username" /> $msgUsername
<br>
password:
<input type="password" name="password" /> $msgPassword
<br>
<input type="submit" value="登陆">
</form>
</body>
success.jsp
<body>
欢迎你$username
<a href="$pageContext.request.contextPath /logout">退出</a>
</body>
浏览器中输入http://localhost:8080/shiro5-web/login
以上是关于Shiro安全框架学习05 - 集成Web的主要内容,如果未能解决你的问题,请参考以下文章