多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php框架 > 框架設計 > Spring Security使用數據庫中的用戶進行身份認證

Spring Security使用數據庫中的用戶進行身份認證

來源:程序員人生   發布時間:2015-08-17 08:24:29 閱讀次數:3697次

本文在上1篇博文的基礎上,將使用數據庫中的用戶進行身份認證。從本文中你將會看到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(用戶角色表)3個表,表之間的關系以下:


wKioL1TBtlex1oMgAACUVwD_IMc921.jpg


    為了方便大家進行測試,建表的語句以下:

/* Navicat MySQL Data Transfer Source Server : 10.0.0.12 Source Server Version : 50619 Source Host : 10.0.0.12:3305 Source Database : favsecurity Target Server Type : MYSQL Target Server Version : 50619 File Encoding : 65001 Date: 2015-01⑵3 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', '管理 員'); INSERT INTO `user` VALUES ('2', 'user', 'user', '1', '用戶 '); 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>

       <security:jdbc-user-service data-source-ref="dataSource" users-by-username-query="select username,password,1 as enabled from user WHERE username=?"

        authorities-by-username-query="select u.username, r.name as role from user u,user_role ur, role r where u.id=ur.user_Id and r.id = ur.role_Id and u.username=?"/>

      </security:authentication-provider>

 </security:authentication-manager>


    備注:dataSource在springdb.xml中定義,在springSecurity.xml中援用springdb.xml便可。springdb.xml相干的文件以下:

<?xml version="1.0" encoding="UTF⑻"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans⑶.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context⑶.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx⑶.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop⑶.0.xsd "> <context:property-placeholder location="classpath:database.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass"> <value>${dataSource.driverClassName}</value> </property> <property name="jdbcUrl"> <value>${dataSource.url}</value> </property> <property name="user"> <value>${dataSource.username}</value> </property> <property name="password"> <value>${dataSource.password}</value> </property> <!-- 最大連接數 --> <property name="maxPoolSize"> <value>${dataSource.c3p0.max_size}</value> </property> <!-- 最小連接數 --> <property name="minPoolSize"> <value>${dataSource.c3p0.min_size}</value> </property> <!-- 最大空閑時間,超時未被使用則連接被拋棄,單位毫秒 --> <property name="maxIdleTime"> <value>${dataSource.c3p0.max_idle_time}</value> </property> <!-- 取得連接的超時時間,如果超過這個時間,會拋出異常,單位毫秒 --> <!-- <property name="checkoutTimeout"> <value>${dataSource.c3p0.checkout_timeout}</value> </property> --> <!-- 最大的PreparedStatement的數量 --> <property name="maxStatements"> <value>${dataSource.c3p0.max_statements}</value> </property> <!-- 每隔120秒檢查連接池里的空閑連接 ,單位是秒 --> <property name="idleConnectionTestPeriod"> <value>${dataSource.c3p0.idle_test_period}</value> </property> <!-- 當連接池里面的連接用完的時候,C3P01下獲得的新的連接數 --> <property name="acquireIncrement"> <value>${dataSource.c3p0.acquire_increment}</value> </property> </bean> </beans>

<![CDATA[ dataSource.driverClassName=com.mysql.jdbc.Driver dataSource.username=favccxx dataSource.password=favboy dataSource.url=jdbc:mysql://10.0.0.12:3305/favsecurity?useUnicode=true&characterEncoding=utf⑻ # For c3p0 connect pool dataSource.c3p0.max_size=200 dataSource.c3p0.min_size=10 dataSource.c3p0.max_idle_time=300 #dataSource.c3p0.checkout_timeout=30000 dataSource.c3p0.max_statements=0 dataSource.c3p0.idle_test_period=120 dataSource.c3p0.acquire_increment=5 ]]>

4. 工程運行效果以下

wKioL1TBuF2icSKJAAEI0IdviBU699.jpg

wKiom1TBt4bRzodmAAGcgxLBT3Q508.jpg




生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 日韩综合网站 | 欧美大陆日韩 | 2018精品国产一区二区 | 欧美在线视频观看 | 日本香蕉一区二区在线观看 | 国产精品不卡片视频免费观看 | 日本护士xxx人 | 国产真实女人一级毛片 | 久草国产精品 | 欧美一级毛片一级 | 日本一区二区三区四区无限 | 亚洲理论欧美理论在线观看 | 娇小老少配xxxxx性视频 | 好大好爽好舒服 | 免费网站看v片在线观看 | 亚洲精品美女久久久aaa | 日本欧美一区二区三区高清 | 国产精品女上位在线观看 | 一区二区三区高清不卡 | 欧美专区一区 | 免费观看成人www精品视频在线 | 国产尤物视频在线 | 国产亚洲精品久久久久久久久激情 | 亚洲黄色在线观看 | 成人午夜在线观看 | 亚洲欧美一区二区三区国产精品 | 精品国产福利 | 欧美三级午夜伦理片 | 69视频在线观看高清免费 | 最近免费中文在线视频 | 欧美高清xxxx性| 国产欧美一区二区三区精品 | 亚洲最新色图 | 精品国产日韩久久亚洲 | 精品久久久久久久 | 免看一级a毛片一片成人不卡 | 精品免费国产一区二区三区 | 国产在线综合网 | 精品国产中文一级毛片在线看 | 波多野结衣中出在线 | 亚洲欧美日韩中文字幕网址 |