List of usage examples for org.springframework.security.core.context SecurityContext getAuthentication
Authentication getAuthentication();
From source file:org.jasig.schedassist.web.register.delegate.DelegateRegistrationHelper.java
/** * //from www . j a v a 2 s. c om * @return true if the current authenticated delegate has ineligible for service */ public boolean currentDelegateIsIneligible() { SecurityContext context = SecurityContextHolder.getContext(); Authentication authentication = context.getAuthentication(); DelegateCalendarAccountUserDetailsImpl currentUser = (DelegateCalendarAccountUserDetailsImpl) authentication .getPrincipal(); boolean result = !currentUser.isEnabled(); return result; }
From source file:com.miserablemind.butter.security.MyAccessDeniedHandler.java
/** * Handle Exception Method// ww w . ja va 2 s. c o m * * @param request {@link HttpServletRequest} provides session and {@link SecurityContext} * @param response used for redirecting to 500 page * @param accessDeniedException exception used for logging the stack trace * @throws IOException * @throws ServletException */ @Override public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException { long userId = 0; SecurityContext securityContext = (SecurityContext) request.getSession() .getAttribute("SPRING_SECURITY_CONTEXT"); if (securityContext != null) { Object principal = securityContext.getAuthentication().getPrincipal(); if (principal != null && principal != "anonymousUser") { AppUser user = (AppUser) principal; userId = user.getId(); } } logger.error("Accessing Denied. User ID: " + userId + " URL: " + request.getServletPath(), accessDeniedException); response.sendRedirect("/error/500"); }
From source file:de.iew.services.impl.TreeSketchPadServiceImplIntegrationTest.java
@Test public void testListAllPolygons() throws Exception { SecurityContext securityContext = SecurityContextHolder.getContext(); Authentication sketchPadUser = securityContext.getAuthentication(); SketchPadService sketchPadService = (SketchPadService) this.applicationContext.getBean("sketchPadService"); double[] x = new double[] { 0, 1, 2, 3 }; double[] y = new double[] { 4, 5, 6, 7 }; sketchPadService.createPolygon(sketchPadUser, 1, x, y, 1, 1); sketchPadService.createPolygon(this.user2, 1, x, y, 1, 1); List<Polygon> polygons = sketchPadService.listAllPolygons(1); assertEquals(1, polygons.size());/*from ww w. ja v a2 s . co m*/ }
From source file:com.hp.autonomy.searchcomponents.hod.test.HodTestConfiguration.java
@PostConstruct public void init() throws HodErrorException { final String apiKey = environment.getProperty(API_KEY_PROPERTY); final String application = environment.getProperty(APPLICATION_PROPERTY); final String domain = environment.getProperty(DOMAIN_PROPERTY); final AuthenticationService authenticationService = new AuthenticationServiceImpl(hodServiceConfig); final TokenProxy<EntityType.Application, TokenType.Simple> tokenProxy = authenticationService .authenticateApplication(new ApiKey(apiKey), application, domain, TokenType.Simple.INSTANCE); final HodAuthentication authentication = mock(HodAuthentication.class); final HodAuthenticationPrincipal hodAuthenticationPrincipal = mock(HodAuthenticationPrincipal.class); final ResourceIdentifier identifier = mock(ResourceIdentifier.class); when(identifier.toString()).thenReturn(application); when(identifier.getDomain()).thenReturn(domain); when(hodAuthenticationPrincipal.getApplication()).thenReturn(identifier); when(authentication.getPrincipal()).thenReturn(hodAuthenticationPrincipal); //noinspection unchecked,rawtypes when(authentication.getTokenProxy()).thenReturn((TokenProxy) tokenProxy); final SecurityContext securityContext = mock(SecurityContext.class); when(securityContext.getAuthentication()).thenReturn(authentication); SecurityContextHolder.setContext(securityContext); }
From source file:de.iew.services.impl.TreeSketchPadServiceImplIntegrationTest.java
@Test public void testCreatePolygon() throws Exception { SecurityContext securityContext = SecurityContextHolder.getContext(); Authentication sketchPadUser = securityContext.getAuthentication(); SketchPadService sketchPadService = (SketchPadService) this.applicationContext.getBean("sketchPadService"); double[] x = new double[] { 0, 1, 2, 3 }; double[] y = new double[] { 4, 5, 6, 7 }; Polygon polygon = sketchPadService.createPolygon(sketchPadUser, 1, x, y, 1, 1); assertNotNull(polygon.getId());/*from w w w . j a v a2 s . co m*/ assertEquals(this.sketchPadDao.findById(1), polygon.getSketchPad()); assertEquals(this.sketchPadColorDao.findById(1), polygon.getLineColor()); assertEquals(this.sketchPadStrokeDao.findById(1), polygon.getStroke()); assertEquals(4, polygon.getSegments().size()); }
From source file:org.duracloud.account.app.controller.AbstractAccountController.java
/** * @return/* ww w. j a v a 2 s . c om*/ */ protected DuracloudUser getUser() throws DBNotFoundException { SecurityContext securityContext = SecurityContextHolder.getContext(); Authentication authentication = securityContext.getAuthentication(); String username = authentication.getName(); return this.userService.loadDuracloudUserByUsername(username); }
From source file:org.jasig.schedassist.web.register.delegate.DelegateRegistrationHelper.java
/** * Returns the value of the name field if {@link IDelegateCalendarAccount#getLocation()} does not return null; otherwise * returns the default value of {@link Preferences#LOCATION}. * //from w ww.j a v a 2 s. c o m * @return a {@link String} containing the current authenticated {@link IDelegateCalendarAccount} location name */ public String currentDelegateLocation() { SecurityContext context = SecurityContextHolder.getContext(); Authentication authentication = context.getAuthentication(); DelegateCalendarAccountUserDetailsImpl currentUser = (DelegateCalendarAccountUserDetailsImpl) authentication .getPrincipal(); String accountLocation = currentUser.getDelegateCalendarAccount().getLocation(); if (StringUtils.isNotBlank(accountLocation)) { return accountLocation; } else { return Preferences.LOCATION.getDefaultValue(); } }
From source file:org.verinice.persistence.Dao.java
protected void enableAccessControlFilters() { SecurityContext securityContext = SecurityContextHolder.getContext(); Account account = (Account) securityContext.getAuthentication().getPrincipal(); Session session = entityManager.unwrap(Session.class); if (account.isScoped()) { session.enableFilter("scope").setParameter("scopeId", account.getScopeId()); }//from ww w . ja v a2 s. c o m if (!account.isAdmin()) { session.enableFilter("userReadAccess").setParameterList("accountGroups", account.getAccountGroups()); } }
From source file:org.openmrs.contrib.metadatarepository.service.UserSecurityAdvice.java
/** * Method to enforce security and only allow administrators to modify users. Regular * users are allowed to modify themselves. * * @param method the name of the method executed * @param args the arguments to the method * @param target the target class/* w ww.j av a2 s . co m*/ * @throws Throwable thrown when args[0] is null or not a User object */ public void before(Method method, Object[] args, Object target) throws Throwable { SecurityContext ctx = SecurityContextHolder.getContext(); if (ctx.getAuthentication() != null) { Authentication auth = ctx.getAuthentication(); boolean administrator = false; Collection<GrantedAuthority> roles = auth.getAuthorities(); for (GrantedAuthority role1 : roles) { if (role1.getAuthority().equals(Constants.ADMIN_ROLE)) { administrator = true; break; } } User user = (User) args[0]; AuthenticationTrustResolver resolver = new AuthenticationTrustResolverImpl(); // allow new users to signup - this is OK b/c Signup doesn't allow setting of roles boolean signupUser = resolver.isAnonymous(auth); if (!signupUser) { User currentUser = getCurrentUser(auth); if (user.getId() != null && !user.getId().equals(currentUser.getId()) && !administrator) { log.warn("Access Denied: '" + currentUser.getUsername() + "' tried to modify '" + user.getUsername() + "'!"); throw new AccessDeniedException(ACCESS_DENIED); } else if (user.getId() != null && user.getId().equals(currentUser.getId()) && !administrator) { // get the list of roles the user is trying add Set<String> userRoles = new HashSet<String>(); if (user.getRoles() != null) { for (Object o : user.getRoles()) { Role role = (Role) o; userRoles.add(role.getName()); } } // get the list of roles the user currently has Set<String> authorizedRoles = new HashSet<String>(); for (GrantedAuthority role : roles) { authorizedRoles.add(role.getAuthority()); } // if they don't match - access denied // regular users aren't allowed to change their roles if (!CollectionUtils.isEqualCollection(userRoles, authorizedRoles)) { log.warn("Access Denied: '" + currentUser.getUsername() + "' tried to change their role(s)!"); throw new AccessDeniedException(ACCESS_DENIED); } } } else { if (log.isDebugEnabled()) { log.debug("Registering new user '" + user.getUsername() + "'"); } } } }