下載已完成的項目: http : //www.mediafire.com/?tkm2vd9ro7oqhmu
首先,我們將研究如何在項目中添加密碼加密。
如下所示編輯spring安全文件。
<authentication-manager> <authentication-provider> <password-encoder hash='md5'/> <jdbc-user-service data-source-ref='dataSource' users-by-username-query='select username,password, 'true' as enabled from USER_DETAILS where username=?' authorities-by-username-query='select USER_DETAILS.username , USER_AUTH.AUTHORITY as authorities from USER_DETAILS,USER_AUTH where USER_DETAILS.username = ? AND USER_DETAILS.username=USER_AUTH.USERNAME ' /> </authentication-provider> </authentication-manager>
而已。 我們剛剛在項目中添加了md5密碼加密。
要對此進行測試,我們需要像下面一樣編輯出test-data.sql文件。
insert into USER_DETAILS values ('user','202cb962ac59075b964b07152d234b70'); -- password - 123 insert into USER_DETAILS values ('admin','21232f297a57a5a743894a0e4a801fc3'); -- password - admin insert into USER_AUTH values ('user', 'ROLE_USER'); insert into USER_AUTH values ('admin', 'ROLE_ADMIN');
現在,我們將研究如何基于HTML狀態代碼來自定義錯誤頁面。 否則,默認錯誤頁面將非常難看。 :D如果您對HTML狀態代碼沒有正確的了解,請查看this 。
在這里,我們正在處理403(拒絕權限)和404(找不到資源)狀態碼。 因為如果您要處理Spring Security,我們肯定需要處理這兩個狀態代碼(不是必須的,而是一種好習慣)
有多種方法可以做到這一點。 更改spring security xml并添加其他標簽可以做到這一點,但是在這里我們不會這樣做。 始終保持簡單。 因此,我們將編輯web.xml并將錯誤頁面標記添加到此任務。
在此之前,我們需要創建404和403自定義錯誤頁面。 創建兩個jsp頁面并將其放在webapp目錄下(不在WEB-INF目錄內)。
之后,更改web.xml并添加以下標記。
<error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page> <error-page> <error-code>403</error-code> <location>/403.jsp</location> </error-page>
而已。 我們只是自定義錯誤頁面
這些是我們可以使用Spring Security進行的一些基本操作。 在不久的將來,我將提出更多有關CAS集成,LDAP集成等等的Spring安全性的有趣文章。 敬請關注 :)
參考: Spring Security第2部分–密碼加密,來自我們JCG合作伙伴 Rajith Delantha的“ 自定義404和403錯誤”頁面 ,位于Looping with Rajith…博客中。
翻譯自: https://www.javacodegeeks.com/2012/07/spring-security-part-2-password.html