网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

shiro与spring集成的示例分析

这篇文章将为大家详细讲解有关shiro与spring集成的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联-专业网站定制、快速模板网站建设、高性价比贵港网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式贵港网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖贵港地区。费用合理售后完善,10多年实体公司更值得信赖。

shiro的四大基石

  • 身份验证(登录)Authentication:身份认证 / 登录,验证用户是不是拥有相应的身份;

  • 授权(权限)Authorization:验证某个已登录的用户是否拥有某个权限

  • 密码学(密码加密) Cryptography:加密,保护数据的安全性,如密码加密存储到数据库,而不是明文存储;

  • 会话管理 Session Management:用户登录后就是一次会话,在没有退出之前,它的所有信息都在会话中;

导包



  
  
    org.apache.shiro
    shiro-core
    1.4.0
  
  
  
    commons-logging
    commons-logging
    1.2
  
  
  
    junit
    junit
    4.9
  

ini文件(再创建shiro.ini文件)文件中有咱们的用户角色权限

# ini文件里面放的就是咱们的用户,角色,权限,资源

# -----------------------------------------------------------------------------
# users:用户
#  root:用户名 123456:密码 admin:角色
# -----------------------------------------------------------------------------
[users]
root = 123456, admin
guest = guest, it

# -----------------------------------------------------------------------------
# roles:角色
#  admin = * :admin这个用户拥有所有权限
#  it = employee:* :it这个角色拥有员工的所有权限
#  hr = employee:save :hr这个角色拥有员工添加权限
# -----------------------------------------------------------------------------
[roles]
admin = *
it = employee:*
hr = employee:save

功能测试(一定要有测试包org.junit.Test才能测试)

主要测试登录,权限认证

@Test
public void testHello() throws Exception{
  //①.拿到权限管理对象
  /**
   * 读取了shiro.ini的文件(隐藏了realm) -> 隐藏了iniRealm
   * SecurityManager:权限管理器,shiro的所有功能都放在里面
   */
  Factory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
  SecurityManager securityManager = factory.getInstance();
  //②.相当于把SecurityManager放到了当前上下文
  /**
   * 可以让我们在当前系统的任何位置都可以拿到SecurityManager对象
   */
  SecurityUtils.setSecurityManager(securityManager);
  //③.拿到当前用户(没有登录就是游客)
  Subject currentUser = SecurityUtils.getSubject();
  System.out.println("用户是否登录:"+currentUser.isAuthenticated());

  //④.如果没有登录,让他进行登录
  if(!currentUser.isAuthenticated()){
    //ctrl+alt+t :包含代码
    try {
      //4.1 准备令牌(对象) 用户名密码令牌
      UsernamePasswordToken token = new UsernamePasswordToken("guest","guest");
      //4.2 进行登录功能
      currentUser.login(token);
    } catch (UnknownAccountException e) {
      //Unknown(未知)Account(账号)Exception:用户名不存在
      e.printStackTrace();
      System.out.println("哥,你是傻子嘛?");
    }catch (IncorrectCredentialsException e){
      //Incorrect(不正确)Credentials(凭证)Exception:密码错误
      e.printStackTrace();
      System.out.println("哥,密码错误了?");
    }catch (AuthenticationException e){
      //AuthenticationException:登录中最大的那个异常
      e.printStackTrace();
      System.out.println("发生了一个神秘的错误!!!");
    }
  }
  System.out.println("用户是否登录:"+currentUser.isAuthenticated());

  System.out.println("是否是管理员角色:"+currentUser.hasRole("admin"));
  System.out.println("是否是IT角色:"+currentUser.hasRole("it"));

  System.out.println("是否可以操作employee:save权限:"+currentUser.isPermitted("employee:save"));
  System.out.println("是否可以操作employee:index权限:"+currentUser.isPermitted("employee:index"));
  System.out.println("是否可以操作department:index权限:"+currentUser.isPermitted("department:index"));

  //⑤.还可以登出(注销)
  currentUser.logout();
  System.out.println("用户是否登录:"+currentUser.isAuthenticated());
}

关于“shiro与spring集成的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


分享名称:shiro与spring集成的示例分析
文章地址:http://bjjierui.cn/article/pddpoh.html

其他资讯