教程集 www.jiaochengji.com
教程集 >  脚本编程  >  java  >  正文 spring secruity 数据库方式配置用户登录

spring secruity 数据库方式配置用户登录

发布时间:2016-11-27   编辑:jiaochengji.com
教程集为您提供spring secruity 数据库方式配置用户登录等资源,欢迎您收藏本站,我们将为您提供最新的spring secruity 数据库方式配置用户登录资源
文章给大家转一篇关于spring secruity 数据库方式配置用户登录,希望此文章对大家学习spring secruity 会有所帮助。

前几天学习了,直接在XML中配置用户密码,利用spring security 登录的例子。这种方式适合做演示,真实的项目在大多数情况下都会用数据库或者LDAP来做用户管理. 所以今天继续学习利用数据库方式配置用户登录, 在前面例子的基础上做,最主要的改变如下:

增加数据库相关的jar包
所有用到的jar包如下,有可能有多余的.

 程序代码

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8074')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8074>

antlr-2.7.7.jar
aopalliance.jar
commons-logging-1.1.jar
jstl-1.1.2.jar
mysql-connector-java-3.1.12-bin.jar
spring-aop-3.2.4.RELEASE.jar
spring-aspects-3.2.4.RELEASE.jar
spring-beans-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-context-support-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-e­xpression-3.2.4.RELEASE.jar
spring-jdbc-3.2.4.RELEASE.jar
spring-security-config-3.1.4.RELEASE.jar
spring-security-core-3.1.4.RELEASE.jar
spring-security-web-3.1.4.RELEASE.jar
spring-tx-3.2.4.RELEASE.jar
spring-web-3.2.4.RELEASE.jar
spring-webmvc-3.2.4.RELEASE.jar
standard-1.1.2.jar

 

新建数据库,并插入测试数据
数据库脚本如下

 程序代码

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4919')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4919>

Create TABLE `users` (
  `USER_ID` INT(10) UNSIGNED NOT NULL,
  `USERNAME` VARCHAR(45) NOT NULL,
  `PASSWORD` VARCHAR(45) NOT NULL,
  `ENABLED` tinyint(1) NOT NULL,
  PRIMARY KEY (`USER_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Create TABLE `user_roles` (
  `USER_ROLE_ID` INT(10) UNSIGNED NOT NULL,
  `USER_ID` INT(10) UNSIGNED NOT NULL,
  `AUTHORITY` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`USER_ROLE_ID`),
  KEY `FK_user_roles` (`USER_ID`),
  CONSTRAINT `FK_user_roles` FOREIGN KEY (`USER_ID`) REFERENCES `users` (`USER_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Insert INTO users (USER_ID, USERNAME,PASSWORD, ENABLED)
VALUES (100, 'yihaomen', '123456', TRUE);

Insert INTO user_roles (USER_ROLE_ID, USER_ID,AUTHORITY)
VALUES (1, 100, 'ROLE_USER');

 

这样在数据库security 中就建立了 users 表以及 user_roles表,并插入了记录.

配置spring securtiy 的数据库认证方式


 程序代码

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7879')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7879>

<authentication-manager>
        <authentication-provider>
            <jdbc-user-service data-source-ref="dataSource"               
                users-by-username-query="
                    select username,password, enabled
                    from users where USERNAME=?"                
                authorities-by-username-query="
                    select u.username, ur.authority from users u, user_roles ur
                    where u.user_id = ur.user_id and u.username =?  "                    
            />
        </authentication-provider>
    </authentication-manager>

 

启动程序,运行结果如下,


程序代码下载 ,去掉了jar包,自己添加,完整实例下载:http://file.jiaochengji.com/download/2013/12/a35.zip

您可能感兴趣的文章:
spring secruity 数据库方式配置用户登录
Spring 学习笔记--强烈推荐
快速部署 Spring PetClinic 到函数计算平台
基于spring的数据库读写分离分析及实例教程
找不到 org.junit.jupiter.api.Test报错
java中spring集成mybatis的使用方法
Linux VSFTP 配置详解
golang微服务框架对比_微服务全流程各组件详细对比分析
vsftpd+pam创建虚拟用户登录FTP服务器
Codeigniter怎么整合Tank Auth权限类库?方法教程

[关闭]
~ ~