天天看點

Spring Security 學習之HTTP表單驗證

早已久仰Spring Security大名,一直沒機會實踐,最近計劃對其進行系統學習并通過bolg将心得記錄與博友們分享!

準備工作:

1. Spring Security 源碼和Samples可以從以下連結下載下傳:

<a href="https://github.com/spring-projects/spring-security/tree/master/samples" target="_blank">https://github.com/spring-projects/spring-security/tree/master/samples</a>

2. 從Spring官網下載下傳STS

3. 學習時使用的版本 -- Spring : 4.0.0.RELEASE,Spring Security : 3.2.0.RELEASE

曆史:

前身為“The Acegi Security System for Spring”,始于2006年,項目得到廣大認可和适用後更名為Spring Security納入Spring的項目之一。

适用場景:

JAVA應用安全管理中的認證和授權,特别是使用Spring架構開發的JAVA應用。

基本原理:

Spring的DI和AOP -- Spring Security大量使用AOP以避免對業務邏輯的幹涉,并與Spring核心架構深度內建。

javax.servlet.FilterChain -- 目前Spring Security主要用于web應用,在web應用中通過Filter攔截HTTP請求進行安檢。

<a href="http://s3.51cto.com/wyfs02/M01/12/1A/wKiom1L5lYPScmOhAAEvnSE3zVU615.jpg" target="_blank"></a>

HTTP表單認證:

Spring Security 内置HTTP表單認證支援,使用Security名字空間可以非常簡單讓Web應用支援HTTP表單認證,基本使用步驟如下:

1. web.xml配置

首先我們需要在web描述符中配置一個Filter名為springSecurityFilterChain供Spring架構使用,這個名稱不能自己随便更改,否則Spring架構會找不到。

1

2

3

4

5

6

7

8

<code>&lt;</code><code>filter</code><code>&gt;</code>

<code>  </code><code>&lt;</code><code>filter-name</code><code>&gt;springSecurityFilterChain&lt;/</code><code>filter-name</code><code>&gt;</code>

<code>  </code><code>&lt;</code><code>filter-class</code><code>&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/</code><code>filter-class</code><code>&gt;</code>

<code>&lt;/</code><code>filter</code><code>&gt;</code>

<code>&lt;</code><code>filter-mapping</code><code>&gt;</code>

