分享

Shiro系列之Shiro+Mysql实现用户认证(Authentication)

 飞鹰飞龙飞天 2017-01-05

网上大多数介绍Apache Shiro的资料都是使用ini文件的简单配置为例,很少用讲到如何配合数据库来实现用户认证的。我也是刚刚开始接触Shiro,在这里介绍一个入门级别的Shiro+MySQL的配置方法,这个方法仅仅是个开始,并没有和Web,Spring,Mybatis等框架进行整合,后续我还会继续和大家分享我的学习过程及心得。

 

now we can start the things that we really care about.

 

数据库中创建一个用户表,字段可以很简单。

  1. CREATE TABLE `sec_user` (  
  2.   `user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,  
  3.   `user_name` varchar(64) COLLATE utf8_bin DEFAULT NULL,  
  4.   `password` varchar(128) COLLATE utf8_bin DEFAULT NULL,  
  5.   `created_time` datetime DEFAULT NULL,  
  6.   `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,  
  7.   PRIMARY KEY (`user_id`)  
  8. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_bin  

 在表中插入一条记录,用户名:chris.mao.zb@163.com,密码:cmao

 

在resources目录下创建一个ini文件,配置Shiro(后续文件会将此文件内容移至XML文件中)。在这个配置文件中我们要设置数据源,以及用户认证时使用数据库查询语句。这里用到了Shiro中自带的JdbcRealm类。

[main]  
dataSource=org.springframework.jdbc.datasource.DriverManagerDataSource
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://127.0.0.1:3306/YOUR_DATABASE_NAME
dataSource.username=YOUR_USERNAME
dataSource.password=YOUR_PASSWORD

jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.permissionsLookupEnabled = true  
jdbcRealm.dataSource=$dataSource
jdbcRealm.authenticationQuery = SELECT password FROM sec_user WHERE user_name = ?  

securityManager.realms=$jdbcRealm

关于用户认证的查询语句,我在这里多说两句,小伙伴们不要嫌我啰嗦。我们只需要以用户名为查询条件,查询出密码字段即可,如果您在select后面使用了星号(*)或是查询字段多于一个,都无法通过用户认证 。

配置文件写好后,我们就可以动手写个测试方法,来验证是否可以实现用户认证功能了。

 

  1. package com.emerons.learning;  
  2.   
  3. import static org.junit.Assert.*;  
  4.   
  5. import org.apache.shiro.SecurityUtils;  
  6. import org.apache.shiro.authc.DisabledAccountException;  
  7. import org.apache.shiro.authc.ExcessiveAttemptsException;  
  8. import org.apache.shiro.authc.ExpiredCredentialsException;  
  9. import org.apache.shiro.authc.IncorrectCredentialsException;  
  10. import org.apache.shiro.authc.LockedAccountException;  
  11. import org.apache.shiro.authc.UnknownAccountException;  
  12. import org.apache.shiro.authc.UsernamePasswordToken;  
  13. import org.apache.shiro.config.IniSecurityManagerFactory;  
  14. import org.apache.shiro.mgt.SecurityManager;  
  15. import org.apache.shiro.subject.Subject;  
  16. import org.apache.shiro.util.Factory;  
  17. import org.junit.After;  
  18. import org.junit.Before;  
  19. import org.junit.Test;  
  20.   
  21. public class JdbcRealmTest {  
  22.   
  23.     @Before  
  24.     public void setUp() throws Exception {  
  25.     }  
  26.   
  27.     @After  
  28.     public void tearDown() throws Exception {  
  29.     }  
  30.   
  31.     @Test  
  32.     public void test() {  
  33.         // 1.获取SecurityManager工厂,此处使用ini配置文件初始化SecurityManager  
  34.         Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-jdbc-realm.ini");  
  35.         // 2.获取SecurityManager实例,并绑定到SecurityUtils  
  36.         SecurityManager sm = factory.getInstance();  
  37.         SecurityUtils.setSecurityManager(sm);  
  38.   
  39.         // 3.得到Subject  
  40.         Subject subject = SecurityUtils.getSubject();  
  41.         // 4.创建用户登录凭证  
  42.         UsernamePasswordToken token = new UsernamePasswordToken("chris.mao@emerson.com", "chrismao");  
  43.         // 5.登录,如果登录失败会抛出不同的异常,根据异常输出失败原因  
  44.         try {  
  45.             subject.login(token);  
  46.             // 6.判断是否成功登录  
  47.             assertEquals(true, subject.isAuthenticated());  
  48.             System.out.println("登录成功!!");  
  49.             // 7.注销用户  
  50.             subject.logout();  
  51.         } catch (IncorrectCredentialsException e) {  
  52.             System.out.println("登录密码错误. Password for account " + token.getPrincipal() + " was incorrect.");  
  53.         } catch (ExcessiveAttemptsException e) {  
  54.             System.out.println("登录失败次数过多");  
  55.         } catch (LockedAccountException e) {  
  56.             System.out.println("账号已被锁定. The account for username " + token.getPrincipal() + " was locked.");  
  57.         } catch (DisabledAccountException e) {  
  58.             System.out.println("账号已被禁用. The account for username " + token.getPrincipal() + " was disabled.");  
  59.         } catch (ExpiredCredentialsException e) {  
  60.             System.out.println("账号已过期. the account for username " + token.getPrincipal() + "  was expired.");  
  61.         } catch (UnknownAccountException e) {  
  62.             System.out.println("账号不存在. There is no user with username of " + token.getPrincipal());  
  63.         }  
  64.     }  
  65.   
  66. }  

 

运行测试代码,得到如下输出:

INFO : org.springframework.jdbc.datasource.DriverManagerDataSource - Loaded JDBC driver: com.mysql.jdbc.Driver
INFO : org.apache.shiro.realm.AuthorizingRealm - No cache or cacheManager properties have been set.  Authorization cache cannot be obtained.
INFO : org.apache.shiro.config.IniSecurityManagerFactory - Realms have been explicitly set on the SecurityManager instance - auto-setting of realms will not occur.
INFO : org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler...
登录成功!!
INFO : org.apache.shiro.realm.AuthorizingRealm - No cache or cacheManager properties have been set.  Authorization cache cannot be obtained.

 我们从日志上可以看到,程序加载了Jdbc驱动,明确指定了realm,这说明我们的Shiro配置文件加载成功。最后看到输出了“登录成功”说明这个认证功能已经实现了。大家也可以试着修改测试代码用的用户名或是密码,可以在控制台看到类似下面的输出,说明也可以抛出正确的异常。

INFO : org.springframework.jdbc.datasource.DriverManagerDataSource - Loaded JDBC driver: com.mysql.jdbc.Driver
INFO : org.apache.shiro.realm.AuthorizingRealm - No cache or cacheManager properties have been set.  Authorization cache cannot be obtained.
INFO : org.apache.shiro.config.IniSecurityManagerFactory - Realms have been explicitly set on the SecurityManager instance - auto-setting of realms will not occur.
登录密码错误. Password for account chris.mao@emerson.com was incorrect.

 

至此,使用Shiro + Mysql实现用户认证(Authentication)的功能已经完成。大家可以在这个基础上,完善实现角色授权(Authroization),操作允可(Permission)等功能,参见《Shiro系列之Shiro+Mysql实现用户授权(Authorization) 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多