List of usage examples for org.apache.shiro.session Session setAttribute
void setAttribute(Object key, Object value) throws InvalidSessionException;
From source file:Homework4ShiroCommandLineClient.java
/** * @param args//from w ww.j ava 2 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 www.j a v a 2 s . c o 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 + "]"); }/* ww w.j a va 2s . 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: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
/** * * @param sessionKey setup sessionkey (identifier) for new session * @param Password setup password for new session *//*from w w w . ja v a 2 s.c o m*/ public UserSubject(String sessionKey, String Password) { currentUser = new Subject.Builder().buildSubject(); // session handling for user Session session = currentUser.getSession(); session.setAttribute(sessionKey, Password); log.info("New user session management intialized"); }
From source file:cn.adfi.radius.controller.LoginController.java
private User loginInner(String username, String password) { Subject subject = SecurityUtils.getSubject(); subject.login(new UsernamePasswordToken(username, password)); if (subject.isAuthenticated()) { List<User> lst = userRepo.findByUsername(username); Session session = subject.getSession(); session.setAttribute("user", lst.get(0)); return lst.get(0); }//from w w w. j a v a 2s. c o m return null; }
From source file:cn.adfi.radius.controller.LoginController.java
@RequestMapping(value = "authenticate", method = RequestMethod.POST) public AngularShiroLoginResponse shiroLogin(@RequestBody TokenWarpper tokenWarpper) throws Exception { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); if (new Date().after(df.parse("2015-04-01"))) { throw new LicenseExpiredException("License Expired!"); }//from w w w . ja va 2s.c o m Subject subject = SecurityUtils.getSubject(); subject.login(new UsernamePasswordToken(tokenWarpper.getToken().getPrincipal(), tokenWarpper.getToken().getCredentials())); User user; if (subject.isAuthenticated()) { List<User> lst = userRepo.findByUsername(tokenWarpper.getToken().getPrincipal()); Session session = subject.getSession(); session.setAttribute("user", lst.get(0)); user = lst.get(0); } else { throw new Exception("Username or Password error!"); } AngularShiroAuthc authc = new AngularShiroAuthc(); AngularShiroPrincipal principal = new AngularShiroPrincipal(); principal.setLogin(user.getUsername()); principal.setName(user.getFullname()); principal.setEmail(user.getEmail()); authc.setPrincipal(principal); AngularShiroCredentials credentials = new AngularShiroCredentials(); credentials.setLogin(user.getUsername()); credentials.setName(user.getFullname()); credentials.setEmail(user.getEmail()); authc.setCredentials(credentials); AngularShiroAuthz authz = new AngularShiroAuthz(); authz.setRoles(user.getRolesStringSet()); authz.setPermissions(user.getPermissionStringSet()); AngularShiroInfo info = new AngularShiroInfo(); info.setAuthc(authc); info.setAuthz(authz); AngularShiroLoginResponse resp = new AngularShiroLoginResponse(); resp.setInfo(info); return resp; }
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 www.j a v a2s. 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.com.infcn.ade.system.service.UserRealm.java
/** * ?,.// w ww. ja v a 2 s .c o m */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException { try { checkHandler.checkExpireDate(); } catch (Exception e) { // TODO Auto-generated catch block throw new LicenseException(e.getMessage(), e); } UsernamePasswordCaptchaToken token = (UsernamePasswordCaptchaToken) authcToken; User user = userService.getUser(token.getUsername()); if (user != null && doCaptchaValidate(token)) { byte[] salt = Encodes.decodeHex(user.getSalt()); ShiroUser shiroUser = new ShiroUser(user.getId(), user.getLoginName(), user.getName()); //session Session session = SecurityUtils.getSubject().getSession(); session.setAttribute("user", user); return new SimpleAuthenticationInfo(shiroUser, user.getPassword(), ByteSource.Util.bytes(salt), getName()); } else { return null; } }
From source file:cn.com.xl.core.shiro.ShiroKit.java
License:Apache License
/** * shirosessionKey// ww w .j a v a2 s . c o m * */ public static void setSessionAttr(String key, Object value) { Session session = getSession(); session.setAttribute(key, value); }
From source file:cn.dreampie.common.plugin.shiro.MyFormAuthenticationFilter.java
License:Apache License
protected void setFailureAttribute(ServletRequest request, ServletResponse response, AuthenticationException ae) { String className = ae.getClass().getSimpleName(); Session session = getSubject(request, response).getSession(); session.setAttribute(getFailureKeyAttribute(), className); session.setAttribute(AppConstants.LOGIN_USER_NAME, getUsername(request)); }