List of usage examples for org.apache.shiro.subject Subject getSession
Session getSession();
From source file:Homework4ShiroCommandLineClient.java
/** * @param args/*w w w. jav a 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(//w w w . j a v a2 s. c om "\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 www . j a v a2s. 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) { 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.basketballsalzburg.bbstats.components.LoginLink.java
License:Apache License
private void removeSavedRequest() { Subject subject = securityService.getSubject(); if (subject != null) { subject.getSession().removeAttribute(WebUtils.SAVED_REQUEST_KEY); }//www . ja va 2s . c o m }
From source file:au.org.theark.core.web.component.customfieldupload.CustomFieldUploadStep4.java
License:Open Source License
@Override public void onStepOutNext(AbstractWizardForm<?> form, AjaxRequestTarget target) { // Filename seems to be lost from model when moving between steps in wizard containerForm.getModelObject().getUpload().setFilename(wizardForm.getFileName()); // Perform actual upload of data containerForm.getModelObject().getUpload().setStartTime(new Date(System.currentTimeMillis())); StringBuffer uploadReport = null; String filename = containerForm.getModelObject().getFileUpload().getClientFileName(); String fileFormat = filename.substring(filename.lastIndexOf('.') + 1).toUpperCase(); FileFormat fileFormatObj;/*from www. j av a 2 s . c o m*/ fileFormatObj = iArkCommonService.getFileFormatByName(fileFormat); containerForm.getModelObject().getUpload().setFileFormat(fileFormatObj); char delimiterChar = containerForm.getModelObject().getUpload().getDelimiterType().getDelimiterCharacter(); Subject currentUser = SecurityUtils.getSubject(); Long studyId = (Long) currentUser.getSession().getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID); Study study = iArkCommonService.getStudy(studyId); Long sessionModuleId = (Long) SecurityUtils.getSubject().getSession() .getAttribute(au.org.theark.core.Constants.ARK_MODULE_KEY); ArkModule arkModule = iArkCommonService.getArkModuleById(sessionModuleId); //We have to decide the custom filed/category goes under which function to the DB. //At the moment if it is subject or the lims //Knowing the Module and the current fuction we can dicide that ArkFunction currentFunction = containerForm.getModelObject().getUpload().getArkFunction(); UploadLevel uploadLevel = containerForm.getModelObject().getUpload().getUploadLevel(); ArkFunction adjustedArkFunctionForCustomField = null; //Common custom field update split to study in here. if (arkModule.getName().equals(au.org.theark.core.Constants.ARK_MODULE_STUDY) && currentFunction.getName() .equals(au.org.theark.core.Constants.FUNCTION_KEY_VALUE_SUBJECT_CUSTOM_FIELD_UPLOAD)) { // Field upload if (uploadLevel.getName().equalsIgnoreCase( au.org.theark.core.web.component.customfieldupload.Constants.UPLOAD_LEVEL_FIELD)) { adjustedArkFunctionForCustomField = iArkCommonService .getArkFunctionByName(au.org.theark.core.Constants.FUNCTION_KEY_VALUE_SUBJECT_CUSTOM_FIELD); iCustomImporter = new CustomFieldImporter(study, adjustedArkFunctionForCustomField, iArkCommonService, fileFormat, delimiterChar); //Category upload } else if (uploadLevel.getName().equalsIgnoreCase( au.org.theark.core.web.component.customfieldupload.Constants.UPLOAD_LEVEL_CATEGORY)) { adjustedArkFunctionForCustomField = iArkCommonService.getArkFunctionByName( au.org.theark.core.Constants.FUNCTION_KEY_VALUE_SUBJECT_CUSTOM_FIELD_CATEGORY); iCustomImporter = new CustomFieldCategoryImporter(study, adjustedArkFunctionForCustomField, iArkCommonService, fileFormat, delimiterChar); } } //Common custom field update split to lims in here. if (arkModule.getName().equals(au.org.theark.core.Constants.ARK_MODULE_LIMS) && currentFunction.getName() .equals(au.org.theark.core.Constants.FUNCTION_KEY_VALUE_LIMS_CUSTOM_FIELD_UPLOAD)) { // Field upload if (uploadLevel.getName().equalsIgnoreCase( au.org.theark.core.web.component.customfieldupload.Constants.UPLOAD_LEVEL_FIELD)) { adjustedArkFunctionForCustomField = iArkCommonService .getArkFunctionByName(au.org.theark.core.Constants.FUNCTION_KEY_VALUE_LIMS_CUSTOM_FIELD); iCustomImporter = new CustomFieldImporter(study, adjustedArkFunctionForCustomField, iArkCommonService, fileFormat, delimiterChar); //Category upload } else if (uploadLevel.getName().equalsIgnoreCase( au.org.theark.core.web.component.customfieldupload.Constants.UPLOAD_LEVEL_CATEGORY)) { adjustedArkFunctionForCustomField = iArkCommonService.getArkFunctionByName( au.org.theark.core.Constants.FUNCTION_KEY_VALUE_LIMS_CUSTOM_FIELD_CATEGORY); iCustomImporter = new CustomFieldCategoryImporter(study, adjustedArkFunctionForCustomField, iArkCommonService, fileFormat, delimiterChar); } } //Need to persist the custom field category try { log.info("Uploading data dictionary file"); InputStream inputStream = containerForm.getModelObject().getFileUpload().getInputStream(); if (fileFormat.equalsIgnoreCase("XLS")) { Workbook w; try { w = Workbook.getWorkbook(inputStream); inputStream = iCustomImporter.convertXlsToCsv(w); inputStream.reset(); } catch (BiffException e) { log.error(e.getMessage()); } catch (IOException e) { log.error(e.getMessage()); } } uploadReport = iCustomImporter.uploadAndReportMatrixDataDictionaryFile(inputStream, containerForm.getModelObject().getFileUpload().getSize()); // Determined FieldUpload entities if (iCustomImporter instanceof CustomFieldCategoryImporter) containerForm.getModelObject().setCustomFieldUploadCategoryCollection( ((CustomFieldCategoryImporter) iCustomImporter).getFieldUploadList()); else if (iCustomImporter instanceof CustomFieldImporter) { containerForm.getModelObject().setCustomFieldUploadCollection( ((CustomFieldImporter) iCustomImporter).getFieldUploadList()); } } catch (FileFormatException ffe) { log.error(Constants.FILE_FORMAT_EXCEPTION + ffe); } catch (IOException ioe) { log.error(ioe.getMessage()); } catch (ArkSystemException ase) { log.error(ase.getMessage()); } // Update the report if (uploadReport != null) { updateUploadReport(uploadReport.toString()); } // Save all objects to the database save(iCustomImporter); }
From source file:au.org.theark.lims.util.BioCustomFieldUploadValidator.java
License:Open Source License
public BioCustomFieldUploadValidator() { super();// ww w. j a v a 2s .c o m Subject currentUser = SecurityUtils.getSubject(); studyId = (Long) currentUser.getSession().getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID); this.study = iArkCommonService.getStudy(studyId); this.existantSubjectUIDRows = new HashSet<Integer>(); this.nonExistantUIDs = new HashSet<Integer>(); this.errorCells = new HashSet<ArkGridCell>(); simpleDateFormat.setLenient(false); }
From source file:au.org.theark.lims.util.BioCustomFieldUploadValidator.java
License:Open Source License
@SuppressWarnings("unchecked") public BioCustomFieldUploadValidator(IArkCommonService iArkCommonService, ILimsService iLimsService) { super();/* w w w. jav a 2 s. com*/ this.iArkCommonService = iArkCommonService; this.iLimsService = iLimsService; Subject currentUser = SecurityUtils.getSubject(); studyId = (Long) currentUser.getSession().getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID); this.study = iArkCommonService.getStudy(studyId); this.existantSubjectUIDRows = new HashSet<Integer>(); this.nonExistantUIDs = new HashSet<Integer>(); this.errorCells = new HashSet<ArkGridCell>(); simpleDateFormat.setLenient(false); }
From source file:au.org.theark.lims.web.component.bioupload.BioUploadStep4.java
License:Open Source License
@Override public void onStepOutNext(AbstractWizardForm<?> form, AjaxRequestTarget target) { form.getNextButton().setEnabled(false); target.add(form.getNextButton());//from w w w.j a v a 2s.c om // Filename seems to be lost from model when moving between steps in wizard? is this a symptom of something greater? containerForm.getModelObject().getUpload().setFilename(wizardForm.getFileName()); String fileFormat = containerForm.getModelObject().getUpload().getFileFormat().getName(); char delimiterChar = containerForm.getModelObject().getUpload().getDelimiterType().getDelimiterCharacter(); try { List<String> uidsToUpload = containerForm.getModelObject().getUidsToUpload(); //log.info("________________________________________________________" + "about to try passing list of uids is of size " + uidsToUpload.size() ); InputStream inputStream = containerForm.getModelObject().getFileUpload().getInputStream(); long size = containerForm.getModelObject().getFileUpload().getSize(); Long uploadId = containerForm.getModelObject().getUpload().getId(); String report = generateInitialUploadReport(); Subject currentUser = SecurityUtils.getSubject(); Long studyId = (Long) currentUser.getSession() .getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID); if (containerForm.getModelObject().getUpload().getUploadType().getName() .equalsIgnoreCase("Subject Demographic Data")) { StudyDataUploadExecutor task = new StudyDataUploadExecutor(iArkCommonService, iLimsService, inputStream, uploadId, //null user studyId, fileFormat, delimiterChar, size, report, uidsToUpload); task.run(); } else if (containerForm.getModelObject().getUpload().getUploadType().getName() .equalsIgnoreCase("Study-specific (custom) Data")) { BioSpecimenCustomDataUploadExecutor task = new BioSpecimenCustomDataUploadExecutor( iArkCommonService, iLimsService, inputStream, uploadId, //null user studyId, fileFormat, delimiterChar, size, report, uidsToUpload); task.run(); } } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }
From source file:au.org.theark.phenotypic.util.PhenoDataUploadValidator.java
License:Open Source License
public PhenoDataUploadValidator() { super();/* w w w . j a v a2 s.c om*/ Subject currentUser = SecurityUtils.getSubject(); studyId = (Long) currentUser.getSession().getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID); this.study = iArkCommonService.getStudy(studyId); this.existantSubjectUIDRows = new HashSet<Integer>(); this.nonExistantUIDs = new HashSet<Integer>(); this.errorCells = new HashSet<ArkGridCell>(); this.warningRows = new HashSet<Integer>(); this.insertCells = new HashSet<ArkGridCell>(); simpleDateFormat.setLenient(false); }
From source file:au.org.theark.phenotypic.util.PhenoDataUploadValidator.java
License:Open Source License
@SuppressWarnings("unchecked") public PhenoDataUploadValidator(IArkCommonService iArkCommonService, IPhenotypicService iPhenotypicService) { super();//w w w . ja v a2s.c o m this.iArkCommonService = iArkCommonService; this.iPhenotypicService = iPhenotypicService; Subject currentUser = SecurityUtils.getSubject(); studyId = (Long) currentUser.getSession().getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID); this.study = iArkCommonService.getStudy(studyId); this.existantSubjectUIDRows = new HashSet<Integer>(); this.nonExistantUIDs = new HashSet<Integer>(); this.errorCells = new HashSet<ArkGridCell>(); this.warningRows = new HashSet<Integer>(); this.insertCells = new HashSet<ArkGridCell>(); simpleDateFormat.setLenient(false); }