Shiro——Realm数据交互的实现
来源:互联网 发布:臣妾做不到网络歌曲 编辑:程序博客网 时间:2024/05/01 10:29
自定义一个Realm,自定义Realm可以实现Realm接口,当然,根据自己的业务需求,也可以继承不同的父类,例如,我们需要实现认证功能,那么就可以继承AuthenticatingRealm,实现该接口的getAuthenticationInfo()方法。代码如下:
public class MyRealm extends AuthenticatingRealm { /** * 1.doGetAuthenticationInfo 获取认证信息,如果数据库中没有数据,返回null,如果得到正确的用户名和密码,返回指定类型对象 * 2.AuthenticationInfo是一个抽象接口,此处我们使用SimpleAuthenticationInfo,封装正确的用户名和密码 * @param authenticationToken 这是我们需要进行认证的令牌 * @return * @throws AuthenticationException */ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { SimpleAuthenticationInfo authenticationInfo=null; //1.将token转化为UsernamePasswordToken UsernamePasswordToken token= (UsernamePasswordToken) authenticationToken; //2.获取用户名 String username=token.getUsername(); //3.查询数据库中是否具有指定用户名和密码的用户 try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/myblog?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=UTC","root",""); PreparedStatement ps=connection.prepareStatement("SELECT * FROM user WHERE login_id=?"); ps.setString(1,username); ResultSet rs=ps.executeQuery(); if (rs.next()){ Object principal=username; Object credential=rs.getObject(2); String realmName=this.getName(); authenticationInfo=new SimpleAuthenticationInfo(principal,credential,realmName); }else { throw new AuthenticationException(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } //4.如果查询到了,那么封装查询结果,并返回结果 //5.如果没有查询到,则抛出异常 return authenticationInfo; }
认证功能实现的原理
认证的过程中核心的过程是前台输入数据与后台数据库查询结果之间的比对,AuthenticatingRealm中有一个关键的成员属性,也就是
private CredentialsMatcher credentialsMatcher;
CredentialsMatcher 是一个接口,该接口中定义了一个方法:
boolean doCredentialsMatch(AuthenticationToken var1, AuthenticationInfo var2)
本案例中使用的是SimpleAuthenticationInfo,其所使用到的比对方法实现是SimpleCredentialsMatcher的:
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) { Object tokenCredentials = this.getCredentials(token); Object accountCredentials = this.getCredentials(info); return this.equals(tokenCredentials, accountCredentials); }
这里的token封装了前台传入的用户名密码,info则封装了数据库查询的结果。到这里,从前端页面到数据库的交互就完成了。
阅读全文
0 0
- Shiro——Realm数据交互的实现
- Shiro Review——自定义Realm实现认证
- Shiro Review——自定义Realm实现授权
- shiro realm实现
- shiro自定义Realm实现
- Shiro——多个Realm的配置
- Shiro的Realm
- 使用shiro进行登录校验;自定义realm的实现
- Shiro 自定义realm授权与认证的实现
- Shiro 的身份认证-Realm
- @Autowire与@Resource的区别——Shiro框架中定义Realm遇到的问题
- Shiro学习笔记(2)——身份验证之Realm
- Shiro 学习笔记(3)—— 自定义 Realm
- Realm及相关对象——《跟我学Shiro》
- Shiro 学习笔记(2)—— Jdbc Realm
- Realm及相关对象——《跟我学Shiro》
- Shiro、Realm
- Shiro-Realm
- win7系统下IE主页被360/2345篡改了怎么恢复【图文教程】
- c语言的头文件
- 线上服务CPU100%问题快速定位实战(转载公众号:架构师之路)
- NSGAii代码分析
- Linux shell基础知识
- Shiro——Realm数据交互的实现
- 面试的角度诠释Java工程师(一)
- 整理的关于vim的快捷键大全
- 浅析三种特殊进程:孤儿进程,僵尸进程和守护进程.
- 一些比较好的国外IT网站
- nginx+tomcat 报 504 gateway timeout
- Web监听器导图详解
- HDU4185 Oil Skimming(二分图匹配,匈牙利算法)
- pip更新所有需要更新的包