List of usage examples for org.apache.shiro.authc UsernamePasswordToken getPrincipal
public Object getPrincipal()
From source file:Homework4ShiroCommandLineClient.java
/** * @param args// w w w .j a v a2 s.c om */ 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 ww w. ja va2 s. co 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: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 + "]"); }//www. j a v a2 s .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:at.oculus.teamf.technical.accessrights.UserSubject.java
License:Open Source License
public void loginUser(String username, String password) { // let's login the current user so we can check against roles and permissions: if (!currentUser.isAuthenticated()) { UsernamePasswordToken token = new UsernamePasswordToken(username, password); token.setRememberMe(true);/*from ww w . java 2 s . c om*/ log.info("Trying to login user [" + username + "]"); // check if username and password are correct and login try { currentUser.login(token); } catch (UnknownAccountException uae) { //logger.info("There is no user with username of " + token.getPrincipal()); log.warn("There is no user with username of " + token.getPrincipal()); } catch (IncorrectCredentialsException ice) { log.warn("Password for account " + token.getPrincipal() + " was incorrect!"); } catch (LockedAccountException lae) { log.warn("The account for username " + token.getPrincipal() + " is locked. Please contact your administrator to unlock it."); } // ... catch more exception here catch (AuthenticationException ae) { //unexpected conditions ? } } // check if login was succesfull if (currentUser.isAuthenticated()) { log.info("Login [" + currentUser.getPrincipal() + "] successfull"); } else { log.warn("Login failed"); } }
From source file:au.org.theark.core.security.AAFRealm.java
License:Open Source License
@Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException { SimpleAuthenticationInfo sai = null; ArkUserVO userVO = null;//from www. jav a2s . com UsernamePasswordToken token = (UsernamePasswordToken) authcToken; //log.info("IN AAFRealm.doGetAuthenticationInfo"); //log.info("authToken: " + authcToken.getPrincipal().toString()); log.info("AAF token username: " + token.getUsername()); try { //log.info("checking user"); userVO = iArkCommonService.getUser(token.getUsername().trim()); if (userVO != null) { // Check if the user is in the Ark Database ArkUser arkUser = iArkCommonService.getArkUser(token.getUsername().trim()); // Also check if the Ark User is linked with any study and has roles. // If no roles found, stop the user from logging in until an administrator has set it up if (!iArkCommonService.isArkUserLinkedToStudies(arkUser)) { throw new UnknownAccountException(UNKNOWN_ACCOUNT); } final WebRequest webRequest = (WebRequest) RequestCycle.get().getRequest(); final HttpServletRequest httpReq = (HttpServletRequest) webRequest.getContainerRequest(); //log.info("checking shib headers"); String userName = httpReq.getHeader("AJP_mail"); String password = httpReq.getHeader("AJP_Shib-Session-ID"); if (userName != null && password != null) { //log.info("creating SimpleAuthenticationInfo"); sai = new SimpleAuthenticationInfo(token.getPrincipal(), token.getCredentials(), getName()); } } } catch (ArkSystemException e) { log.error(e.getMessage()); } catch (EntityNotFoundException e) { throw new UnknownAccountException(UNKNOWN_ACCOUNT); } return sai; }
From source file:ch.reboundsoft.shinobi.authstore.CachedAuthStoreImpl.java
@Override public synchronized boolean login(String name, String password) { log.info("Login using cached auth store"); Subject currentUser;//from www . ja va 2 s .c om if (subjects.containsKey(name)) { currentUser = subjects.get(name); } else { currentUser = SecurityUtils.getSubject(); subjects.put(name, currentUser); } if (!currentUser.isAuthenticated()) { UsernamePasswordToken token = new UsernamePasswordToken(name, password); try { currentUser.login(token); } catch (UnknownAccountException uae) { log.info("There is no user with username of " + token.getPrincipal()); return false; } catch (IncorrectCredentialsException ice) { log.info("Password for account " + token.getPrincipal() + " was incorrect!"); return false; } catch (LockedAccountException lae) { log.info("The account for username " + token.getPrincipal() + " is locked. " + "Please contact your administrator to unlock it."); return false; } catch (AuthenticationException ae) { log.info("Strange auth error: " + ae.toString()); return false; } } cache.add(getCacheKey(name), password); return true; }
From source file:ch.reboundsoft.shinobi.authstore.DefaultAuthStoreImpl.java
@Override public synchronized boolean login(String name, String password) { log.info("Login using default auth store"); Subject currentUser;//www . j a va 2s .co m if (subjects.containsKey(name)) { currentUser = subjects.get(name); } else { currentUser = SecurityUtils.getSubject(); subjects.put(name, currentUser); } if (!currentUser.isAuthenticated()) { UsernamePasswordToken token = new UsernamePasswordToken(name, password); try { currentUser.login(token); } catch (UnknownAccountException uae) { log.info("There is no user with username of " + token.getPrincipal()); return false; } catch (IncorrectCredentialsException ice) { log.info("Password for account " + token.getPrincipal() + " was incorrect!"); return false; } catch (LockedAccountException lae) { log.info("The account for username " + token.getPrincipal() + " is locked. " + "Please contact your administrator to unlock it."); return false; } catch (AuthenticationException ae) { log.info("Strange auth error: " + ae.toString()); return false; } } return true; }
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 ww w . j a v a2 s . 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 . j ava2 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 + "]"); }//from ww w . ja v a2 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();// }