Spring Security使用数据库中的用户进行身份认证

本文在上一篇博文的基础上,将使用数据库中的用户进行身份认证。从本文中你将会看到Spring Security使用数据库中的用户进行身份认证依然是非常简单的事情。

1. 在pom.xml中添加mysql数据库驱动与c3p0数据源的相关的依赖。

<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.21</version> </dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version> </dependency>

2.准备MySQL数据库及相关数据。

本例子使用到user(用户表)、role(角色表)和user_role(用户角色表)三个表,表之间的关系如下:

为了方便大家进行测试,建表的语句如下:

/*Navicat MySQL Data Transfer Source Server: 10.0.0.12Source Server Version : 50619Source Host: 10.0.0.12:3305Source Database: favsecurity Target Server Type : MYSQLTarget Server Version : 50619File Encoding: 65001 Date: 2015-01-23 10:28:39*/ SET FOREIGN_KEY_CHECKS=0; — —————————— Table structure for role– —————————-DROP TABLE IF EXISTS `role`;CREATE TABLE `role` ( `id` int(11) NOT NULL DEFAULT '0' COMMENT 'id', `name` varchar(50) DEFAULT NULL COMMENT 'name', `descn` varchar(50) DEFAULT NULL COMMENT 'descn', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='角色表'; — —————————— Records of role– —————————-INSERT INTO `role` VALUES ('1', 'ROLE_ADMIN', '管理员角色');INSERT INTO `role` VALUES ('2', 'ROLE_USER', '用户角色'); — —————————— Table structure for user– —————————-DROP TABLE IF EXISTS `user`;CREATE TABLE `user` ( `id` int(11) NOT NULL DEFAULT '0' COMMENT 'id', `username` varchar(50) DEFAULT NULL COMMENT 'username', `password` varchar(50) DEFAULT NULL COMMENT 'password', `status` varchar(1024) DEFAULT NULL COMMENT 'status', `descn` varchar(1024) DEFAULT NULL COMMENT 'descd', PRIMARY KEY (`id`), KEY `AK_Key_1` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表'; — —————————— Records of user– —————————-INSERT INTO `user` VALUES ('1', 'admin', 'admin', '1', '管理\r\n员');INSERT INTO `user` VALUES ('2', 'user', 'user', '1', '用户\r\n');INSERT INTO `user` VALUES ('3', 'favccxx', 'favboy', '1', '帅锅'); — —————————— Table structure for user_role– —————————-DROP TABLE IF EXISTS `user_role`;CREATE TABLE `user_role` ( `user_id` int(11) DEFAULT NULL COMMENT '用户表_id', `role_id` int(11) DEFAULT NULL COMMENT '角色表_id', KEY `FK_FK_USER_ROLE_ROLE` (`role_id`), KEY `FK_FK_USER_ROLE_USER` (`user_id`), CONSTRAINT `FK_FK_USER_ROLE_USER` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`), CONSTRAINT `FK_FK_USER_ROLE_ROLE` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户角色表'; — —————————— Records of user_role– —————————-INSERT INTO `user_role` VALUES ('1', '1');INSERT INTO `user_role` VALUES ('1', '2');INSERT INTO `user_role` VALUES ('2', '2');INSERT INTO `user_role` VALUES ('3', '1');INSERT INTO `user_role` VALUES ('3', '2');

3.修改springSecurity.xml,更改security:authentication-provider提供的用户访问机制。

<security:authentication-manager>

<security:authentication-provider>

<security:user-service>

<security:user name="favccxx" password="favccxx" authorities="ROLE_USER,ROLE_ADMIN"/>

<security:user name="super" password="super" authorities="ROLE_SUPERADMIN"/>

</security:user-service>

</security:authentication-provider>

</security:authentication-manager>

将上面的灰色划掉的部分更改为下面绿色的部分。

<security:authentication-manager>

<security:authentication-provider>

有一种旅行,叫单车旅行。它没有奢侈准备,

Spring Security使用数据库中的用户进行身份认证

相关文章:

你感兴趣的文章:

标签云: