Spring Security教程第五部分-数据库连接登录
2016-08-16 10:21
465 查看
这部分比前面几节稍微要复杂点,首先看看官方文档对于数据库是怎么定义的
Security Database Schema
因为我们只涉及到用户登录,为了简单,所以我们只看第一小节
to match the database dialect you are using.
简单来说,就是User Schema是用JDBC的连接方式与数据库进行通信,它可以负责对用户名 ,密码的验证以及对用户角色的鉴权.在这里需要申明的是,这个User Schema如果不是要自己重写的话,所有表的结构都是固定统一的,包含名称,字段都不可以修改.下面看看官方对数据库是怎么定义的
//创建用户表
接下来,再插入几条数据,
插入数据以后,接下来对配置文件进行更改
首先根据自己数据库类型,配置数据库的驱动,mysql如下配置
最后我们如何判断是否起作用了呢?
就是除了用户登录控制以外,还有用户权限控制,配置文件添加如下代码
此外还需要设置
到这里,重启工程,在tomcat下运行,
打开首页后,登录用户admin即可进入index,登录user则会跳到错误页面
Security Database Schema
因为我们只涉及到用户登录,为了简单,所以我们只看第一小节
1.1. User Schema
The standard JDBC implementation of theUserDetailsService(
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则会跳到错误页面
相关文章推荐
- 关于数据库连接字符串问题(第五部分连接access-2007)
- Spring Security教程第四部分-自定义登录页面
- php基础教程--数据库连接及登录注册
- Silverlight 教程第五部分:用 ListBox 和 DataBinding 显示列表数据 (木野狐译)
- 在JB里设置数据库连接[转贴,部分修正]
- SQL语言入门教程:第五课 删除数据库表格
- Spring Security连接数据库查询实例
- Silverlight教程第五部分:用 ListBox 和 DataBinding 显示列表数据 (木野狐译)
- win2003 + sqlServr 2000的怪问题,改了Adminsitrator的登录密码或计算机名后就不能从远程连接到这台机的数据库了
- Flex 3 连接数据库基本教程
- SQL Anywhere教程(六):连接数据库(Part II)
- Silverlight 教程第五部分:用 ListBox 和 DataBinding 显示列表数据
- 主流数据库之间对SQL:2003标准的不同实现方法比较(第五部分 Insert语句)
- JDBC连接各种数据库方法-JSP教程,数据库相关
- EJB3.0 学习教程(连载) 第五部分
- SQL Anywhere教程(五):连接数据库(Part I)
- javascript教程 - 第五部分 高级话题
- ASP.NET基础教程-SqlConnection对象-建立数据库连接
- 使用JSP连接ORACEL数据库做的登录框架
- 使用JSP连接ORACEL数据库做的登录框架