Spring Security教程第五部分-数据库连接登录

这部分比前面几节稍微要复杂点,首先看看官方文档对于数据库是怎么定义的

Security Database Schema

因为我们只涉及到用户登录,为了简单,所以我们只看第一小节

1.1. User Schema

The standard JDBC implementation of the UserDetailsService (JdbcDaoImpl) requires tables to load the password, account status (enabled or disabled) and a list of authorities (roles) for the user. You will need to adjust this schema to match the database dialect you are using.

简单来说,就是User Schema是用JDBC的连接方式与数据库进行通信,它可以负责对用户名 ,密码的验证以及对用户角色的鉴权.在这里需要申明的是,这个User Schema如果不是要自己重写的话,所有表的结构都是固定统一的,包含名称,字段都不可以修改.下面看看官方对数据库是怎么定义的

create table users(
    username varchar_ignorecase(50) not null primary key,
    password varchar_ignorecase(50) not null,
    enabled boolean not null
);

create table authorities (
    username varchar_ignorecase(50) not null,
    authority varchar_ignorecase(50) not null,
    constraint fk_authorities_users foreign key(username) references users(username)
);
create unique index ix_auth_username on authorities (username,authority);
这里建了三个表,分别是用户表,权限表和索引表,需要注意的是,这里数据库默认的是HSQLDB database数据库,所以,对于MySql来说,其对应的建表的语句有所不同,实际中建表的SQL语句代码如下所示:

//新建并使用数据库
<pre id="recommend-content-1459970031" class="recommend-text mb-10">CREATE DATABASE SPRINGSECURITY; 
USE SPRINGSECURITY;

//创建用户表

create table users
(
username varchar(50) not null primary key, /*用户名*/
password varchar(50) not null, /*密码*/
enabled char not null /*是否禁用*/
);
/*权限表*/

create table authorities
(
username varchar(50) not null,
authority varchar(50) not null,
);
//创建索引

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

 接下来,再插入几条数据, 

//插入用户
INSERT INTO users(username,PASSWORD,enabled)VALUES('admin','admin',1);
INSERT INTO users(username,PASSWORD,enabled)VALUES('user','user',1);
//对应用户插入权限
INSERT INTO authorities VALUES('admin','ROLE_ADMIN');
INSERT INTO authorities VALUES('user','ROLE_USER');
这里分别赋予admin用户ROLE_ADMIN权限,user用户ROLE_USER权限,注意这里authority必须要是ROLE_开头的

插入数据以后,接下来对配置文件进行更改

首先根据自己数据库类型,配置数据库的驱动,mysql如下配置

<!-- 配置数据源 -->	
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
	  <property name="url" value="jdbc:mysql://localhost:3306/SPRINGSECURITY"/>
	  <property name="username" value="root"/>
	  <property name="password" value="123456"/>
	</bean>
接下来配置认证的管理器authentication-manager

<security:authentication-manager>
	<security:authentication-provider>
	       <security:jdbc-user-service data-source-ref="dataSource" />
	</security:authentication-provider>
</security:authentication-manager>
设置好data-source-ref为数据源里面的id后基本配置完毕

最后我们如何判断是否起作用了呢?

就是除了用户登录控制以外,还有用户权限控制,配置文件添加如下代码

<security:http auto-config="true">
	 <!-- 指定登录页面 -->
	 <security:form-login login-page="/IdeJsp/login.jsp"/>
	 <security:intercept-url pattern="/IdeJsp/index.jsp" access="ROLE_ADMIN" />
	 </security:session-management>
</security:http>
上面的配置功能实现了对index.jsp这个页面的权限控制,只允许ROLE_ADMIN用户进行访问

此外还需要设置

<!-- security3.1以后版版本设置首页不被拦截方法如下-->
<security:http pattern="/IdeJsp/login.jsp" security="none" />		
设置为login.jsp不进行拦截
到这里,重启工程,在tomcat下运行,

打开首页后,登录用户admin即可进入index,登录user则会跳到错误页面


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值