List of usage examples for org.apache.shiro SecurityUtils setSecurityManager
public static void setSecurityManager(SecurityManager securityManager)
From source file:com.github.ldw4033.shiro.HelloShiro.java
public static void main(String[] args) { // 1??SecurityManagerIni??SecurityManager Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); // 2?SecurityManager SecurityUtils 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 + "]"); // }// w ww.java 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("lonestarr2", "vespa2"); log.info(token.getPrincipal() + ""); 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:eagle6")) { 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.github.mizool.technology.web.MockitoExtensions.java
License:Apache License
public Subject mockSubject() { SecurityManager securityManager = mock(SecurityManager.class); SecurityUtils.setSecurityManager(securityManager); Subject subject = mock(Subject.class); SubjectThreadState subjectThreadState = new SubjectThreadState(subject); subjectThreadState.bind();/* w ww. java 2 s . c o m*/ return subject; }
From source file:com.github.richardwilly98.esdms.inject.TestProviderModule.java
License:Open Source License
@Override protected void configure() { install(new BootstrapModule()); install(new TestEsClientModule()); install(new ServicesModule()); install(new ShiroAopModule()); install(new EsShiroModule()); Injector injector = Guice.createInjector(new CloseableModule(), new Jsr250Module(), new BootstrapModule(), new TestEsClientModule(), new ServicesModule(), new EsShiroModule()); org.apache.shiro.mgt.SecurityManager securityManager = injector .getInstance(org.apache.shiro.mgt.SecurityManager.class); SecurityUtils.setSecurityManager(securityManager); }
From source file:com.github.richardwilly98.esdms.services.AuthenticationProvider.java
License:Open Source License
@Inject AuthenticationProvider(final Client client, final org.apache.shiro.mgt.SecurityManager securityManager, final UserService userService) throws ServiceException { this.client = client; this.securityManager = securityManager; this.userService = userService; SecurityUtils.setSecurityManager(securityManager); createIndex();/*www . ja v a 2 s. co m*/ refreshIndex(); }
From source file:com.github.sdorra.shiro.ShiroRule.java
License:Open Source License
/** * Method description/*from ww w . jav a 2s . co m*/ * * * @param subjectAware */ private void initializeSecurityManager(SubjectAwareDescriptor subjectAware) { String cfg = subjectAware.getConfiguration(); if (cfg.length() > 0) { Factory<SecurityManager> factory = new IniSecurityManagerFactory(cfg); SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); } String username = subjectAware.getUsername(); if ((username != null) && (username.length() > 0)) { UsernamePasswordToken token = new UsernamePasswordToken(username, subjectAware.getPassword()); SecurityUtils.getSubject().login(token); } }
From source file:com.github.sdorra.shiro.ShiroRule.java
License:Open Source License
/** * Method description//from ww w. j a v a 2 s.c o m * */ private void tearDownShiro() { try { SecurityManager securityManager = SecurityUtils.getSecurityManager(); LifecycleUtils.destroy(securityManager); ThreadContext.unbindSecurityManager(); ThreadContext.unbindSubject(); ThreadContext.remove(); } catch (UnavailableSecurityManagerException e) { // we don't care about this when cleaning up the test environment // (for example, maybe the subclass is a unit test and it didn't // need a SecurityManager instance because it was using only mock Subject instances) } SecurityUtils.setSecurityManager(null); }
From source file:com.gwthotel.admintest.suite.Test4.java
License:Apache License
private void testShiro() { Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(realM); org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); Subject currentUser = SecurityUtils.getSubject(); PasswordSecurityToken token = construct("user", "secret", null); try {//from w ww .j a v a2s . c o m currentUser.login(token); fail("Not expected here"); } catch (Exception e) { System.out.println("Is expected, hotel is null"); } token = construct("user", "secret", "hotel"); try { currentUser.login(token); fail("Not expected here"); } catch (Exception e) { System.out.println("Is expected, hotel does not exist"); } // now create a hotel OObject ho = new OObject(); ho.setName("hotel"); ho.setDescription("Grzyb"); List<OObjectRoles> roles = new ArrayList<OObjectRoles>(); iAdmin.addOrModifObject(getI(), ho, roles); try { currentUser.login(token); fail("Not expected here"); } catch (Exception e) { System.out.println("Is expected, user do not have any role"); } roles = new ArrayList<OObjectRoles>(); Person pe = new Person(); pe.setName("user"); pe.setDescription("user name"); OObjectRoles role = new OObjectRoles(pe); role.getRoles().add("man"); roles.add(role); iAdmin.addOrModifObject(getI(), ho, roles); currentUser.login(token); System.out.println("Welcome .."); assertTrue("Man role expected", currentUser.hasRole("man")); assertFalse("Admin role not expected", currentUser.hasRole("admin")); currentUser.logout(); }
From source file:com.gwthotel.admintest.suite.Test4.java
License:Apache License
private void testShiro1() { Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(realM); org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); Subject currentUser = SecurityUtils.getSubject(); PasswordSecurityToken token = construct("user", "secret", "hotel"); currentUser.login(token);//w w w. j a v a 2s.c o m assertTrue("Man role expected", currentUser.hasRole("mana")); assertTrue("Acc role expected", currentUser.hasRole("acc")); assertFalse("Rybka role not expected", currentUser.hasRole("rybka")); }
From source file:com.hf.shiro.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:shiro2/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: ///*from w ww . j a v a 2 s .c o m*/ 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 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: //winnebago:drive:eagle5winnebagoeagle5drive 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.ikanow.aleph2.security.service.SecurityService.java
License:Apache License
@Inject public SecurityService(IServiceContext serviceContext, SecurityManager securityManager) { this.serviceContext = serviceContext; SecurityUtils.setSecurityManager(securityManager); }