【Spring Security实战系列】Spring Security实战(三)

来源:互联网 发布:服装店记账软件手机 编辑:程序博客网 时间:2024/04/29 22:40
Spring Security实战(二)中讲解了用Spring Security自带的默认数据库存储用户和权限的数据,但是Spring Security默认提供的表结构太过简单了,其实就算默认提供的表结构很复杂,也不一定能满足项目对用户信息和权限信息管理的要求。那么接下来就讲解如何自定义数据库实现对用户信息和权限信息的管理。
一 自定义表结构
这里还是用的mysql数据库,所以pom.xml文件都不用修改。这里只要新建三张表即可,user表、role表、user_role表。其中user用户表,role角色表为保存用户权限数据的主表,user_role为关联表。user用户表,role角色表之间为多对多关系,就是说一个用户可以有多个角色。建表语句并插入数据:
- - 角色  create table role(      id bigint,      `name` varchar(50),      descn varchar(200)  );  alter table role add constraint pk_role primary key(id);  - - alter table role alter column id int generated by default as identity(1, 1);   - - 用户  create table `user`(      id bigint,      username varchar(50),      `password` varchar(50),      `status` integer,      descn varchar(200)  );  alter table `user` add constraint pk_user primary key(id);  - - alter table `user` alter column id bigint generated by default as identity(start with 1);    - - 用户角色连接表  create table user_role(      user_id bigint,      role_id bigint  );  alter table user_role add constraint pk_user_role primary key(user_id, role_id);  alter table user_role add constraint fk_user_role_user foreign key(user_id) references `user`(id);  alter table user_role add constraint fk_user_role_role foreign key(role_id) references role(id);- - 插入数据insert into user(id,username,password,status,descn) values(1,'admin','admin',1,'管理员');  insert into user(id,username,password,status,descn) values(2,'user','user',1,'用户');    insert into role(id,name,descn) values(1,'ROLE_ADMIN','管理员角色');  insert into role(id,name,descn) values(2,'ROLE_USER','用户角色');    insert into user_role(user_id,role_id) values(1,1);  insert into user_role(user_id,role_id) values(1,2);  insert into user_role(user_id,role_id) values(2,2); 
二 修改Spring Security的配置文件(applicationContext-security.xml)
现在我们要在这样的数据结构基础上使用Spring Security,Spring Security所需要的数据无非就是为了处理两种情况,一是判断登录用户是否合法,二是判断登陆的用户是否有权限访问受保护的系统资源。因此我们所要做的工作就是在现有数据结构的基础上,为Spring Security提供这两种数据。
在jdbc-user-service标签中有这样两个属性:
1. users-by-username-query为根据用户名查找用户,系统通过传入的用户名查询当前用户的登录名,密码和是否被禁用这一状态。
2.authorities-by-username-query为根据用户名查找权限,系统通过传入的用户名查询当前用户已被授予的所有权限。
所以 users-by-username-query属性要的是通过username来查询用户名、密码和是否可用;authorities-by-username-query属性是通过username来查询用户权限,所以在我们自定义的表结构的基础上对sql语句进行修改,得到如下配置:

<!-- 默认数据库对用户进行存储 Spring Security默认情况下需要两张表,用户表和权限表。-->    <authentication-manager>        <authentication-provider>            <jdbc-user-service data-source-ref="mysqlDataSource"                  users-by-username-query="select username,`password`,`status` as enabled from `user` where username = ?"                  authorities-by-username-query="select `user`.username,role.`name` from `user`,role,user_role where `user`.id=user_role.user_id and user_role.role_id=role.id and `user`.username = ?" />        </authentication-provider>    </authentication-manager>

这样最终得到的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?><beans:beans xmlns="http://www.springframework.org/schema/security"             xmlns:beans="http://www.springframework.org/schema/beans"             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"             xmlns:sec="http://www.springframework.org/schema/security"             xsi:schemaLocation="http://www.springframework.org/schema/beans                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd                        http://www.springframework.org/schema/context                        http://www.springframework.org/schema/context/spring-context-3.1.xsd                        http://www.springframework.org/schema/tx                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd                        http://www.springframework.org/schema/security                        http://www.springframework.org/schema/security/spring-security.xsd">    <!-- 配置不过滤的资源(静态资源及登录相关).是忽略拦截某些资源的意思,主要是针对静态资源 -->    <http pattern="/**/*.css" security="none"></http>    <http pattern="/**/*.jpg" security="none"></http>    <http pattern="/**/*.jpeg" security="none"></http>    <http pattern="/**/*.gif" security="none"></http>    <http pattern="/**/*.png" security="none"></http>    <http pattern="/js/*.js" security="none"></http>    <http pattern="/login.jsp" security="none"></http>    <http pattern="/getCode" security="none" /><!-- 不过滤验证码 -->    <http pattern="/test/**" security="none"></http><!-- 不过滤测试内容 -->    <http auto-config="true">        <!-- 表示访问app.jsp时,需要ROLE_SERVICE权限 -->        <intercept-url pattern="/adminPage.jsp" access="hasRole('ROLE_ADMIN')"></intercept-url>        <!--表示访问任何资源都需要ROLE_ADMIN权限。-->        <intercept-url pattern="/**" access="hasRole('ROLE_USER')"></intercept-url>    </http>    <!-- 导入数据源 -->    <beans:import resource="applicationContext-dataSource.xml"></beans:import>    <!-- 默认数据库对用户进行存储 Spring Security默认情况下需要两张表,用户表和权限表。-->    <authentication-manager>        <authentication-provider>            <jdbc-user-service data-source-ref="mysqlDataSource"                  users-by-username-query="select username,`password`,`status` as enabled from `user` where username = ?"                  authorities-by-username-query="select `user`.username,role.`name` from `user`,role,user_role where `user`.id=user_role.user_id and user_role.role_id=role.id and `user`.username = ?" />        </authentication-provider>    </authentication-manager></beans:beans>

和配置(如applicationContext.xml、pplicationContext-dataSource.xml、logback.xml、datasource.properties)和前面的实战二(Spring Security实战(二))完全一样,请参考前面的实战吧!

三 结果
由于只是换了用户信息和权限信息保存的方式,其他的都没有改变,效果和实战一的效果是一样的

阅读全文
0 0
原创粉丝点击