List of usage examples for org.apache.shiro.subject Subject logout
void logout();
From source file:Homework4ShiroCommandLineClient.java
/** * @param args/*w w w .j a v a 2 s . com*/ */ 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 . j av a 2 s . com "\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:QuickstartGuice.java
License:Apache License
public static void main(String[] args) { // We will utilize standard Guice bootstrapping to create a Shiro SecurityManager. Injector injector = Guice.createInjector(new QuickstartShiroModule()); SecurityManager securityManager = injector.getInstance(SecurityManager.class); // 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 ww . java 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) { 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(); System.exit(0); }
From source file:Standalone.java
License:Apache License
public static void main(String[] args) { IniConfiguration config = new IniConfiguration(); //the following call will automatically use shiro.ini at the root of the classpath: config.init();//from ww w. jav a 2 s . c o m //This is for Standalone (single-VM) applications that don't use a configuration container (Spring, JBoss, etc) //See its JavaDoc for our feelings on this. SecurityUtils.setSecurityManager(config.getSecurityManager()); //Now you are ready to access the Subject, as shown in the Quickstart: Subject currentUser = SecurityUtils.getSubject(); //anything else you want to do with the Subject (see the Quickstart for examples). currentUser.logout(); System.exit(0); }
From source file:$.AuthRS.java
License:Open Source License
@GET
@Path("/logout")
@Produces(MediaType.APPLICATION_JSON)
public Response logout_dan() {
System.out.println("============= Logout");
Subject _subject = SecurityUtils.getSubject();
if (_subject != null) {
_subject.logout();
}//from ww w . j a v a 2 s.c o m
return Response.ok(true).build();
}
From source file:at.pollux.thymeleaf.shiro.dialect.HasPermissionTagTest.java
License:Apache License
@Test public void itShouldRenderForUserAlice() { TestUsers user = TestUsers.ALICE;/* ww w .jav a 2s . co m*/ Subject subjectUnderTest = createAndLoginSubject(user); String result = processThymeleafFile(FILE_UNDER_TEST, new Context()); assertThat(result, not(containsString("shiro:"))); assertThat(result, containsString("PERM_A_ATTRIBUTE_STATIC")); assertThat(result, containsString("PERM_A_ELEMENT_STATIC")); assertThat(result, containsString("PERM_B_ATTRIBUTE_STATIC")); assertThat(result, containsString("PERM_B_ELEMENT_STATIC")); assertThat(result, containsString("PERM_C_ATTRIBUTE_STATIC")); assertThat(result, containsString("PERM_C_ELEMENT_STATIC")); subjectUnderTest.logout(); }
From source file:at.pollux.thymeleaf.shiro.dialect.HasPermissionTagTest.java
License:Apache License
@Test public void itShouldRenderForUserBob() { TestUsers user = TestUsers.BOB;/*w ww . j ava 2 s . c o m*/ Subject subjectUnderTest = createAndLoginSubject(user); String result = processThymeleafFile(FILE_UNDER_TEST, new Context()); assertThat(result, not(containsString("shiro:"))); assertThat(result, containsString("PERM_A_ATTRIBUTE_STATIC")); assertThat(result, containsString("PERM_A_ELEMENT_STATIC")); assertThat(result, not(containsString("PERM_B_ATTRIBUTE_STATIC"))); assertThat(result, not(containsString("PERM_B_ELEMENT_STATIC"))); assertThat(result, containsString("PERM_C_ATTRIBUTE_STATIC")); assertThat(result, containsString("PERM_C_ELEMENT_STATIC")); subjectUnderTest.logout(); }
From source file:at.pollux.thymeleaf.shiro.dialect.HasPermissionTagTest.java
License:Apache License
@Test public void itShouldRenderForUserCaesar() { TestUsers user = TestUsers.CAESAR;//from ww w . j a v a 2 s .co m Subject subjectUnderTest = createAndLoginSubject(user); String result = processThymeleafFile(FILE_UNDER_TEST, new Context()); assertThat(result, not(containsString("shiro:"))); assertThat(result, not(containsString("PERM_A_ATTRIBUTE_STATIC"))); assertThat(result, not(containsString("PERM_A_ELEMENT_STATIC"))); assertThat(result, not(containsString("PERM_B_ATTRIBUTE_STATIC"))); assertThat(result, not(containsString("PERM_B_ELEMENT_STATIC"))); assertThat(result, containsString("PERM_C_ATTRIBUTE_STATIC")); assertThat(result, containsString("PERM_C_ELEMENT_STATIC")); subjectUnderTest.logout(); }
From source file:at.pollux.thymeleaf.shiro.dialect.ShiroDialectTest.java
License:Apache License
@Test public void testPrincipal() { Subject subjectUnderTest = new Subject.Builder(getSecurityManager()).buildSubject(); setSubject(subjectUnderTest);/* w ww . j a v a 2s . c o m*/ Context context = new Context(); String result; // Guest user result = templateEngine.process(TEST_TEMPLATE_PATH, context); assertNull(subjectUnderTest.getPrincipal()); // sanity assertFalse(result.contains("shiro:")); assertFalse(result.contains("DEFPRINCIPAL1")); assertFalse(result.contains("DEFPRINCIPAL2")); // Logged in user subjectUnderTest.login(new UsernamePasswordToken(USER1, PASS1)); assertEquals(USER1, subjectUnderTest.getPrincipal()); // sanity result = templateEngine.process(TEST_TEMPLATE_PATH, context); assertFalse(result.contains("shiro:")); assertTrue(result.contains("DEFPRINCIPAL1<span>" + USER1 + "</span>DEFPRINCIPAL1")); assertTrue(result.contains("DEFPRINCIPAL2" + USER1 + "DEFPRINCIPAL2")); subjectUnderTest.logout(); }
From source file:at.pollux.thymeleaf.shiro.dialect.ShiroDialectTest.java
License:Apache License
@Test public void testPrincipalWithType() { Subject subjectUnderTest = new Subject.Builder(getSecurityManager()).buildSubject(); setSubject(subjectUnderTest);//from w ww . jav a 2s. co m Context context = new Context(); String result; // Guest user result = templateEngine.process(TEST_TEMPLATE_PATH, context); assertFalse(result.contains("shiro:")); assertFalse(result.contains("TYPEPRINCIPAL1")); assertFalse(result.contains("TYPEPRINCIPAL2")); // Logged in user subjectUnderTest.login(new UsernamePasswordToken(USER1, PASS1)); assertEquals(Integer.valueOf(0), SecurityUtils.getSubject().getPrincipals().oneByType(Integer.class)); // sanity result = templateEngine.process(TEST_TEMPLATE_PATH, context); assertFalse(result.contains("shiro:")); assertTrue(result.contains("TYPEPRINCIPAL1<span>0</span>TYPEPRINCIPAL1")); assertTrue(result.contains("TYPEPRINCIPAL20TYPEPRINCIPAL2")); subjectUnderTest.logout(); }