下载已完成的项目: 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