千家信息网

如何在原有的框架中集成shiro

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,今天就跟大家聊聊有关如何在原有的框架中集成shiro,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。今天的任务是在原有的框架中集成shiro1.
千家信息网最后更新 2026年11月02日如何在原有的框架中集成shiro

今天就跟大家聊聊有关如何在原有的框架中集成shiro,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

今天的任务是在原有的框架中集成shiro


1.shiro的认识
> 权限框架(提供的易用的API,功能强大)

1.1 和Spring security区别

框架 | shiro | Spring security
---|------| ---
易用性 | √ | X
粒度 | 粗 | 细(强大)

1.2 shiro的四大基石
> 身份验证、授权、密码学和会话管理
> securityManager:核心对象 realm:获取数据接口


2.shiro的核心api
2.1 操作之前,先得到securityManager对象
```

 //一.创建我们自己的RealmMyRealm myRealm = new MyRealm();//二.搞一个核心对象:DefaultSecurityManager securityManager = new DefaultSecurityManager();securityManager.setRealm(myRealm);//三.把securityManager放到上下文中SecurityUtils.setSecurityManager(securityManager);


```

## 2.2 我们使用过的方法

```

//1.拿到当前用户Subject currentUser = SecurityUtils.getSubject();//2.判断是否登录currentUser.isAuthenticated();//3.登录(需要令牌的)/**    UnknownAccountException:用户名不存在    IncorrectCredentialsException:密码错误    AuthenticationException:其它错误*/ UsernamePasswordToken token = new UsernamePasswordToken("admin", "123456"); currentUser.login(token); //4.判断是否是这个角色/权限currentUser.hasRole("角色名")currentUser.isPermitted("权限名")


```
# 3.密码加密功能
```

/** * String algorithmName, Object source, Object salt, int hashIterations) * 第一个参数algorithmName:加密算法名称 * 第二个参数source:加密原密码 * 第三个参数salt:盐值 * 第四个参数hashIterations:加密次数 */SimpleHash hash = new SimpleHash("MD5","123456","itsource",10);System.out.println(hash.toHex());```# 4.自定义Realm> 继承AuthorizingRealm>> 实现两个方法:doGetAuthorizationInfo(授权)  /doGetAuthenticationInfo(登录认证)```//身份认证@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {    //1.拿用户名与密码    UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;    String username = token.getUsername();    //2.根据用户名拿对应的密码    String password = getByName(username);    if(password==null){        return null; //返回空代表用户名有问题    }    //返回认证信息    //准备盐值    ByteSource salt = ByteSource.Util.bytes("asdf");    //密码是shiro自己进行判断    SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username,password,salt,getName());    return authenticationInfo;}//授权@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {    //拿到用户名  Principal:主体(用户对象/用户名)    String username = (String)principalCollection.getPrimaryPrincipal();    //拿到角色    Set roles = findRolesBy(username);    //拿到权限    Set permis = findPermsBy(username);    //把角色权限交给用户    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();    authorizationInfo.setRoles(roles);    authorizationInfo.setStringPermissions(permis);    return authorizationInfo;}


> 注意:如果我们的密码加密,应该怎么判断(匹配器)

//一.创建我们自己的RealmMyRealm myRealm = new MyRealm();//创建一个凭证匹配器(无法设置盐值)HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();//  使用MD5的方式比较密码matcher.setHashAlgorithmName("md5");// 设置编码的迭代次数matcher.setHashIterations(10);//设置凭证匹配器(加密方式匹配)myRealm.setCredentialsMatcher(matcher);


```


# 5.集成Spring
> 去找:shiro-root-1.4.0-RC2\samples\spring

## 5.1 导包
```

     org.apache.shiro    shiro-all    1.4.0    pom        org.apache.shiro    shiro-spring    1.4.0  

## 5.2 web.xml> 这个过滤器是一个代码(只关注它的名称)

     shiroFilter    org.springframework.web.filter.DelegatingFilterProxy          targetFilterLifecycle      true            shiroFilter    /*  

## 5.3 application-shiro.xml

> 在咱们的application引入

``

> 是从案例中拷备过来,进行了相应的修改```

                                                                                                                                                                                                                                                                                                

```

5.4 获取Map过滤

> 注意,返回的Map必需是有序的(LinkedHashMap)```

public class FilterChainDefinitionMapFactory {    /**     * 后面这个值会从数据库中来拿     * /s/login.jsp = anon     * /login = anon     * /s/permission.jsp = perms[user:index]     * /depts/index = perms[depts:index]     * /** = authc     */    public Map createFilterChainDefinitionMap(){        //注:LinkedHashMap是有序的        Map filterChainDefinitionMap = new LinkedHashMap<>();        filterChainDefinitionMap.put("/s/login.jsp", "anon");        filterChainDefinitionMap.put("/login", "anon");        filterChainDefinitionMap.put("/s/permission.jsp", "perms[user:index]");        filterChainDefinitionMap.put("/depts/index", "perms[depts:index]");        filterChainDefinitionMap.put("/**", "authc");        return filterChainDefinitionMap;    }}

今日重点 : 对securityManage 对象的获取,在权限认证步骤中需要的信息传递(用户,角色,权限)

细节 : 对于在设置权限列表时需要注意顺序,-----放行在前->权限在后->最后同一拦截 /** = authc

看完上述内容,你们对如何在原有的框架中集成shiro有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

用户 密码 权限 用户名 加密 框架 对象 角色 参数 认证 中集 内容 核心 登录 强大 有序 信息 凭证 功能 名称 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 喔噻互联网科技有限公司 湖北九亚互联网络科技有限公司 互联网科技工作委员会 互联网科技保险 网络科技有限互联网 互联网科技公司官网 壕鑫互联网络科技 互联网高科技企业 互联网金融和金融科技 人人互联网科技是培训 互联网科技大会案例 茂名市互联网科技产业园 武汉健翔互联网络科技有限公司 互联网科技与大数据专业 半导体行业属于互联网高科技类吗 互联网科技包装股 九思连强互联网电子科技有限公司 张家口科技产业互联网平台 恒生互联网科技趋势 科技发展第三阶段互联网时代 互联网和科技技术 广州互联网科技公司 互联网科技公司的市场部门 呼和浩特新华互联网科技学院 黑科技 体验场景 互联网大会 互联网科技包装股 广州梦谷互联网科技有限公司领导 云鲸互联网络科技有限公司 唐山双旺合互联网科技产业园 互联网科技创业计划的风险分析
0