这篇主要的内容是:
自定义用户表和权限表
也是解决第三篇留下的问题,这个解决方案是在这个大哥的博客看到的http://downpour.iteye.com/blog/319965
下面就是我自己做的例子:
准备工作:
1.在数据库中 自定义我们的用户表 和 权限表.
-- 用户表 create table tb_users( id number primary key not null, c_username nvarchar2(50) not null, c_password nvarchar2(50) not null, c_enabled char(1) not null ); --权限表 create table tb_role( id number primary key not null, c_authority nvarchar2(50) not null ); --用户权限中间表 create table tb_user_role( id number primary key not null, c_user_id number not null, c_role_id number not null ); --添加相应的外键引用 和 索引 alter table tb_user_role add constraint fk_user_id foreign key (c_user_id) references tb_users(id); alter table tb_user_role add constraint fk_role_id foreign key(c_role_id) references tb_role(id); alter table tb_users add constraint uq_username unique(c_username); create unique index ix_user_role on tb_user_role (c_user_id,c_role_id); --插入测试数据. insert into tb_users values(1,'user','ee11cbb19052e40b07aac0ca060c23ee','1'); insert into tb_users values(2,'admin','21232f297a57a5a743894a0e4a801fc3','1'); insert into tb_users values(3,'zyk','a90157d9f9a8d683152ca521cf1ecfac','1'); select * from users insert into tb_role values(1,'ROLE_ADMIN'); insert into tb_role values(2,'ROLE_USER'); select * from tb_role insert into tb_user_role values(1,1,2); insert into tb_user_role values(2,2,1); insert into tb_user_role values(3,3,1); insert into tb_user_role values(4,3,2); select * from tb_user_role
上面的用户名 和 密码 都是一样的 例如用户名 user 对应的 密码是 user 经过MD5 处理出来后的字串
是ee11cbb19052e40b07aac0ca060c23ee 保存在数据库 出于安全性的考虑.
2.配置applicationCotext.xml
<!--配置认证管理器 --> <security:authentication-manager> <security:authentication-provider> <security:password-encoder hash="md5" /> <security:jdbc-user-service data-source-ref="dataSource" users-by-username-query="select u.c_username username,u.c_password password,u.c_enabled enabled from tb_users u where u.c_username=? " authorities-by-username-query="select u.c_username username,r.c_authority authority from tb_users u , tb_role r ,tb_user_role ur where u.id=ur.c_user_id and r.id=ur.c_role_id and u.c_username=?" /> <!-- <security:user-service> --> <!-- <security:user name="user" password="user" --> <!-- authorities="ROLE_USER" /> --> <!-- </security:user-service> --> </security:authentication-provider> </security:authentication-manager>
到此 使用 用户名 zyk 密码 zyk 进行登录成功 .测试Ok.
通过添加users-by-username-query 和authorities-by-username-query 属性,我们可
以使用你自己的SQL 覆盖默认的SQL 语句。这样我们就可以指定查询的是我们自己定义的表了
但是 我们必须确保我们的SQL 语句返回的列与Spring Security 所期待的一样
很显然的是 用户表 需要的 字段是 username , password,enabled .
权限表 则是 username ,authority
共同的是 他们都需要 username 作为where子句的限制条件.
相关推荐
三更springsecurity学习笔记
SpringSecurity学习总结源代码
Spring Security学习总结一
spring security方面的学习资料,包含:Spring+Security+3+与+CAS单点登录配置;Spring+Security3中文教程;Spring-Security安全权限管理手册;Spring+Security文库;还有一个学习笔记!
Spring Security学习总结二
Spring Security OAuth2.0学习笔记 什么是认证、授权、会话。 Java Servlet为支持http会话做了哪些事儿。 基于session认证机制的运作流程。 基于token认证机制的运作流程。 理解Spring Security的工作原理,Spring ...
Shiro+Spring Security学习文档,Spring+Security-3.0.1中文官方文档.pdf,跟我学Shiro教程.pdf。。。
spring security学习总结文档是我自己在学习security之后总结的,该部分是初级部分
SpringSecurity视频讲解,内含课程演示的源代码,,,,,,
Spring Security是一个功能强大且高度可定制的身份验证和授权框架,专门用于保护Java应用程序的安全性。它构建在Spring Framework基础之上,提供了全面的安全解决方案,包括身份验证、授权、攻击防护等功能。 Spring...
SpringSecurity学习总结一.pdf
适合Java新手的学习资源:介绍针对初学者设计的Spring Security学习资源,例如教程、视频课程或者书籍,并强调这些资源将如何帮助他们轻松入门并掌握关键概念。 学习收益:描述学习Spring Security的收益,例如提升...
family168上的学习spring security的资料,chm形式
和spring集成使用的完美权限框架,学习java一定要学会spring_security
视频详细讲解,需要的小伙伴自行网盘下载,链接见附件,永久有效。 首先,SSM环境中我们通过xml配置的...Springsecurity在两种不同的开发模式中使用,有经典的独立web后台管理系统,也有时下最流行的前后端分离场景。
SpringSecurity中文文档 2个 学习SpringSecurity必备
Spring Security-3中文官方文档(及教程),另有一篇详细的教程文档。
Spring Security学习总结 关于Spring Security的完全学习总结 强烈推荐
即使用一个基于Spring3的web工程作为基础,以理解使用Spring Security3使其保证安全的概念和策略。 不管你是不是已经使用Spring Security还是只是对这个软件有兴趣,就都会在本书中得到有用的信息。 在本节的内容中,你...