您好,登錄后才能下訂單哦!
我們首先來一個(gè)簡(jiǎn)單Spring Security登錄,首先需要搭建環(huán)境,這里我們用Spring+SpringMVC+Spring Security,數(shù)據(jù)庫用Hibernate4+Oracle,關(guān)于jar包,Spring以及SpringMVC我用的是3.2版本的。
在web.xml中我們主要是配置Spring、SpringMVC以及Spring Security的集成。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="DogStoreApp" version="2.5"> <display-name>Dog Store</display-name> <!-- 集成spring的通用配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- SpringMVC前端控制器 --> <servlet> <servlet-name>dogstore</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dogstore</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- springSecurity核心過濾器配置 --> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
現(xiàn)在我們需要配置Spring Security的組件,在配置Spring Security的權(quán)限控制文件之前,我們來拓展一下Spring Security權(quán)限控制方法:
1、不用數(shù)據(jù)庫,全部數(shù)據(jù)寫在配置文件,這個(gè)也是官方文檔里面的demo;
2、使用數(shù)據(jù)庫,根據(jù)spring security默認(rèn)實(shí)現(xiàn)代碼設(shè)計(jì)數(shù)據(jù)庫,也就是說數(shù)據(jù)庫已經(jīng)固定了,這種方法不靈活,而且那個(gè)數(shù)據(jù)庫設(shè)計(jì)得很簡(jiǎn)陋,實(shí)用性差;
3、spring security和Acegi不同,它不能修改默認(rèn)filter了,但支持插入filter,所以根據(jù)這個(gè),我們可以插入自己的filter來靈活使用;
4、暴力手段,修改源碼,前面說的修改默認(rèn)filter只是修改配置文件以替換filter而已,這種是直接改了里面的源碼,但是這種不符合OO設(shè)計(jì)原則,而且不實(shí)際,不可用。
現(xiàn)在配置dogstore-security.xml這個(gè)文件,關(guān)于命名空間配置,官方提供了兩種配置方案
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:security="http://www.springframework.org/schema/security" 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-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> ... </beans>
第二種、命名空間用security開頭,在配置中不需要security前綴,但是bean的配置需要用<beans:bean>配置
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" 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-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> ... </beans:beans>
首先我們先按照上面第一種權(quán)限控制來做個(gè)簡(jiǎn)單的demo:dogstore-security.xml,注意自己引用的jar與命名空間版本要一致
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> <http auto-config="true"> <intercept-url pattern="/*" access="ROLE_USER"/> </http> <authentication-manager alias="authenticationManager"> <authentication-provider> <user-service> <user authorities="ROLE_USER" name="guest" password="guest"/> </user-service> </authentication-provider> </authentication-manager> </beans:beans>
第一個(gè)http標(biāo)簽其實(shí)主要是配置攔截url用的,里邊大概配置了如果你要訪問某個(gè)路徑,需要哪個(gè)連接權(quán)限,而http標(biāo)簽下邊的authentication-manger標(biāo)簽下的標(biāo)簽則配置了那些用戶都擁有哪些權(quán)限,目前我們先暫時(shí)按這個(gè)步驟去做,后面詳細(xì)介紹。
現(xiàn)在我們來完善Spring、SpringMVC所需的配置文件,上面web.xml中我們已經(jīng)預(yù)留了contextConfigLocation來引入配置文件,首先創(chuàng)建dogstore-base.xml空文件,這是Spring配置文件,如果后面需要什么,我們?cè)偬砑?/span>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> </beans>
SpringMVC的配置文件,先配置視圖解析,SpringMVC配置會(huì)自動(dòng)查找配置文件,Servlet的名字是(<servlet-name>)是dogstore,約定勝于配置將會(huì)在WEB-INF目錄下尋找名為dogstore-servelt.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value=""/> <property name="suffix" value=".jsp"/> </bean> </beans>
目前Spring、SpringMVC以及Spring Security配置文件基本用法已經(jīng)配置,引入web.xml,SpringMVC自己引入配置文件。
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/dogstore-security.xml /WEB-INF/dogstore-base.xml </param-value> </context-param>
將上面配置的加入tomcat,啟動(dòng),在沒有自定義登錄頁面之前,SpringSecurity會(huì)自動(dòng)生成登錄頁面,如下圖,我們?cè)趙eb.xml下添加首頁來驗(yàn)證攔截是否有效
<welcome-file-list> <welcome-file>main.jsp</welcome-file> </welcome-file-list>
在WebContent下添加main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> 我已經(jīng)登錄進(jìn)來了! </body> </html>
輸入錯(cuò)誤的賬號(hào)或者錯(cuò)誤密碼
輸入上面配置的guest/guest,登錄成功。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。