加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

spring security使用数据库管理用户权限

发布时间:2020-12-15 01:50:52 所属栏目:大数据 来源:网络整理
导读:div class="cnblogs_code" password= authorities= /> password= authorities= /> 将上述配置代码配置为 = /> 现在只要再为jdbc-user-service提供一个dataSource就可以让Spring Security使用数据库中的权限信息了。在此我们使用spring创建一个演示用的dataSo

<div class="cnblogs_code">

    
         password= authorities= />
         password= authorities= />
    

将上述配置代码配置为

=/>

现在只要再为jdbc-user-service提供一个dataSource就可以让Spring Security使用数据库中的权限信息了。在此我们使用spring创建一个演示用的dataSource实现,这个dataSource会连接到hsqldb数据库,从中获取用户权限信息。[]

=> value=/> value=/> value=/> value=/>

最终的配置文件如下所示:

encoding=?> === http: http: http:
<http auto-config=<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;true</span><span style="color: #800000;"&gt;'</span>>
    <intercept-url pattern=<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;/admin.jsp</span><span style="color: #800000;"&gt;"</span> access=<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;ROLE_ADMIN</span><span style="color: #800000;"&gt;"</span> />
    <intercept-url pattern=<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;/**</span><span style="color: #800000;"&gt;"</span> access=<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;ROLE_USER</span><span style="color: #800000;"&gt;"</span> />
</http>

<authentication-manager>
    <authentication-provider>
        <jdbc-user-service data-source-<span style="color: #0000ff;"&gt;ref</span>=<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;dataSource</span><span style="color: #800000;"&gt;"</span>/>
    </authentication-provider>
</authentication-manager>

//连接数据库
<beans:bean id=<span style="color: #800000;">"<span style="color: #800000;">dataSource<span style="color: #800000;">" <span style="color: #0000ff;">class=<span style="color: #800000;">"<span style="color: #800000;">org.springframework.jdbc.datasource.DriverManagerDataSource<span style="color: #800000;">">
<beans:property name=<span style="color: #800000;">"<span style="color: #800000;">driverClassName<span style="color: #800000;">" value=<span style="color: #800000;">"<span style="color: #800000;">org.hsqldb.jdbcDriver<span style="color: #800000;">"/>
<beans:property name=<span style="color: #800000;">"<span style="color: #800000;">url<span style="color: #800000;">" value=<span style="color: #800000;">"<span style="color: #800000;">jdbc:hsqldb:res:/hsqldb/test<span style="color: #800000;">"/>
<beans:property name=<span style="color: #800000;">"<span style="color: #800000;">username<span style="color: #800000;">" value=<span style="color: #800000;">"<span style="color: #800000;">sa<span style="color: #800000;">"/>
<beans:property name=<span style="color: #800000;">"<span style="color: #800000;">password<span style="color: #800000;">" value=<span style="color: #800000;">""/>

Spring Security默认情况下需要两张表,用户表和权限表。以下是hsqldb中的建表语句:

create table users(150) not 50) not not create table authorities (2<span style="color: #000000;">
username varchar_ignorecase(
50) not <span style="color: #0000ff;">null
<span style="color: #000000;">,authority varchar_ignorecase(
50) not <span style="color: #0000ff;">null
<span style="color: #000000;">,constraint fk_authorities_users foreign key(username) references users(username)
);

create unique index ix_auth_username on authorities (username,authority);3

1.users:用户表。包含username用户登录名,password登陆密码,enabled用户是否被禁用三个字段。

其中username用户登录名为主键。

?

2.authorities:权限表。包含username用户登录名,authorities对应权限两个字段。

其中username字段与users用户表的主键使用外键关联。

3.对authorities权限表的username和authority创建唯一索引,提高查询效率

Spring Security会在初始化时,从这两张表中获得用户信息和对应权限,将这些信息保存到缓存中。其中users表中的登录名和密码用来控制用户的登录,而权限表中的信息用来控制用户登陆后是否有权限访问受保护的系统资源。

我们在示例中预先初始化了一部分数据:

insert into users(username,password,enabled) values('admin','admin','user','user',insert into authorities(username,authority) values('admin','ROLE_ADMIN'<span style="color: #000000;">);
insert into authorities(username,'ROLE_USER'<span style="color: #000000;">);
insert into authorities(username,authority) values(
'user','ROLE_USER');

这个实现和之前将用户和用户权限写在配置文件中明显方便很多。尤其是用户数量过多时。不过这种方法是保持最基本的表结构,也是默认的表结构。最好在理解原理基础上自定义数据库。(方法后续讲到。)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读