List of usage examples for org.springframework.security.core.context SecurityContext setAuthentication
void setAuthentication(Authentication authentication);
From source file:org.jasig.springframework.security.portlet.context.PortletSessionSecurityContextRepositoryTests.java
@Test public void sessionIsCreatedAndContextStoredWhenContextChanges() throws Exception { PortletSessionSecurityContextRepository repo = new PortletSessionSecurityContextRepository(); MockPortletRequest request = new MockPortletRequest(); MockPortletResponse response = new MockPortletResponse(); PortletRequestResponseHolder holder = new PortletRequestResponseHolder(request, response); SecurityContext context = repo.loadContext(holder); assertNull(request.getPortletSession(false)); // Simulate authentication during the request context.setAuthentication(testToken); repo.saveContext(context, holder);//from w w w . j a v a 2s . c o m assertNotNull(request.getPortletSession(false)); assertEquals(context, request.getPortletSession().getAttribute(SPRING_SECURITY_CONTEXT_KEY, PortletSession.APPLICATION_SCOPE)); }
From source file:net.d53.syman.web.controller.PatientController.java
@RequestMapping(value = "/api/auth", method = RequestMethod.POST) @ResponseBody/*from w ww.ja va 2 s .co m*/ public String APIauthenticate(@RequestParam String username, @RequestParam String password, HttpServletRequest request, HttpServletResponse response) { String token = null; UsernamePasswordAuthenticationToken authenticationRequest = new UsernamePasswordAuthenticationToken( username, password); authenticationRequest.setDetails(APIAuthenticationToken.API_TOKEN_IDENTIFIER); try { APIAuthenticationToken res = (APIAuthenticationToken) authenticationManager .authenticate(authenticationRequest); LOGGER.info(ToStringBuilder.reflectionToString(res)); if (res != null) { token = res.getCredentials().toString(); LOGGER.info("Generated token " + token); SecurityContext context = SecurityContextHolder.getContext(); context.setAuthentication(res); this.securityContextRepository.saveContext(context, request, response); } else { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); } } catch (AuthenticationException e) { LOGGER.info("Authentication error: " + e.getMessage()); SecurityContextHolder.clearContext(); response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); } return token; }
From source file:utils.AbstractTest.java
public void authenticate(String username) { UserDetails userDetails;// w ww. jav a 2s. co m TestingAuthenticationToken authenticationToken; SecurityContext context; if (username == null) authenticationToken = null; else { userDetails = loginService.loadUserByUsername(username); authenticationToken = new TestingAuthenticationToken(userDetails, null); } context = SecurityContextHolder.getContext(); context.setAuthentication(authenticationToken); }
From source file:utilities.AbstractTest.java
public void authenticate(final String username) { UserDetails userDetails;// w w w.j a v a 2 s . c om TestingAuthenticationToken authenticationToken; SecurityContext context; if (username == null) authenticationToken = null; else { userDetails = this.loginService.loadUserByUsername(username); authenticationToken = new TestingAuthenticationToken(userDetails, null); } context = SecurityContextHolder.getContext(); context.setAuthentication(authenticationToken); }
From source file:fr.treeptik.cloudunit.modules.redis.SpringBootRedisModuleControllerTestIT.java
@Before public void setup() { logger.info("setup"); this.mockMvc = MockMvcBuilders.webAppContextSetup(context).addFilters(springSecurityFilterChain).build(); User user = null;/* w w w.j a v a 2s . co 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); }
From source file:org.vaadin.spring.security.internal.VaadinManagedSecurity.java
@Override public Authentication login(Authentication authentication, boolean rememberMe) throws AuthenticationException { if (rememberMe) { throw new UnsupportedOperationException( "Remember Me is currently not supported when using managed security"); }//www . j a v a2 s . co m SecurityContext context = SecurityContextHolder.getContext(); logger.debug("Authenticating using {}, rememberMe = {}", authentication, rememberMe); final Authentication fullyAuthenticated = getAuthenticationManager().authenticate(authentication); logger.debug("Setting authentication of context {} to {}", context, fullyAuthenticated); context.setAuthentication(fullyAuthenticated); return fullyAuthenticated; }
From source file:fr.treeptik.cloudunit.alias.AliasControllerTestIT.java
@Before public void setup() { logger.info("setup"); this.mockMvc = MockMvcBuilders.webAppContextSetup(context).addFilters(springSecurityFilterChain).build(); User user = null;/*from ww w.j a v a 2s.c o m*/ try { user = userService.findByLogin("johndoe"); } catch (ServiceException e) { logger.error(e.getLocalizedMessage()); } Authentication authentication = new UsernamePasswordAuthenticationToken(user.getLogin(), user.getPassword()); Authentication result = authenticationManager.authenticate(authentication); SecurityContext securityContext = SecurityContextHolder.getContext(); securityContext.setAuthentication(result); session = new MockHttpSession(); session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, securityContext); if (!isAppCreated) { try { logger.info("**********************************"); logger.info(" Create Tomcat server "); logger.info("**********************************"); String jsonString = "{\"applicationName\":\"" + applicationName1 + "\", \"serverName\":\"" + release + "\"}"; ResultActions resultats = mockMvc.perform(post("/application").session(session) .contentType(MediaType.APPLICATION_JSON).content(jsonString)); resultats.andExpect(status().isOk()); logger.info("Create Tomcat server"); jsonString = "{\"applicationName\":\"" + applicationName2 + "\", \"serverName\":\"" + release + "\"}"; resultats = mockMvc.perform(post("/application").session(session) .contentType(MediaType.APPLICATION_JSON).content(jsonString)); resultats.andExpect(status().isOk()); isAppCreated = true; } catch (Exception e) { logger.error(e.getMessage()); } } }
From source file:com.pamarin.income.security.DefaultBasicAuthenImpl.java
/** * @param username/* ww w . ja v a 2 s .c om*/ * @param password */ @Override public void login(String username, String password) throws UsernameNotFoundException { SecurityContext securityContext = SecurityContextHolder.getContext(); //check authen from user details service UserDetails userDetails = detailsService.loadUserByUsername(username); Authentication authentication = manager .authenticate(new UsernamePasswordAuthenticationToken(userDetails, password)); //keep authentication to security context securityContext.setAuthentication(authentication); }
From source file:net.shibboleth.idp.oidc.flow.PreAuthorizeUserApprovalAction.java
/** * Store spring security authentication context. * * @param profileRequestContext the profile request context * @param springRequestContext the spring request context * @param authentication the authentication *//* w w w .j av a2 s.co m*/ private void storeSpringSecurityAuthenticationContext( @Nonnull final ProfileRequestContext profileRequestContext, final RequestContext springRequestContext, final Authentication authentication) { final HttpServletRequest request = OIDCUtils.getHttpServletRequest(springRequestContext); if (request == null) { throw new OIDCException("HttpServletRequest cannot be null"); } final SecurityContext securityContext = SecurityContextHolder.getContext(); securityContext.setAuthentication(authentication); SecurityContextHolder.setContext(securityContext); final HttpSession session = request.getSession(); session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext); log.debug("Stored authentication [{}] into Spring security context", SecurityContextHolder.getContext().getAuthentication()); }
From source file:nz.net.orcon.kanban.security.SecurityToolImpl.java
@Override public void iAmSystem() throws Exception { SecurityContext context = SecurityContextHolder.getContext(); Collection<? extends GrantedAuthority> authorities = this.getRoles(SYSTEM); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(SYSTEM, "", authorities);//w w w .j ava 2 s. c o m context.setAuthentication(authentication); }