List of usage examples for org.apache.shiro SecurityUtils setSecurityManager
public static void setSecurityManager(SecurityManager securityManager)
From source file:com.test.shiro.App.java
public static void main(String[] args) { log.info("My First Apache Shiro Application"); //1./*from w ww. j ava 2 s . c o m*/ IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini"); //2. org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance(); //3. SecurityUtils.setSecurityManager(securityManager); Subject currentUser = SecurityUtils.getSubject(); // 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."); 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(); }
From source file:com.test.shiro.Tutorial.java
public static void main(String[] args) { log.info("My First Apache Shiro Application"); System.out.println("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"); if (!currentUser.isAuthenticated()) { //collect user principals and credentials in a gui specific manner //such as username/password html form, X509 certificate, OpenID, etc. //We'll use the username/password example here since it is the most common. UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa"); //this is all you have to do to support 'remember me' (no config - built in!): token.setRememberMe(true);// ww w.j av a 2 s . c o m try { currentUser.login(token); } catch (UnknownAccountException uae) { log.info("There is no user with username of " + token.getPrincipal()); System.out.println("There is no user with username of " + token.getPrincipal()); } catch (IncorrectCredentialsException ice) { log.info("Password for account " + token.getPrincipal() + " was incorrect!"); System.out.println("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."); System.out.println("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) { ae.printStackTrace(); } } log.info("User [" + currentUser.getPrincipal() + "] logged in successfully."); System.out.println("User [" + currentUser.getPrincipal() + "] logged in successfully."); if (currentUser.hasRole("schwartz")) { log.info("May the Schwartz be with you!"); System.out.println("May the Schwartz be with you!"); } else { log.info("Hello, mere mortal."); System.out.println("Hello, mere mortal."); } if (currentUser.isPermitted("lightsaber:weild")) { log.info("You may use a lightsaber ring. Use it wisely."); System.out.println("You may use a lightsaber ring. Use it wisely."); } else { log.info("Sorry, lightsaber rings are for schwartz masters only."); System.out.println("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!"); System.out.println("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!"); System.out.println("Sorry, you aren't allowed to drive the 'eagle5' winnebago!"); } currentUser.logout(); System.exit(0); }
From source file:com.thesett.util.security.shiro.ShiroUtils.java
License:Apache License
protected static void setSecurityManager(SecurityManager securityManager) { SecurityUtils.setSecurityManager(securityManager); }
From source file:com.ucs.test.Quickstart.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 /*from w w w . j a v a 2 s . co m*/ Subject currentUser = SecurityUtils.getSubject(); // Do some stuff with a Session (no need for a web or EJB container!!!) // : Shiro 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 + "]"); } // 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."); // 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:xyzzzzz")) { 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("employee:query:tom")) { 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.vectorization.server.node.Application.java
License:Open Source License
public void run(String[] args) { Options options = createOptions();//from w ww . j a v a2s . co m try { CommandLine line = commandLineParser.parse(options, args); if (line.hasOption("V")) { printVersionAndExit(); } if (line.hasOption("help")) { printHelpAndExit(options); } if (line.hasOption("P")) { int port = Integer.parseInt(line.getOptionValue("P")); server.setPort(port); } printVersion(); SecurityUtils.setSecurityManager(securityManager); server.run(); } catch (ParseException e) { e.printStackTrace(); } log.info("Server Stopping"); }
From source file:com.vmware.gemfire.tools.pulse.tests.Server.java
License:Apache License
public Server(int port, String properties, String jsonAuthFile) throws Exception { this.propFile = properties; mbs = ManagementFactory.getPlatformMBeanServer(); url = new JMXServiceURL(formJMXServiceURLString(DEFAULT_HOST, port)); // Load the beans first, otherwise we get access denied loadMBeans();/* www .j a va 2s . c om*/ if (jsonAuthFile != null) { System.setProperty("spring.profiles.active", "pulse.authentication.gemfire"); Map<String, Object> env = new HashMap<String, Object>(); // set up Shiro Security Manager Properties securityProperties = new Properties(); securityProperties.setProperty(SampleSecurityManager.SECURITY_JSON, jsonAuthFile); Realm realm = new CustomAuthRealm(SampleSecurityManager.class.getName(), securityProperties); SecurityManager securityManager = new DefaultSecurityManager(realm); SecurityUtils.setSecurityManager(securityManager); // register the AccessControll bean AccessControlMBean acc = new AccessControlMBean(); ObjectName accessControlMBeanON = new ObjectName(ResourceConstants.OBJECT_NAME_ACCESSCONTROL); MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer(); platformMBeanServer.registerMBean(acc, accessControlMBeanON); // wire in the authenticator and authorizaton JMXShiroAuthenticator interceptor = new JMXShiroAuthenticator(); env.put(JMXConnectorServer.AUTHENTICATOR, interceptor); cs = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs); cs.setMBeanServerForwarder(new MBeanServerWrapper()); //set up the AccessControlMXBean } else { System.setProperty("spring.profiles.active", "pulse.authentication.default"); cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); } try { java.rmi.registry.LocateRegistry.createRegistry(port); System.out.println("RMI registry ready."); } catch (Exception e) { System.out.println("Exception starting RMI registry:"); e.printStackTrace(); } cs.start(); }
From source file:com.wegas.app.AbstractEJBContainerTest.java
License:MIT License
@BeforeClass public static void setUp() throws Exception { Map<String, Object> properties = new HashMap<>(); // Init Ejb container properties.put(EJBContainer.MODULES, new File[] { new File("../wegas-core/target/embed-classes") }); properties.put("org.glassfish.ejb.embedded.glassfish.installation.root", "../wegas-core/src/test/glassfish"); //properties.put(EJBContainer.APP_NAME,"class"); //ejbContainer.getContext().rebind("inject", this); // Init shiro SecurityUtils.setSecurityManager(new IniSecurityManagerFactory("classpath:shiro.ini").getInstance()); Logger.getLogger("javax.enterprise.system.tools.deployment").setLevel(Level.OFF); Logger.getLogger("javax.enterprise.system").setLevel(Level.OFF); container = EJBContainer.createEJBContainer(properties); Helper.lookupBy(container.getContext(), UserFacade.class, UserFacade.class).guestLogin(); //login as guest gmFacade = Helper.lookupBy(container.getContext(), GameModelFacade.class, GameModelFacade.class); }
From source file:com.wegas.app.AbstractEmbeddedGlassfishTest.java
License:MIT License
@BeforeClass public static void setUp() throws Exception { try {//from w ww . j a v a2 s . co m Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); BootstrapProperties bootstrapProperties = new BootstrapProperties(); //bootstrapProperties.setInstallRoot("./src/test/glassfish"); // Only for glassfish-embedded-staticshell GlassFishProperties glassfishProperties = new GlassFishProperties(); glassfishProperties.setPort("https-listener", 5353); glassfishProperties.setPort("http-listener", 5454); glassfishProperties.setConfigFileURI( (new File("./src/test/glassfish/domains/domain1/config/domain.xml")).toURI().toString()); TestHelper.resetTestDB(); glassfish = GlassFishRuntime.bootstrap(bootstrapProperties).newGlassFish(glassfishProperties); Logger.getLogger("javax.enterprise.system.tools.deployment").setLevel(Level.OFF); Logger.getLogger("javax.enterprise.system").setLevel(Level.OFF); glassfish.start(); File war = new File("./target/Wegas.war"); appName = glassfish.getDeployer().deploy(war, "--contextroot=Wegas"); //ScatteredArchive archive = new ScatteredArchive("Wegas", // ScatteredArchive.Type.WAR, // new File("./target/embed-war/")); //archive.addClassPath(new File("./target/classes/")); // target/classes directory contains complied servlets //archive.addClassPath(new File("../wegas-core/target/classes")); // wegas-core dependency setBaseUrl("http://localhost:5454/Wegas"); context = AbstractEmbeddedGlassfishTest.getContext(); userFacade = lookup(UserFacade.class); userFacade.guestLogin(); gmFacade = lookup(GameModelFacade.class); } catch (Exception e) { if (glassfish != null) { glassfish.dispose(); } throw e; } }
From source file:com.wegas.app.pmg.GameModelTest.java
License:MIT License
@BeforeClass public static void setUp() throws Exception { Map<String, Object> properties = new HashMap<>(); // Init Ejb container properties.put(EJBContainer.MODULES, new File[] { new File("../wegas-core/target/embed-classes") }); properties.put("org.glassfish.ejb.embedded.glassfish.installation.root", "../wegas-core/src/test/glassfish"); //properties.put(EJBContainer.APP_NAME,"class"); //ejbContainer.getContext().rebind("inject", this); // Init shiro SecurityUtils.setSecurityManager(new IniSecurityManagerFactory("classpath:shiro.ini").getInstance()); Logger.getLogger("javax.enterprise.system.tools.deployment").setLevel(Level.OFF); Logger.getLogger("javax.enterprise.system").setLevel(Level.OFF); container = EJBContainer.createEJBContainer(properties); Helper.lookupBy(container.getContext(), UserFacade.class, UserFacade.class).guestLogin(); //login as guest gmFacade = Helper.lookupBy(container.getContext(), GameModelFacade.class, GameModelFacade.class); }
From source file:com.wegas.core.ejb.TestHelper.java
License:MIT License
public static EJBContainer getEJBContainer() throws NamingException { Map<String, Object> properties = new HashMap<>(); // Init Ejb container properties.put(EJBContainer.MODULES, new File[] { new File("target/embed-classes") }); properties.put("org.glassfish.ejb.embedded.glassfish.installation.root", "./src/test/glassfish"); //properties.put(EJBContainer.APP_NAME,"class"); //ejbContainer.getContext().rebind("inject", this); // Init shiro SecurityUtils.setSecurityManager(new IniSecurityManagerFactory("classpath:shiro.ini").getInstance()); Logger.getLogger("javax.enterprise.system.tools.deployment").setLevel(Level.SEVERE); Logger.getLogger("javax.enterprise.system").setLevel(Level.SEVERE); org.glassfish.ejb.LogFacade.getLogger().setLevel(Level.SEVERE); return EJBContainer.createEJBContainer(properties); }