HttpBasic和FormLogin都是security的認證方式,這種認證是在用戶訪問被保護資源時進行登錄使用。本節主要講解如何使用HttpBasic實現認證。
我舉個簡單的例子,一個商品管理系統中擁有:查看商品,新增商品,修改商品,刪除商品這幾個功能。藉助這些功能使用一下HttpBasic。
1:如圖所示,新增一個商品controller
package com.xhc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 商品
*
* @author xuhongchang
*/
@Controller
@RequestMapping("/goods")
public class GoodsController {
/**
* 商品首頁
*
* @return
*/
@RequestMapping("/index")
public String index() {
return "index";
}
/**
* 商品新增
*
* @return
*/
@RequestMapping("/add")
public String add() {
return "goods/addGoods";
}
/**
* 商品修改
*
* @return
*/
@RequestMapping("/update")
public String update() {
return "goods/updateGoods";
}
/**
* 商品刪除
*
* @return
*/
@RequestMapping("/delete")
public String delete() {
return "goods/deleteGoods";
}
/**
* 商品展示
*
* @return
*/
@RequestMapping("/list")
public String list() {
return "goods/listGoods";
}
}
2:在springmvc.xml中增加配置。增加後的代碼如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:contenxt="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 掃描Controller類-->
<contenxt:component-scan base-package="com.xhc"/>
<!--註解方式處理器映射器和處理器適配器 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!--視圖解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前綴 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 後綴-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3:如下圖新增jsp/goods文件夾,並新增對應的jsp文件。具體頁面代碼分別列在下面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>新增商品</title>
</head>
<body>
新增商品頁面
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>商品刪除</title>
</head>
<body>
商品刪除頁面
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>顯示商品</title>
</head>
<body>
顯示商品頁面
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>商品更新</title>
</head>
<body>
商品更新頁面
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首頁</title>
</head>
<body>
商品信息管理</br>
<a href="${pageContext.request.contextPath}/goods/list">商品顯示</a></br>
<a href="${pageContext.request.contextPath}/goods/add">商品添加</a></br>
<a href="${pageContext.request.contextPath}/goods/update">商品修改</a></br>
<a href="${pageContext.request.contextPath}/goods/delete">商品刪除</a></br>
</body>
</html>
啓動項目,訪問ip+端口+項目名+goods/index
4:以上是爲使用springSecurity做準備。下面講解一下spring-security.xml文件中標籤的含義。
<!-- 從標籤的名字就可以大致看出各自的作用,具體的使用,下面會介紹到的 -->
<!-- security:http主要的主要作用是配置過濾器鏈
1: 保護資源
2: 定義什麼角色訪問什麼樣的資源
3: 定義認證方式
4: 定義登錄頁面,登錄請求地址,錯誤處理等
-->
<security:http>
<security:http-basic/>
</security:http>
<!-- 認證管理器:認證信息的提供方式,用戶名,密碼,權限等 -->
<security:authentication-manager>
</security:authentication-manager>
5:配置Http-basic
修改spring-security.xml文件的security:http標籤中的內容,security:http-basic即表示項目使用http-basic方式進行認證。
<security:http>
<!--
pattern:需要攔截的資源。/**表示攔截所有的資源
access:攔截的方式。
isFullyAuthenticated:資源需要認證才能被訪問
-->
<security:intercept-url pattern="/**" access="isFullyAuthenticated()"/>
<!-- 使用http-basic方式進行認證 -->
<security:http-basic/>
</security:http>
重新啓動項目進行訪問,出現下圖,表示配置成功。訪問項目資源需要登錄。
可是用戶名和密碼從哪裏得到呢?
還記得上面說的<security:authentication-manager>標籤嗎?修改配置,用戶名和密碼暫時寫死,權限一定要以ROLE_開頭。
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="xhc" password="123456" authorities="ROLE_USER"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
啓動運行,輸入用戶名密碼即可訪問資源。
到此,http-basic的配置方式基本完成。但是用戶名和密碼暫時是寫死,後面會將此換成從數據庫中讀取。稍安勿躁,稍安勿躁。