List of usage examples for org.apache.shiro.util Factory getInstance
T getInstance();
From source file:Homework4ShiroCommandLineClient.java
/** * @param args//from w ww. java 2 s. co 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(/* ww w.j a v a 2 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 a va 2s .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();/*ww w .ja v a 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:at.pollux.thymeleaf.shiro.dialect.ShiroDialectTest.java
License:Apache License
private static void setupShiro() { Ini ini = new Ini(); Ini.Section usersSection = ini.addSection("users"); usersSection.put(USER1, PASS1 + ",rolea,roled"); usersSection.put(USER2, PASS2 + ",roleb,rolec"); usersSection.put(USER3, PASS3 + ",rolec,rolee"); Ini.Section rolesSection = ini.addSection("roles"); rolesSection.put("rolea", "*"); rolesSection.put("roleb", "permtype1:permaction1:perminst1"); rolesSection.put("rolec", "permtype1:permaction2:*"); rolesSection.put("roled", "permtype3:*"); Factory<SecurityManager> factory = new TestIniSecurityManagerFactory(ini); SecurityManager secMgr = factory.getInstance(); setSecurityManager(secMgr);// w w w . j ava2s . c o m }
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 . ja v a2s. co m*/ 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 . ja v a2 s . c om // 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 ww .jav a 2 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();// }