Shiro ini配置

时间:2021-11-28 00:57:39

Shiro.ini配置:

  ini配置文件类似Java中的properties(key = value),不过提供了key/value分类的特性,每个部分的key不重复即可

  在eclipse中设置打开方式:

Shiro ini配置

  [main]:

    提供了对根对象Security及其依赖对象的配置

securityManager=org.apache.shiro.mgt.DefaultSecurityManager  

  [users]:

    提供了对用户/密码及其角色的配置(username=password, role1, role2...)

  [roles]:

    提供了用户及其权限之间关系的配置(role1=permission1, permission2...)

  [urls]:

    用于web,提供了对web url拦截相关的配置(url=filter1[param], filter2...)

Test:

shiro-first.ini

  [users]
  draco = 615
  harry = 130
@Test
public void testLoginAndLogout(){ //创建sessionFactory
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-first.ini"); //创建securityManager
SecurityManager securityManager = factory.getInstance(); //将securityManager配置在当前运行环境中
SecurityUtils.setSecurityManager(securityManager); //获取subject
Subject currentUser = SecurityUtils.getSubject(); //创建用户令牌
UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa"); //直接登录
try {
currentUser.login(token);
} catch (UnknownAccountException uae) {
log.info("用户名不存在 " + token.getPrincipal());
} catch (IncorrectCredentialsException ice) {
log.info("密码错误 " + token.getPrincipal() + " was incorrect!");
} catch (LockedAccountException lae) {
log.info("未知错误? ");
} boolean authenticated = currentUser.isAuthenticated();
System.out.println("是否登陆成功:"+ authenticated);
}
2017-10-14 19:43:36,739 INFO [com.roxy.shiro.quickstart.Quickstart] - 用户名不存在 lonestarr
是否登陆成功:false