读书人

《Spring Security3》第四章第一部分翻

发布时间: 2012-12-18 12:43:41 作者: rapoo

《Spring Security3》第四章第一部分翻译上(数据库管理信息)

这次上传的部分内容是入门级的,比较简单,但是本章整体的功能还是非常重要的。

?

?

第四章??凭证安全存储

<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" 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/security http://www.springframework.org/schema/security/ spring-security3.0.xsd">

<jdbc:embedded-database id="dataSource" type="HSQL"> <jdbc:script location="classpath:security-schema.sql"/></jdbc:embedded-database>

?如果此时重启服务,你可以在日志上看到初始化HSQL数据库。需要记住的是<embedded-database>只会在内存中创建数据库,所以你在内存中看不到任何东西,也不能使用标准的工具进行查询。

配置JdbcDaoImpl凭证存储

<authentication-manager alias="authenticationManager"> <authentication-provider> <jdbc-user-service data-source-ref="dataSource"/> </authentication-provider></authentication-manager>

?【data-source-ref引用了我们在上一步声明<embedded-database>时定义的bean。】

添加用户定义到schema中

insert into users(username, password, enabled) values ('admin','admin',true);insert into authorities(username,authority) values ('admin','ROLE_USER');insert into authorities(username,authority) values ('admin','ROLE_ADMIN');insert into users(username, password, enabled) values ('guest','guest',true);insert into authorities(username,authority) values ('guest','ROLE_USER');commit;

<jdbc:embedded-database id="dataSource" type="HSQL"> <jdbc:script location="classpath:security-schema.sql"/> <jdbc:script location="classpath:test-data.sql"/></jdbc:embedded-database>

?在SQL添加到数据库配置后,我们应该能够启动应用并登录。Spring Security现在已经查找数据库的认证和GrantedAuthority信息。


?你可能意识到我们根本没有引用这个实现类。这是因为在更新后的Spring Security配置中<jdbc-user-service>声明会自动配置JdbcDaoImpl并将其织入到AuthenticationProvider中。在本章接下类的内容中,我们将会介绍如何配置Spring Security使用我们自定义的JdbcDaoImpl实现,它继续包含了修改密码功能(在第三章中我们添加到InMemoryDaoImpl了)。让我们看一下如何实现自定义的支持修改密码功能的JdbcDaoImpl子类配置。

读书人网 >其他数据库

热点推荐