<code>  </code><code>&lt;</code><code>url-pattern</code><code>&gt;/*&lt;/</code><code>url-pattern</code><code>&gt;</code>

<code>&lt;/</code><code>filter-mapping</code><code>&gt;</code>

2. Spring bean配置

Spring Security bean配置分兩部分,分别是資源通路權限配置和使用者定義,部分标簽解說:

http标簽 :用于建立FilterChainProxy和它将使用的bean。

auto-config="true" :表示以下配置

<code>&lt;</code><code>http</code><code>&gt;</code>

<code>    </code><code>&lt;</code><code>form-login</code> <code>/&gt;</code>

<code>    </code><code>&lt;</code><code>http-basic</code> <code>/&gt;</code>

<code>    </code><code>&lt;</code><code>logout</code> <code>/&gt;</code>

<code>  </code><code>&lt;/</code><code>http</code><code>&gt;</code>

intercept-url :定義被保護資源的通路權限

pattern :指定被保護的資源,可以使用正規表達式

access :通路權限定義,有多種方式,示例中使用角色,角色必須以ROLE_字首開始。

user :定義使用者名密碼和擁有的角色,密碼可以使用MD5加密。

9

10

11

12

13

14

15

16

17

18

19

20

21

<code>&lt;?</code><code>xml</code> <code>version</code><code>=</code><code>"1.0"</code> <code>encoding</code><code>=</code><code>"UTF-8"</code><code>?&gt;</code>

<code>&lt;</code><code>beans</code> <code>xmlns</code><code>=</code><code>"http://www.springframework.org/schema/beans"</code>

<code>    </code><code>xmlns:xsi</code><code>=</code><code>"http://www.w3.org/2001/XMLSchema-instance"</code> <code>xmlns:security</code><code>=</code><code>"http://www.springframework.org/schema/security"</code>

<code>    </code><code>xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd</code>

<code>        </code><code>http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd"&gt;</code>

<code>    </code><code>&lt;</code><code>security:http</code> <code>auto-config</code><code>=</code><code>"true"</code><code>&gt;</code>

<code>        </code><code>&lt;</code><code>security:intercept-url</code> <code>pattern</code><code>=</code><code>"/hello"</code>

<code>            </code><code>access</code><code>=</code><code>"ROLE_ADMIN"</code> <code>/&gt;</code>

<code>        </code><code>&lt;</code><code>security:intercept-url</code> <code>pattern</code><code>=</code><code>"/**"</code> <code>access</code><code>=</code><code>"ROLE_USER"</code> <code>/&gt;</code>

<code>    </code><code>&lt;/</code><code>security:http</code><code>&gt;</code>

<code>    </code><code>&lt;</code><code>security:authentication-manager</code><code>&gt;</code>

<code>        </code><code>&lt;</code><code>security:authentication-provider</code><code>&gt;</code>

<code>            </code><code>&lt;</code><code>security:user-service</code><code>&gt;</code>

<code>                </code><code>&lt;</code><code>security:user</code> <code>authorities</code><code>=</code><code>"ROLE_USER"</code> <code>name</code><code>=</code><code>"stevex"</code>

<code>                    </code><code>password</code><code>=</code><code>"stevex"</code> <code>/&gt;</code>

<code>                </code><code>&lt;</code><code>security:user</code> <code>authorities</code><code>=</code><code>"ROLE_USER, ROLE_ADMIN"</code>

<code>                    </code><code>name</code><code>=</code><code>"admin"</code> <code>password</code><code>=</code><code>"admin"</code> <code>/&gt;</code>

<code>            </code><code>&lt;/</code><code>security:user-service</code><code>&gt;</code>

<code>        </code><code>&lt;/</code><code>security:authentication-provider</code><code>&gt;</code>

<code>    </code><code>&lt;/</code><code>security:authentication-manager</code><code>&gt;</code>

<code>&lt;/</code><code>beans</code><code>&gt;</code>

實踐:

有很多安全相關的專業概念,需要自己慢慢認識,我們先建立一個執行個體,感性認識一下,步驟如下:

1. New--&gt;Spring Project--&gt;選擇"Spring MVC Project"模闆--Finish

2. 修改pom.xml,将Spring的版本更改為4.0.0.Release,增加Spring Security的依賴

<code>&lt;</code><code>dependency</code><code>&gt;</code>

<code>&lt;</code><code>groupId</code><code>&gt;org.springframework.security&lt;/</code><code>groupId</code><code>&gt;</code>

<code>    </code><code>&lt;</code><code>artifactId</code><code>&gt;spring-security-core&lt;/</code><code>artifactId</code><code>&gt;</code>

<code>    </code><code>&lt;</code><code>version</code><code>&gt;3.2.0.RELEASE&lt;/</code><code>version</code><code>&gt;</code>

<code>&lt;/</code><code>dependency</code><code>&gt;</code>

<code>&lt;</code><code>artifactId</code><code>&gt;spring-security-config&lt;/</code><code>artifactId</code><code>&gt;</code>

<code>&lt;</code><code>version</code><code>&gt;3.2.0.RELEASE&lt;/</code><code>version</code><code>&gt;</code>

<code>&lt;</code><code>artifactId</code><code>&gt;spring-security-web&lt;/</code><code>artifactId</code><code>&gt;</code>

3. 修改web.xml,增加springSecurityFilterChain

<code>&lt;</code><code>context-param</code><code>&gt;</code>

<code>        </code><code>&lt;</code><code>param-name</code><code>&gt;contextConfigLocation&lt;/</code><code>param-name</code><code>&gt;</code>

<code>        </code><code>&lt;</code><code>param-value</code><code>&gt;/WEB-INF/spring/root-context.xml /WEB-INF/spring/app-security.xml&lt;/</code><code>param-value</code><code>&gt;</code>

<code>    </code><code>&lt;/</code><code>context-param</code><code>&gt;</code>

<code>                                                                                     </code> 

<code>    </code><code>&lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;</code>

<code>    </code><code>&lt;</code><code>listener</code><code>&gt;</code>

<code>        </code><code>&lt;</code><code>listener-class</code><code>&gt;org.springframework.web.context.ContextLoaderListener&lt;/</code><code>listener-class</code><code>&gt;</code>

<code>    </code><code>&lt;/</code><code>listener</code><code>&gt;</code>

<code>    </code><code>&lt;</code><code>filter</code><code>&gt;</code>

<code>    </code><code>&lt;</code><code>filter-name</code><code>&gt;springSecurityFilterChain&lt;/</code><code>filter-name</code><code>&gt;</code>

<code>    </code><code>&lt;</code><code>filter-class</code><code>&gt;</code>

<code>    </code><code>org.springframework.web.filter.DelegatingFilterProxy</code>

<code>    </code><code>&lt;/</code><code>filter-class</code><code>&gt;</code>

<code>    </code><code>&lt;/</code><code>filter</code><code>&gt;</code>

<code>    </code><code>&lt;</code><code>filter-mapping</code><code>&gt;</code>

<code>    </code><code>&lt;</code><code>url-pattern</code><code>&gt;/*&lt;/</code><code>url-pattern</code><code>&gt;</code>

<code>    </code><code>&lt;/</code><code>filter-mapping</code><code>&gt;</code>

4. 增加app-security.xml

5. 修改HomeController.java,增加hello函數

22

23

24

25

26

27

28

29

30

31

32

33

34

<code>/**</code>

<code> </code><code>* Handles requests for the application home page.</code>

<code> </code><code>*/</code>

