List of usage examples for org.apache.shiro.config IniSecurityManagerFactory IniSecurityManagerFactory
public IniSecurityManagerFactory(String iniResourcePath)
From source file:Homework4ShiroCommandLineClient.java
/** * @param args//from w ww. j a va2 s. c o m */ public static void main(String[] args) { log.info("My First Apache Shiro Application"); Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); Subject currentUser = SecurityUtils.getSubject(); Session session = currentUser.getSession(); session.setAttribute("someKey", "aValue"); String value = (String) session.getAttribute("someKey"); if (value.equals("aValue")) { log.info("Retrieved the correct value! [" + value + "]"); } // let's login the current user so we can check against roles and permissions: if (!currentUser.isAuthenticated()) { UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa"); token.setRememberMe(true); try { currentUser.login(token); } catch (UnknownAccountException uae) { log.info("There is no user with username of " + token.getPrincipal()); } catch (IncorrectCredentialsException ice) { log.info("Password for account " + token.getPrincipal() + " was incorrect!"); } catch (LockedAccountException lae) { log.info("The account for username " + token.getPrincipal() + " is locked. " + "Please contact your administrator to unlock it."); } // ... catch more exceptions here (maybe custom ones specific to your application? catch (AuthenticationException ae) { //unexpected condition? error? } } log.info("User [" + currentUser.getPrincipal() + "] logged in successfully."); if (currentUser.hasRole("schwartz")) { log.info("May the Schwartz be with you!"); } else { log.info("Hello, mere mortal."); } if (currentUser.isPermitted("lightsaber:weild")) { log.info("You may use a lightsaber ring. Use it wisely."); } else { log.info("Sorry, lightsaber rings are for schwartz masters only."); } if (currentUser.isPermitted("winnebago:drive:eagle5")) { log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " + "Here are the keys - have fun!"); } else { log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!"); } currentUser.logout(); System.exit(0); }
From source file:Tutorial.java
public static void main(String[] args) { log.info(//from www.ja va2 s . c o m "\n\n\n\t\t\t**************************************************\n\t\t\t\tMy First Apache Shiro Application\n\t\t\t**************************************************\n"); Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); //Factory<SecurityManager> factory = new IniSecurityManagerFactory("file:src/main/webapp/WEB-INF/shiro.ini"); SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); // get the currently executing user: Subject currentUser = SecurityUtils.getSubject(); // Do some stuff with a Session (no need for a web or EJB container!!!) Session session = currentUser.getSession(); session.setAttribute("someKey", "aValue"); String value = (String) session.getAttribute("someKey"); if (value.equals("aValue")) { log.info("Retrieved the correct value! [" + value + "]"); } // let's login the current user so we can check against roles and permissions: if (!currentUser.isAuthenticated()) { UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa"); token.setRememberMe(true); try { currentUser.login(token); } catch (UnknownAccountException uae) { log.info("There is no user with username of " + token.getPrincipal()); } catch (IncorrectCredentialsException ice) { log.info("Password for account " + token.getPrincipal() + " was incorrect!"); } catch (LockedAccountException lae) { log.info("The account for username " + token.getPrincipal() + " is locked. " + "Please contact your administrator to unlock it."); } // ... 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() + "] logged in successfully."); //test a role: if (currentUser.hasRole("schwartz")) { log.info("May the Schwartz be with you!"); } else { log.info("Hello, mere mortal."); } //test a typed permission (not instance-level) if (currentUser.isPermitted("lightsaber:weild")) { log.info("You may use a lightsaber ring. Use it wisely."); } else { log.info("Sorry, lightsaber rings are for schwartz masters only."); } //a (very powerful) Instance Level permission: if (currentUser.isPermitted("winnebago:drive:eagle5")) { log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " + "Here are the keys - have fun!"); } else { log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!"); } //all done - log out! currentUser.logout(); log.info("User Logged out successfully!!"); System.exit(0); }
From source file:Global.java
License:Open Source License
public static void initialize() { SampleRealm sampleRealm = new SampleRealm(); sampleRealm.ini();//from w w w.j av a2 s .c o m Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); DefaultSecurityManager securityManager = (DefaultSecurityManager) factory.getInstance(); //DefaultSecurityManager securityManager = new DefaultSecurityManager(); //securityManager.setRealm(sampleRealm); /*try { PropertyUtils.getNestedProperty(securityManager, "-1"); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); }*/ // Turn off session storage for better "stateless" management. // https://shiro.apache.org/session-management.html#SessionManagement-StatelessApplications%2528Sessionless%2529 DefaultSubjectDAO subjectDAO = (DefaultSubjectDAO) securityManager.getSubjectDAO(); DefaultSessionStorageEvaluator sessionStorageEvaluator = (DefaultSessionStorageEvaluator) subjectDAO .getSessionStorageEvaluator(); sessionStorageEvaluator.setSessionStorageEnabled(false); //securityManager.setCacheManager(new PlayShiroCache()); //securityManager.setCacheManager(org.apache.shir/o org.apache.shiro.SecurityUtils.setSecurityManager(securityManager); }
From source file:UserSubjectTest.java
License:Open Source License
@Test public void UserSessionsRunTest() { //UserSessionFactory test = new UserSessionFactory(); //UserSubject user = test.createUserSession("user1"); //user.loginUser("simonangerer", "default"); Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); // session handling UserSubject user1 = new UserSubject("someSessionKey1", "someSessionValue1"); UserSubject user2 = new UserSubject("someSessionKey2", "someSessionValue2"); // login tests user1.loginUser("simonangerer", "default"); user2.loginUser("fabiansalzgeber", "123"); //get roles, permissions user1.checkPermission("adminstuff"); user1.checkRole("doctor"); user1.isLoggedIn();//from www . java 2 s . c o m //all done - log out! user1.logoutUser(); user2.logoutUser(); //empty session user1.isLoggedIn(); }
From source file:at.oculus.teamf.technical.accessrights.UserSessionFactory.java
License:Open Source License
private static void UserSessionFactory() { Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); log.info("Security management intialized"); }
From source file:blade.authenticator.shiro.ShiroAuthenticatorPre.java
License:Apache License
@Activate public void activate() { Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:userauth.ini"); SecurityUtils.setSecurityManager(factory.getInstance()); _log.info("activate"); }
From source file:cn.cjam.test.TestShiro.java
public static void main(String[] args) { log.info("My First Apache Shiro Application"); Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); // ??://from w w w.j ava 2 s . c om Subject currentUser = SecurityUtils.getSubject(); // ? Session Session session = currentUser.getSession(); session.setAttribute("someKey", "aValue"); String value = (String) session.getAttribute("someKey"); if (value.equals("aValue")) { log.info("Retrieved the correct value! [" + value + "]"); } // ??? if (!currentUser.isAuthenticated()) { UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa"); token.setRememberMe(true); try { currentUser.login(token); } catch (UnknownAccountException uae) { log.info("There is no user with username of " + token.getPrincipal()); } catch (IncorrectCredentialsException ice) { log.info("Password for account " + token.getPrincipal() + " was incorrect!"); } catch (LockedAccountException lae) { log.info("The account for username " + token.getPrincipal() + " is locked. " + "Please contact your administrator to unlock it."); } // ... ? catch (AuthenticationException ae) { //?? } } //?: //??? ( username): log.info("User [" + currentUser.getPrincipal() + "] logged in successfully."); //: if (currentUser.hasRole("schwartz")) { log.info("May the Schwartz be with you!"); } else { log.info("Hello, mere mortal."); } //?? (?instance-level) if (currentUser.isPermitted("lightsaber:weild")) { log.info("You may use a lightsaber ring. Use it wisely."); } else { log.info("Sorry, lightsaber rings are for schwartz masters only."); } //(?)??: if (currentUser.isPermitted("winnebago:drive:eagle5")) { log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " + "Here are the keys - have fun!"); } else { log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!"); } //? - t! currentUser.logout(); System.exit(0); }
From source file:cn.heweiming.webjars.learn.shiro.ShiroDemo02.java
public static void main(String[] args) { logger.info("My First Apache Shiro Application"); Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); // get the curretnly executing user; Subject currentUser = SecurityUtils.getSubject(); // Do some stuff with a Session (no need for a web or EJB container !!!) Session session = currentUser.getSession(); session.setAttribute("someKey", "aValue"); String value = (String) session.getAttribute("someKey"); if ("aValue".equals(value)) { logger.info("Retrieved the correct value! [" + value + "]"); }/*from w w w . j a v a 2s. co m*/ // let's login the current user so we can check against roles and // permissions: if (!currentUser.isAuthenticated()) { UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa"); token.setRememberMe(true); try { currentUser.login(token); } catch (UnknownAccountException uae) { logger.info("There is no user with username of " + token.getPrincipal()); } catch (IncorrectCredentialsException ice) { logger.info("Password for account " + token.getPrincipal() + " was incorrent!"); } catch (LockedAccountException lae) { logger.info("The account for username " + token.getPrincipal() + " is locked . " + " Please contact your administrator to unlock it."); } catch (AuthenticationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // say who they are: // print their identifying principal (int this case, a username): logger.info("User [" + currentUser.getPrincipal() + "]"); // test a role: if (currentUser.hasRole("schwartz")) { logger.info("May the Schwartz be with you!"); } else { logger.info("Hello, mere mortal."); } // test a typed permissions (not isstance-level) if (currentUser.isPermitted("lightsaber:weild")) { logger.info("You may use a lightsaber ring. Use is wisely."); } else { logger.info("Sorry, lightsaber rings are for schwartz masters only."); } // a (very powerful) Instance Level permission: if (currentUser.isPermitted("winnebago:drive:eagle5")) { logger.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " + "Here are the keys - have fun!"); } else { logger.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!"); } // all done - log out! currentUser.logout(); System.exit(0); }
From source file:cn.hh.study.shiro.QuickStart.java
public static void main(String[] args) { // Using the IniSecurityManagerFactory, which will use the an INI file // as the security file. // ini ?? ?(IniSecurityManagerFactory) Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); // Setting up the SecurityManager... SecurityManager securityManager = factory.getInstance(); // SecurityUtils singleton??????? // ? SecurityManager // ???? SecurityUtils.getSubject() ??? SecurityUtils.setSecurityManager(securityManager); // get the currently executing user: Subject currentUser = SecurityUtils.getSubject(); logger.info("User is authenticated: " + currentUser.isAuthenticated()); // Do some stuff with a Session (no need for a web or EJB container!!!) Session session = currentUser.getSession(); session.setAttribute("someKey", "aValue"); String value = (String) session.getAttribute("someKey"); if (value.equals("aValue")) { logger.info("Retrieved the correct value! [" + value + "]"); }// w w w . j a v a2 s . c o m // let's login the current user so we can check against roles and // permissions: if (!currentUser.isAuthenticated()) { // UsernamePasswordToken token = new UsernamePasswordToken("presidentskroob", "12345"); token.setRememberMe(true); try { currentUser.login(token);// } catch (UnknownAccountException uae) { logger.info("There is no user with username of " + token.getPrincipal()); } catch (IncorrectCredentialsException ice) { logger.info("Password for account " + token.getPrincipal() + " was incorrect!"); } catch (LockedAccountException lae) { logger.info("The account for username " + token.getPrincipal() + " is locked. " + "Please contact your administrator to unlock it."); } // ... 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): logger.info("User [" + currentUser.getPrincipal() + "] logged in successfully."); // test a role: if (currentUser.hasRole("schwartz")) { logger.info("May the Schwartz be with you!"); } else { logger.info("Hello, mere mortal."); } if (currentUser.hasRole("goodguy")) { logger.info("May the goodguy be with you!"); } else { logger.info("Hello, mere mortal."); } // test a typed permission (not instance-level) if (currentUser.isPermitted("lightsaber:weild")) { logger.info("You may use a lightsaber ring. Use it wisely."); } else { logger.info("Sorry, lightsaber rings are for schwartz masters only."); } // a (very powerful) Instance Level permission: if (currentUser.isPermitted("winnebago:drive:eagle5")) { logger.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " + "Here are the keys - have fun!"); } else { logger.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!"); } // all done - log out! currentUser.logout();// }
From source file:cn.hlj.shiro.helloworld.Quickstart.java
License:Apache License
public static void main(String[] args) { // 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): Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = 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: // ?? Subject . SecurityUtils.getSubject() . Subject currentUser = SecurityUtils.getSubject(); // Do some stuff with a Session (no need for a web or EJB container!!!) // WEB EJB Session. Session session = currentUser.getSession(); session.setAttribute("someKey", "aValue"); String value = (String) session.getAttribute("someKey"); if (value.equals("aValue")) { log.info("--> Retrieved the correct value! [" + value + "]"); }/*from ww w .ja v a 2 s . co m*/ // let's login the current user so we can check against roles and permissions: // ??. ??. if (!currentUser.isAuthenticated()) { // ???? UsernamePasswordToken . UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa"); token.setRememberMe(true); try { // ?. ??? Shiro ?. currentUser.login(token); } // ???, UnknownAccountException . // ? UsernamePasswordToken token.getPrincipal() ??? catch (UnknownAccountException uae) { log.info("--> There is no user with username of " + token.getPrincipal()); return; } // ?????, IncorrectCredentialsException . catch (IncorrectCredentialsException ice) { log.info("Password for account " + token.getPrincipal() + " was incorrect!"); return; } // ?, LockedAccountException . catch (LockedAccountException lae) { log.info("The account for username " + token.getPrincipal() + " is locked. " + "Please contact your administrator to unlock it."); } // ... catch more exceptions here (maybe custom ones specific to your application? // ? AuthenticationException ? catch (AuthenticationException ae) { //unexpected condition? error? } } //say who they are: //print their identifying principal (in this case, a username): log.info("--> User [" + currentUser.getPrincipal() + "] logged in successfully."); //test a role: // ?? . if (currentUser.hasRole("schwartz")) { log.info("--> May the Schwartz be with you!"); } else { log.info("--> Hello, mere mortal."); return; } //test a typed permission (not instance-level) // ???. if (currentUser.isPermitted("lightsaber:weild")) { log.info("--> You may use a lightsaber ring. Use it wisely."); } else { log.info("Sorry, lightsaber rings are for schwartz masters only."); } //a (very powerful) Instance Level permission: // ????. // ?? User zs query if (currentUser.isPermitted("winnebago:drive:eagle5")) { log.info("--> You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " + "Here are the keys - have fun!"); } else { log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!"); } //all done - log out! // currentUser.logout(); System.exit(0); }