List of usage examples for org.springframework.security.core.context SecurityContext setAuthentication
void setAuthentication(Authentication authentication);
From source file:de.fau.amos4.test.integration.helper.security.WithMockCustomUserSecurityContextFactory.java
@Override public SecurityContext createSecurityContext(WithMockCustomUser customUser) { SecurityContext context = SecurityContextHolder.createEmptyContext(); // A empty user that is NOT in the database. Client mockClient = createClient(customUser); CurrentClient principal = new CurrentClient(mockClient); Authentication auth = new UsernamePasswordAuthenticationToken(principal, principal.getPassword(), principal.getAuthorities()); context.setAuthentication(auth); return context; }
From source file:samples.WithCustomUserSecurityContextFactory.java
public SecurityContext createSecurityContext(WithCustomUser customUser) { SecurityContext context = SecurityContextHolder.createEmptyContext(); User principal = new User(); principal.setEmail(customUser.email()); principal.setFirstName(customUser.firstName()); principal.setLastName(customUser.lastName()); principal.setId(customUser.id());/*from w w w . ja va 2 s . co m*/ Authentication auth = new UsernamePasswordAuthenticationToken(principal, "password", AuthorityUtils.createAuthorityList("ROLE_USER")); context.setAuthentication(auth); return context; }
From source file:com.codesolid.goalboost.social.SimpleSignInAdapter.java
@Override public String signIn(String localUserId, Connection<?> connection, NativeWebRequest request) { Logger logger = LoggerFactory.getLogger(SimpleSignInAdapter.class); logger.info(//from w w w. ja v a 2 s.c o m "Inside SimpleSignInAdapter.signIn, localUserId = " + localUserId == null ? "NULL" : localUserId); logger.info("Getting security context."); SecurityContext context = SecurityContextHolder.getContext(); logger.info("Calling loadUserByUserName."); UserDetails user = siteUserDetailsService.loadUserByUsername(localUserId); logger.info("Creating UserPasswordAuthenticationToken."); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, null, null); logger.info("Setting token on context"); context.setAuthentication(token); logger.info("Returning null"); return null; //return extractOriginalUrl(request); }
From source file:org.homiefund.test.config.SecurityConextPrincipalFactory.java
@Override public SecurityContext createSecurityContext(WithUser withUser) { SecurityContext context = SecurityContextHolder.createEmptyContext(); UserDTO user = new UserDTO(); user.setId(withUser.id());/*from w w w .j a va2 s .co m*/ user.setHomes(Stream.of(withUser.homes()).map(h -> { HomeDTO home = new HomeDTO(); home.setId(h.id()); return home; }).collect(Collectors.toList())); Authentication auth = new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities()); context.setAuthentication(auth); return context; }
From source file:org.brekka.pegasus.core.security.UnlockAuthenticationProvider.java
@Override protected UserDetails retrieveUser(final String token, final UsernamePasswordAuthenticationToken authentication) throws AuthenticationException { Object credentials = authentication.getCredentials(); String password = credentials.toString(); if (StringUtils.isBlank(password)) { throw new BadCredentialsException("A code is required"); }//ww w.j av a2 s . c o m AnonymousTransferUser anonymousTransferUser = new AnonymousTransferUser(token); SecurityContext context = SecurityContextHolder.getContext(); // Temporarily bind the authentication user to the security context so that we can do the unlock // this is primarily for the EventService to capture the IP/remote user. UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(anonymousTransferUser, anonymousTransferUser); auth.setDetails(authentication.getDetails()); try { context.setAuthentication(auth); anonymousService.unlock(token, password, true); context.setAuthentication(null); return anonymousTransferUser; } catch (PhalanxException e) { if (e.getErrorCode() == PhalanxErrorCode.CP302) { throw new BadCredentialsException("Code appears to be incorrect"); } throw e; } }
From source file:org.alfresco.jive.community.ws.legacy.AlfrescoService.java
@GET @Path("/spaces") public EntityCollection<SpaceEntity> getSpaces(@QueryParam("offset") @DefaultValue("0") int offset, @QueryParam("limit") @DefaultValue("25") int limit, @HeaderParam(PARAM_USER) String user) { SecurityContext sc = SecurityContextHolder.getContext(); Authentication auth = sc.getAuthentication(); try {/*from ww w .j ava 2s . co m*/ User jiveUser = userManager.getUser(encrypter.decrypt(user)); sc.setAuthentication(new JiveUserAuthentication(jiveUser)); Paginator paginator = paginationHelper.getPaginator(offset, limit); return attachPagination(paginator, spaceProvider.getSubSpaces(rootSpace, offset, limit)); } catch (CannotDecryptException e) { throw OpenClientErrorBuilder.forbidden("Cannot decrypt user value"); } catch (UserNotFoundException e) { throw OpenClientErrorBuilder.forbidden("No user specified or specified user does not exist"); } finally { sc.setAuthentication(auth); } }
From source file:org.ff4j.security.test.FlipSecurityTests.java
@Before public void setUp() throws Exception { securityCtx = SecurityContextHolder.getContext(); // Init SpringSecurity Context SecurityContext context = new SecurityContextImpl(); List<GrantedAuthority> listOfRoles = new ArrayList<GrantedAuthority>(); listOfRoles.add(new SimpleGrantedAuthority("ROLE_USER")); User u1 = new User("user1", "user1", true, true, true, true, listOfRoles); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(u1.getUsername(), u1.getPassword(), u1.getAuthorities()); token.setDetails(u1);/*from ww w. ja v a 2 s . c o m*/ context.setAuthentication(token); SecurityContextHolder.setContext(context); // <-- ff4j = new FF4j("test-ff4j-security-spring.xml"); ff4j.setAuthorizationsManager(new SpringSecurityAuthorisationManager()); }
From source file:cn.org.once.cstack.modules.AbstractModuleControllerTestIT.java
@Before public void setUp() throws Exception { logger.info("setup"); applicationName = "app" + new Random().nextInt(100000); this.mockMvc = MockMvcBuilders.webAppContextSetup(context).addFilters(springSecurityFilterChain).build(); User user = null;//from w ww. j av a 2 s.c o m try { user = userService.findByLogin("johndoe"); } catch (ServiceException e) { logger.error(e.getLocalizedMessage()); } assert user != null; Authentication authentication = new UsernamePasswordAuthenticationToken(user.getLogin(), user.getPassword()); Authentication result = authenticationManager.authenticate(authentication); SecurityContext securityContext = SecurityContextHolder.getContext(); securityContext.setAuthentication(result); session = new MockHttpSession(); String secContextAttr = HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY; session.setAttribute(secContextAttr, securityContext); // create an application server String jsonString = "{\"applicationName\":\"" + applicationName + "\", \"serverName\":\"" + server + "\"}"; mockMvc.perform( post("/application").session(session).contentType(MediaType.APPLICATION_JSON).content(jsonString)) .andExpect(status().isOk()); }