<code>@Controller</code>

<code>public</code> <code>class</code> <code>HomeController {</code>

<code>                                              </code> 

<code>    </code><code>private</code> <code>static</code> <code>final</code> <code>Logger logger = LoggerFactory.getLogger(HomeController.</code><code>class</code><code>);</code>

<code>    </code><code>/**</code>

<code>     </code><code>* Simply selects the home view to render by returning its name.</code>

<code>     </code><code>*/</code>

<code>    </code><code>@RequestMapping</code><code>(value = </code><code>"/"</code><code>, method = RequestMethod.GET)</code>

<code>    </code><code>public</code> <code>String home(Locale locale, Model model) {</code>

<code>        </code><code>logger.info(</code><code>"Welcome home! The client locale is {}."</code><code>, locale);</code>

<code>                                                  </code> 

<code>        </code><code>Date date = </code><code>new</code> <code>Date();</code>

<code>        </code><code>DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);</code>

<code>        </code><code>String formattedDate = dateFormat.format(date);</code>

<code>        </code><code>model.addAttribute(</code><code>"serverTime"</code><code>, formattedDate );</code>

<code>        </code><code>return</code> <code>"home"</code><code>;</code>

<code>    </code><code>}</code>

<code>    </code><code>//produces="text/plain" 必須有,否則會有亂碼</code>

<code>    </code><code>@RequestMapping</code><code>(value = </code><code>"/hello"</code><code>, method = RequestMethod.GET, produces=</code><code>"text/plain"</code><code>)</code>

<code>    </code><code>@ResponseBody</code>

<code>    </code><code>public</code> <code>String hello(){</code>

<code>        </code><code>logger.info(</code><code>"request coming!"</code><code>);</code>

<code>        </code><code>return</code> <code>"Hello Stevex, you are so hard!"</code><code>;</code>

<code>}</code>

6. 運作應用進行測試

<a href="http://s3.51cto.com/wyfs02/M00/12/1A/wKiom1L5lUvBlz-EAABLDxN1rDo408.jpg" target="_blank"></a>

大功告成!

<a href="http://down.51cto.com/data/2364050" target="_blank">附件:http://down.51cto.com/data/2364050</a>

     本文轉自sarchitect 51CTO部落格,原文連結:http://blog.51cto.com/stevex/1357939,如需轉載請自行聯系原作者