List of usage examples for org.apache.shiro SecurityUtils setSecurityManager
public static void setSecurityManager(SecurityManager securityManager)
From source file:com.liferay.blade.samples.authenticator.shiro.ShiroAuthenticatorPre.java
License:Apache License
@Activate public void activate() { Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:userauth.ini"); SecurityUtils.setSecurityManager(factory.getInstance()); if (_log.isInfoEnabled()) { _log.info("activate"); }/*from w w w . j av a 2 s. co m*/ }
From source file:com.max.shiro.Tutorial.java
public static void main(String[] args) { System.out.println("start...."); logger.info("My First Apache Shiro Application"); // 1.//from w w w .jav a2 s . co m Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); // 2. SecurityManager securityManager = factory.getInstance(); // 3. 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")) { logger.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) { 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."); } // 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(); System.exit(0); }
From source file:com.mitewater.test.ShrioTest.java
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 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 + "]"); }/*from w w w. j av 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("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:wield")) { 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(); System.exit(0); }
From source file:com.monkeyk.os.web.ShiroTest.java
License:Open Source License
@Test(enabled = false) public void login() { String username = "abc"; //init SecurityManager SimpleAccountRealm realm = new SimpleAccountRealm("simple-realm"); realm.addAccount(username, "abc", "USER"); SimpleAccountRealm realm2 = new SimpleAccountRealm("simple-realm2"); realm2.addAccount(username, "abc", "USER", "ADMIN"); List<Realm> realmList = new ArrayList<>(); realmList.add(realm);/*from ww w . ja v a 2s .co m*/ realmList.add(realm2); SecurityManager securityManager = new DefaultSecurityManager(realmList); SecurityUtils.setSecurityManager(securityManager); UsernamePasswordToken token = new UsernamePasswordToken(username, "abcdd"); final Subject subject = SecurityUtils.getSubject(); subject.login(token); final Subject subject1 = SecurityUtils.getSubject(); assertTrue(subject1.isAuthenticated()); assertFalse(subject1.isPermitted("OK")); assertTrue(subject1.hasRole("USER")); // assertTrue(subject1.isPermitted("USER:c,u")); }
From source file:com.mto.arquillian.demo.security.ShiroBootstrap.java
License:Open Source License
@Override public void contextInitialized(ServletContextEvent servletContextEvent) { ServletContext ctx = servletContextEvent.getServletContext(); String shiroConfig = ctx.getInitParameter("shiroConfigFile"); InputStream in = ctx.getResourceAsStream(shiroConfig); Ini ini = new Ini(); ini.load(in);/*from w w w.ja va 2 s. co m*/ Factory<SecurityManager> factory = new IniSecurityManagerFactory(ini); SecurityManager sm = factory.getInstance(); SecurityUtils.setSecurityManager(sm); }
From source file:com.mybatis.shiro.Tutorial.java
@Test public void testSecurityManager() { Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = (SecurityManager) factory.getInstance(); SecurityUtils.setSecurityManager((SecurityManager) 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 vlaue! [" + value + "]"); if (!currentUser.isAuthenticated()) { UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa"); token.setRememberMe(true);//from ww w . j a va 2 s. co m try { currentUser.login(token); } catch (UnknownAccountException e) { 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."); } } 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("winnerbago: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:com.mybatis.shiro.Tutorial.java
@Test public void testCustomRealm() { Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123"); try {/* ww w . ja v a 2 s .c o m*/ //(?) subject.login(token); } catch (AuthenticationException e) { e.printStackTrace(); } //subject? log.info(subject.isAuthenticated() + ""); subject.logout(); }
From source file:com.mybatis.shiro.Tutorial.java
@Test public void testCustomMultiRealm() { Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123"); try {/*w ww. jav a 2s.c om*/ //4??? subject.login(token); } catch (AuthenticationException e) { //5?? e.printStackTrace(); } System.out.println(subject.isAuthenticated()); //6? subject.logout(); }
From source file:com.mybatis.shiro.Tutorial.java
@Test public void testJdbcRealm() { Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123"); try {/* w w w . j ava2 s . c om*/ //4??? subject.login(token); } catch (AuthenticationException e) { //5?? e.printStackTrace(); } System.out.println(subject.isAuthenticated()); //6? subject.logout(); }
From source file:com.mymanager.security.Authenticator.java
/*** Autenticates a user **/ public Subject authenticate(String username, String pass) { Subject currentUser = null;//from w w w . j a v a 2s . c o m try { Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory( "classpath:shiro.ini"); org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance(); // the key "jdbcRealm" must be the same in the shiro.ini file. JdbcRealm realm = (JdbcRealm) ((IniSecurityManagerFactory) factory).getBeans().get("jdbcRealm"); realm.setAuthenticationQuery(AUTHENTICATION_QUERY); realm.setUserRolesQuery(ROLES_QUERY); //realm.setPermissionsQuery("SELECT permission FROM role_permission,role WHERE role_permission.roleId=role.roleId AND role.name=?"); //realm.setPermissionsLookupEnabled(false); SecurityUtils.setSecurityManager(securityManager); currentUser = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken(username, pass); currentUser.login(token); } catch (Exception e) { e.printStackTrace(); } return currentUser; }