`
qq543123909
  • 浏览: 25290 次
社区版块
存档分类
最新评论

Spring Security 学习(3)

阅读更多

 

A) 上篇中用户名和密码直接写在配置文件中,而实际项目中我们是放在数据库中的。

好吧 . 开始把用户信息 和权限 放入 数据库 (oracle)

 

1.建表 

 

--用户表
create table users(
       username varchar2(50) not null primary key ,
       password varchar2(50) not null,
       enabled char(1) not null --是否禁用
);

-- 用户权限表
create table authorities(
       username varchar2(50) not null,
       authority varchar2(50) not null
);

--外键 
alter table authorities 
add constraint fk_username foreign key (username) references users(username);

--唯一索引
create unique index ix_auth_username on authorities (username,authority);

--插入测试数据. 
INSERT INTO users(username,PASSWORD,enabled)
VALUES('admin','21232f297a57a5a743894a0e4a801fc3',1);
INSERT INTO users(username,PASSWORD,enabled)
VALUES('user','ee11cbb19052e40b07aac0ca060c23ee',1);
 
INSERT INTO authorities VALUES('admin','ROLE_ADMIN');
INSERT INTO authorities VALUES('user','ROLE_USER');


select * from users

select * from authorities

 

2.添加Jar 包 

commons-dbcp-1.2.2.jar,commons-pool-1.3.jar,ojdbc6.jar,spring-jdbc-2.5.6.jar(这里导入的jar 一定要保持和以后项目所加入的Jar 版本一致)

 

3.配置.(applicationContext.xml)

 

	<!-- 数据库连接池 (DBCP) -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
		<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:houserent" />
		<property name="username" value="spring" />
		<property name="password" value="bdqn" />
	</bean>


	<!--配置认证管理器 -->
	<security:authentication-manager>
		<security:authentication-provider>
			<security:password-encoder hash="md5" />
			<!--指定了数据源-->
			<security:jdbc-user-service
				data-source-ref="dataSource" />
			<!-- <security:user-service> -->
			<!-- <security:user name="user" password="ee11cbb19052e40b07aac0ca060c23ee" -->
			<!-- authorities="ROLE_USER" /> -->
			<!-- </security:user-service> -->
		</security:authentication-provider>
	</security:authentication-manager>

 

现在就可以 测试 登陆信息 是来自数据库了.

 

遗留下来的问题 :

 实际项目中需要将这些放到数据数据库中,那么这些信息在数据库中的表结构是什么,能不能自己定义呢?spring Security将表结构已经定义好了,我们可以参考 发行文档中的.  继续学习中....

 

 

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics