shiro认证:连接数据库操作doGetAuthenticationInfo

shiro权限框架课程目标精讲篇(1)
shiro初识,全面介绍架构精讲篇
shiro三大核心组件介绍精讲篇
shiro配置jar包导入依赖精讲篇
shiro认证权限流程测试精讲篇
shiro权限授权测试精讲篇
shiro权限模型构建数据库表精讲篇
shiro权限框架自定义realm认证测试精讲篇
shiro散列算法密码加盐测试精讲篇
shiro权限管理自定义realm授权测试精讲篇
spring集成shiro不进入自定义realm是为什么?
shiro认证:连接数据库操作doGetAuthenticationInfo
shiro授权:链接数据库doGetAuthorizationInfo
shiro框架:缓存器
Ehcache配置时出错- Another unnamed CacheManager already exists in the same VM. Please provide unique name
shiro框架:缓存器Ehcache的详细配置流程
shiro框架:sessionManager设置数据会话的存储时间,账号密码到时间清空
shiro框架集成验证码验证配置流程
shiro框架:记住我remberMe自动登入
5.13.1 需求
修改realm的doGetAuthenticationInfo,从数据库查询用户信息,realm返回的用户信息中包括 (md5加密后的串和salt),实现让shiro进行散列串的校验。

1.1.1 修改doGetAuthenticationInfo从数据库查询用户信息

1、将SysService注入到realm中。

shiro认证:连接数据库操作doGetAuthenticationInfo_第1张图片
image.png

//realm的认证方法,从数据库查询用户信息

  @Override

  **protected** AuthenticationInfo doGetAuthenticationInfo(

 AuthenticationToken token) **throws** AuthenticationException {

  // token是用户输入的用户名和密码

  // 第一步从token中取出用户名

 String userCode = (String) token.getPrincipal();

  // 第二步:根据用户输入的userCode从数据库查询

 SysUser sysUser = **null**;

  **try** {

 sysUser = sysService.findSysUserByUserCode(userCode);

 } **catch** (Exception e1) {

  // **TODO** Auto-generated catch block

 e1.printStackTrace();

 }

  // 如果查询不到返回null

  **if**(sysUser==**null**){//

  **return**  **null**;

 }

  // 从数据库查询到密码

 String password = sysUser.getPassword();

  //盐

 String salt = sysUser.getSalt();

  // 如果查询到返回认证信息AuthenticationInfo

  //activeUser就是用户身份信息

 ActiveUser activeUser = **new** ActiveUser();

 activeUser.setUserid(sysUser.getId());

 activeUser.setUsercode(sysUser.getUsercode());

 activeUser.setUsername(sysUser.getUsername());

  //..

  //根据用户id取出菜单

 List menus = **null**;

  **try** {

  //通过service取出菜单

 menus = sysService.findMenuListByUserId(sysUser.getId());

 } **catch** (Exception e) {

  // **TODO** Auto-generated catch block

 e.printStackTrace();

 }

  //将用户菜单  设置到activeUser

 activeUser.setMenus(menus);

  //将activeUser设置simpleAuthenticationInfo

 SimpleAuthenticationInfo simpleAuthenticationInfo = **new** SimpleAuthenticationInfo(

 activeUser, password,ByteSource.Util.*bytes*(salt), **this**.getName());

  **return** simpleAuthenticationInfo;

 }

设置 凭证匹配器,设置密码的散列次数,也可以自定义


        


    
    
    
    
    
    
 



自定义


    
        
        
        
        
    

DataCatalogCredentialsMatcher


import java.util.concurrent.atomic.AtomicInteger;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;

public class DataCatalogCredentialsMatcher extends HashedCredentialsMatcher {
    private Cache passwordRetryCache;

    public DataCatalogCredentialsMatcher(CacheManager cacheManager) {
        passwordRetryCache = cacheManager.getCache("passwordRetryCache");
    }

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        String username = (String) token.getPrincipal();
        AtomicInteger retryCount = passwordRetryCache.get(username);
        if (retryCount == null) {
            retryCount = new AtomicInteger();
            passwordRetryCache.put(username, retryCount);
        }
        if (retryCount.incrementAndGet() > 5) {
            throw new ExcessiveAttemptsException();
        }
        boolean matchs = super.doCredentialsMatch(token, info);
        if (matchs) {
            passwordRetryCache.remove(username);
        }
        return matchs;
    }

    public Cache getPasswordRetryCache() {
        return passwordRetryCache;
    }

    public void setPasswordRetryCache(Cache passwordRetryCache) {
        this.passwordRetryCache = passwordRetryCache;
    }

你可能感兴趣的:(shiro认证:连接数据库操作doGetAuthenticationInfo)