List of usage examples for org.springframework.security.core.context SecurityContextHolder getContext
public static SecurityContext getContext()
SecurityContext
. From source file:org.jtalks.poulpe.security.AclAuthorizationStrategyImpl.java
/** * Method checks that SecurityContextHolder contains authenticated principal. * * @param acl access control list (is not used, may be null). * @param changeType AclAuthorizationStrategy change type constant (is not used, may be null). *//* www. j av a2 s . c o m*/ @Override public void securityCheck(Acl acl, int changeType) { if ((SecurityContextHolder.getContext() == null) || (SecurityContextHolder.getContext().getAuthentication() == null) || !SecurityContextHolder.getContext().getAuthentication().isAuthenticated()) { throw new AccessDeniedException("Authenticated principal required to operate with ACLs"); } }
From source file:org.web4thejob.security.CustomELRequestMatcherContext.java
public boolean isCredentialsExpired() { if (SecurityContextHolder.getContext().getAuthentication() != null && SecurityContextHolder.getContext().getAuthentication().isAuthenticated() && SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof UserDetailsEx) { return true; } else if (ContextUtil.getBean(CredentialsExpiredErrorHandler.class).getExpiredUserName() != null) { CredentialsExpiredErrorHandler ex = ContextUtil.getBean(CredentialsExpiredErrorHandler.class); request.getSession().setAttribute(SecurityService.EXPIRED_USER_NAME, ContextUtil.getBean(CredentialsExpiredErrorHandler.class).getExpiredUserName()); ex.setExpiredUserName(null);/*from w ww . j a va 2 s .c o m*/ return true; } return false; }
From source file:com.amediamanager.controller.MainController.java
@RequestMapping(value = { "/", "/home", "/welcome" }, method = RequestMethod.GET) public String home(ModelMap model, HttpSession session) { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); // If the user is not authenticated, show a different view if (auth instanceof AnonymousAuthenticationToken) { model.addAttribute("newUser", new NewUser()); model.addAttribute("templateName", "welcome"); } else {// w w w. j a v a 2 s . c o m List<Video> videos = new ArrayList<Video>(); List<TagCount> tags = new ArrayList<TagCount>(); try { // Get user's videos and tags videos = videoService.findByUserId(auth.getName()); tags = tagService.getTagsForUser(auth.getName()); // Add expiring URLs (1 hour) videos = videoService.generateExpiringUrls(videos, 1000 * 60 * 60); } catch (Exception e) { LOG.error("Error loading videos: {}", e); return "redirect:/config"; } model.addAttribute("tags", tags); model.addAttribute("videos", videos); model.addAttribute("templateName", "only_videos"); } return "base"; }
From source file:com.ateam.login.UserSession.java
private boolean isAuthenticated() { boolean result = false; SecurityContext context = SecurityContextHolder.getContext(); if (context instanceof SecurityContext) { Authentication authentication = context.getAuthentication(); if (authentication instanceof AnonymousAuthenticationToken) { // not authenticated } else if (authentication instanceof Authentication) { result = true;/* w w w. java 2s . com*/ } } return result; }
From source file:cn.cuizuoli.gotour.resolver.UserInfoMethodArgumentResolver.java
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { Class<?> paramType = parameter.getParameterType(); if (User.class.isAssignableFrom(paramType)) { SecurityContext securityContext = SecurityContextHolder.getContext(); Authentication authentication = securityContext.getAuthentication(); if (authentication != null) { Object principal = authentication.getPrincipal(); if (principal instanceof User) { return (User) principal; }//from w w w . jav a 2s.c o m } } return null; }
From source file:de.sainth.recipe.backend.rest.controller.CookbookController.java
@Secured({ "ROLE_USER", "ROLE_ADMIN" }) @RequestMapping()// ww w . j av a 2s. c om HttpEntity<List<Cookbook>> getAll() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication instanceof RecipeManagerAuthenticationToken) { RecipeManagerAuthenticationToken token = (RecipeManagerAuthenticationToken) authentication; if (ROLE_ADMIN.name().equals(token.getRole())) { return new ResponseEntity<>(repository.findAll(), HttpStatus.OK); } else { return new ResponseEntity<>(repository.findAllFor(token.getPrincipal()), HttpStatus.OK); } } return new ResponseEntity<>(HttpStatus.FORBIDDEN); }
From source file:org.ngrinder.user.service.UserContextTest.java
@Test public void testGetUser() { UserContext userCtx = new UserContext(); //in super.beforeSetSecurity(), there is an admin user is set, but the auth is invalid try {/* w w w . ja v a2s. co m*/ userCtx.getCurrentUser(); assertTrue(false); } catch (AuthenticationCredentialsNotFoundException e) { assertTrue(true); } UserDetails user = userDetailService.loadUserByUsername(getTestUser().getUserId()); Authentication oriAuth = SecurityContextHolder.getContext().getAuthentication(); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, "123"); SecurityContextHolder.getContext().setAuthentication(token); userCtx.getCurrentUser(); assertTrue(true); SecurityContextHolder.getContext().setAuthentication(oriAuth); }
From source file:org.cloudfoundry.identity.uaa.security.DefaultSecurityContextAccessor.java
@Override public boolean isAdmin() { Authentication a = SecurityContextHolder.getContext().getAuthentication(); return a != null && AuthorityUtils.authorityListToSet(a.getAuthorities()).contains("uaa.admin"); }
From source file:info.gewton.slsecurity.test.Test.java
/** * Efetua autenticao, criando um contexto do Spring Security * @param login usurio/*from w w w. j a va2s. co m*/ * @param password senha */ protected void setSecurityContext(String login, String password) { SecurityContextHolder.getContext() .setAuthentication(new UsernamePasswordAuthenticationToken(login, password)); }
From source file:org.zalando.stups.oauth2.spring.client.AccessTokenUtilsTest.java
@Test public void testMissingUserDetails() throws Exception { SecurityContextHolder.getContext() .setAuthentication(new OAuth2Authentication(mock(OAuth2Request.class), mock(Authentication.class))); assertThat(getAccessTokenFromSecurityContext().isPresent()).isFalse(); }