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
在這裏插入圖片描述

發佈了691 篇原創文章 · 獲贊 2948 · 訪問量 409萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章