List of usage examples for org.apache.shiro SecurityUtils getSubject
public static Subject getSubject()
From source file:cn.dreampie.common.plugin.shiro.plugin.AbstractAuthzHandler.java
License:Apache License
/** * ShiroSubject * * @return */ protected Subject getSubject() { return SecurityUtils.getSubject(); }
From source file:cn.dreampie.shiro.ShiroAuthenticatingFilter.java
License:Apache License
protected void doCaptchaValidate(CaptchaUsernamePasswordToken token) { Session session = SecurityUtils.getSubject().getSession(); if (session == null) { throw new UnknownSessionException("Unable found required Session"); } else {//from www. j av a 2s . c om if (session.getAttribute(DEFAULT_CAPTCHA_PARAM) != null) { String captcha = session.getAttribute(DEFAULT_CAPTCHA_PARAM).toString(); // String captcha = CookieUtils.getCookie(request, AppConstants.CAPTCHA_NAME); if (token.getCaptcha() != null && captcha.equalsIgnoreCase(EncriptionKit.encrypt(token.getCaptcha().toLowerCase()))) { return; } } throw new IncorrectCaptchaException(); } }
From source file:cn.dsgrp.field.stock.web.account.ProfileController.java
License:Apache License
/** * ?Shiro?Id. */ private BigInteger getCurrentUserId() { ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal(); return user.id; }
From source file:cn.guoyukun.spring.web.controller.permission.PermissionList.java
License:Apache License
public void assertHasPermission(String permission, String errorCode) { if (StringUtils.isEmpty(errorCode)) { errorCode = getDefaultErrorCode(); }/* w ww .jav a 2 s . c om*/ String resourcePermission = resourcePermissions.get(permission); if (resourcePermission == null) { resourcePermission = this.resourceIdentity + ":" + permission; } if (!SecurityUtils.getSubject().isPermitted(resourcePermission)) { throw new UnauthorizedException(MessageUtils.message(errorCode, resourcePermission)); } }
From source file:cn.guoyukun.spring.web.controller.permission.PermissionList.java
License:Apache License
public void assertHasAllPermission(String[] permissions, String errorCode) { if (StringUtils.isEmpty(errorCode)) { errorCode = getDefaultErrorCode(); }// ww w. j a v a2 s. c om if (permissions == null || permissions.length == 0) { throw new UnauthorizedException( MessageUtils.message(errorCode, resourceIdentity + ":" + Arrays.toString(permissions))); } Subject subject = SecurityUtils.getSubject(); for (String permission : permissions) { String resourcePermission = resourcePermissions.get(permission); if (resourcePermission == null) { resourcePermission = this.resourceIdentity + ":" + permission; } if (!subject.isPermitted(resourcePermission)) { throw new UnauthorizedException( MessageUtils.message(errorCode, resourceIdentity + ":" + Arrays.toString(permissions))); } } }
From source file:cn.guoyukun.spring.web.controller.permission.PermissionList.java
License:Apache License
public void assertHasAnyPermission(String[] permissions, String errorCode) { if (StringUtils.isEmpty(errorCode)) { errorCode = getDefaultErrorCode(); }//from w w w. jav a 2s . c o m if (permissions == null || permissions.length == 0) { throw new UnauthorizedException( MessageUtils.message(errorCode, resourceIdentity + ":" + Arrays.toString(permissions))); } Subject subject = SecurityUtils.getSubject(); for (String permission : permissions) { String resourcePermission = resourcePermissions.get(permission); if (resourcePermission == null) { resourcePermission = this.resourceIdentity + ":" + permission; } if (subject.isPermitted(resourcePermission)) { return; } } throw new UnauthorizedException( MessageUtils.message(errorCode, resourceIdentity + ":" + Arrays.toString(permissions))); }
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 www. j a va 2s. com*/ // 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 + "]"); }/* ww w. j a v a 2 s . co 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();// }
From source file:cn.hlj.shiro.helloworld.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: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 . SecurityUtils.getSubject() . Subject currentUser = SecurityUtils.getSubject(); // Do some stuff with a Session (no need for a web or EJB container!!!) // WEB EJB 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 + "]"); }//from w ww. j a v a 2 s.c o m // 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 { // ?. ??? Shiro ?. currentUser.login(token); } // ???, UnknownAccountException . // ? UsernamePasswordToken token.getPrincipal() ??? catch (UnknownAccountException uae) { log.info("--> There is no user with username of " + token.getPrincipal()); return; } // ?????, IncorrectCredentialsException . catch (IncorrectCredentialsException ice) { log.info("Password for account " + token.getPrincipal() + " was incorrect!"); return; } // ?, LockedAccountException . 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? // ? AuthenticationException ? 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."); return; } //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: // ????. // ?? User zs query 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:cn.itganhuo.app.common.utils.HttpUtil.java
License:Apache License
/** * ?Shiro?/*from www . ja v a 2 s . c om*/ * * @version 0.0.1-SNAPSHOT * @author -? * @param key * @param value */ public static void setValue(String key, Object value) { Subject current_user = SecurityUtils.getSubject(); current_user.getSession().setAttribute(key, value); }