# ============================================================================= # Quickstart INI Realm configuration # # For those that might not understand the references in this file, the # definitions are all based on the classic Mel Brooks' film "Spaceballs". ;) # =============================================================================
# ----------------------------------------------------------------------------- # Users and their assigned roles # # Each line conforms to the format defined in the # org.apache.shiro.realm.text.TextConfigurationRealm#setUserDefinitions JavaDoc # ----------------------------------------------------------------------------- [users] # user 'root' with password 'secret' and the 'admin' role root = secret, admin # user 'guest' with the password 'guest' and the 'guest' role guest = guest, guest # user 'presidentskroob' with password '12345' ("That's the same combination on # my luggage!!!" ;)), and role 'president' presidentskroob = 12345, president # user 'darkhelmet' with password 'ludicrousspeed' and roles 'darklord' and 'schwartz' darkhelmet = ludicrousspeed, darklord, schwartz # user 'lonestarr' with password 'vespa' and roles 'goodguy' and 'schwartz' lonestarr = vespa, goodguy, schwartz
# ----------------------------------------------------------------------------- # Roles with assigned permissions # # Each line conforms to the format defined in the # org.apache.shiro.realm.text.TextConfigurationRealm#setRoleDefinitions JavaDoc # ----------------------------------------------------------------------------- [roles] # 'admin' role has all permissions, indicated by the wildcard '*' admin = * # The 'schwartz' role can do anything (*) with any lightsaber: schwartz = lightsaber:* # The 'goodguy' role is allowed to 'drive' (action) the winnebago (type) with # license plate 'eagle5' (instance specific id) goodguy = winnebago:drive:eagle5
// The easiest way to create a Shiro SecurityManager with configured // realms, users, roles and permissions is to use the simple INI config. // We'll do that by using a factory that can ingest a .ini file and // return a SecurityManager instance:
// Use the shiro.ini file at the root of the classpath // (file: and url: prefixes load from files and urls respectively): IniSecurityManagerFactoryfactory=newIniSecurityManagerFactory("classpath:shiro.ini"); SecurityManagersecurityManager= factory.getInstance();
// for this simple example quickstart, make the SecurityManager // accessible as a JVM singleton. Most applications wouldn't do this // and instead rely on their container configuration or web.xml for // webapps. That is outside the scope of this simple quickstart, so // we'll just do the bare minimum so you can continue to get a feel // for things. SecurityUtils.setSecurityManager(securityManager);
// Now that a simple Shiro environment is set up, let's see what you can do:
// get the currently executing user: SubjectcurrentUser= SecurityUtils.getSubject();
// Do some stuff with a Session (no need for a web or EJB container!!!) Sessionsession= currentUser.getSession(); session.setAttribute("someKey", "aValue"); Stringvalue= (String) session.getAttribute("someKey"); if (value.equals("aValue")) { log.info("检测到正确的值! [" + value + "]"); }
// let's login the current user so we can check against roles and permissions: if (!currentUser.isAuthenticated()) { UsernamePasswordTokentoken=newUsernamePasswordToken("lonestarr", "vespa"); token.setRememberMe(true); try { currentUser.login(token); } catch (UnknownAccountException uae) { log.info("没有用户名为 " + token.getPrincipal()); } catch (IncorrectCredentialsException ice) { log.info("密码对于账户 " + token.getPrincipal() + " 不正确!"); } catch (LockedAccountException lae) { log.info("账户 " + token.getPrincipal() + " 被锁定 " + "请联系管理员解锁."); } // ... catch more exceptions here (maybe custom ones specific to your application? catch (AuthenticationException ae) { //unexpected condition? error? } }
//say who they are: //print their identifying principal (in this case, a username): log.info("User [" + currentUser.getPrincipal() + "] 登录成功.");
//test a role: if (currentUser.hasRole("schwartz")) { log.info("拥有schwartz角色!"); } else { log.info("没有schwartz角色."); }
//test a typed permission (not instance-level) if (currentUser.isPermitted("lightsaber:wield")) { log.info("拥有lightsaber:wield权限 ."); } else { log.info("没有lightsaber:wield权限"); }