List of usage examples for org.springframework.security.core.context SecurityContextImpl setAuthentication
@Override public void setAuthentication(Authentication authentication)
From source file:de.blizzy.documentr.TestUtil.java
public static SecurityContext createSecurityContext(Authentication authentication) { SecurityContextImpl context = new SecurityContextImpl(); context.setAuthentication(authentication); return context; }
From source file:com.apress.prospringintegration.security.SecurityMain.java
@SuppressWarnings("unchecked") public static SecurityContext createContext(String username, String password, String... roles) { SecurityContextImpl ctxImpl = new SecurityContextImpl(); UsernamePasswordAuthenticationToken authToken; if (roles != null && roles.length > 0) { GrantedAuthority[] authorities = new GrantedAuthority[roles.length]; for (int i = 0; i < roles.length; i++) { authorities[i] = new GrantedAuthorityImpl(roles[i]); }/*from ww w . j a v a 2 s. c o m*/ authToken = new UsernamePasswordAuthenticationToken(username, password, CollectionUtils.arrayToList(authorities)); } else { authToken = new UsernamePasswordAuthenticationToken(username, password); } ctxImpl.setAuthentication(authToken); return ctxImpl; }
From source file:com.mtt.myapp.AbstractSystemTransactionalTest.java
@Before public void beforeSetSecurity() { UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("admin", null); SecurityContextImpl context = new SecurityContextImpl(); context.setAuthentication(token); SecurityContextHolder.setContext(context); }
From source file:com.khs.sherpa.spring.SpringAuthentication.java
public String[] authenticate(String username, String password, HttpServletRequest request, HttpServletResponse response) {/*w ww . j a va 2 s . c o m*/ UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password); Authentication authentication = null; try { authentication = authenticationManager.authenticate(token); } catch (AuthenticationException e) { throw new SherpaInvalidUsernamePassword("username and/or password is incorrect"); } if (authentication.isAuthenticated() == false) { throw new SherpaInvalidUsernamePassword("username and/or password is incorrect"); } List<String> roles = new ArrayList<String>(); for (GrantedAuthority auth : authentication.getAuthorities()) { roles.add(auth.getAuthority()); } SecurityContextImpl context = new SecurityContextImpl(); context.setAuthentication(authentication); SecurityContextHolder.setContext(context); request.getSession().setAttribute("SPRING_SECURITY_CONTEXT_KEY", context); return roles.toArray(new String[roles.size()]); }
From source file:org.cloudfoundry.identity.uaa.authentication.manager.LoginAuthenticationManagerTests.java
@Before public void init() { manager.setApplicationEventPublisher(Mockito.mock(ApplicationEventPublisher.class)); manager.setUserDatabase(userDatabase); oauth2Authentication = new OAuth2Authentication( new DefaultAuthorizationRequest("client", Arrays.asList("read", "write")), null); SecurityContextImpl context = new SecurityContextImpl(); context.setAuthentication(oauth2Authentication); SecurityContextHolder.setContext(context); }
From source file:architecture.ee.web.community.struts2.action.support.SocialCallbackSupport.java
private void createSecurityContext(User userToUse) { if (userToUse.getUserId() > 0) { ExtendedUserDetailsService detailsService = getComponent(ExtendedUserDetailsService.class); UserDetails details = detailsService.loadUserByUsername(userToUse.getUsername()); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(details, null, details.getAuthorities()); SecurityContextImpl context = new SecurityContextImpl(); context.setAuthentication(authentication); SecurityContextHolder.setContext(context); HttpSession httpsession = request.getSession(true); httpsession.setAttribute("SPRING_SECURITY_CONTEXT", context); }/*from w w w . ja v a2 s.c om*/ }
From source file:org.ngrinder.security.NGrinderAuthenticationProviderTest.java
@Test public void testAdditionalAuthenticationChecks() { UserDetails user = userDetailService.loadUserByUsername(getTestUser().getUserId()); //remove authentication temporally Authentication oriAuth = SecurityContextHolder.getContext().getAuthentication(); SecurityContextImpl context = new SecurityContextImpl(); SecurityContextHolder.setContext(context); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("admin", null); try {/*from www . j a v a 2 s . c o m*/ provider.additionalAuthenticationChecks(user, token); assertTrue(false); } catch (BadCredentialsException e) { assertTrue(true); } token = new UsernamePasswordAuthenticationToken("TEST_USER", "123"); provider.additionalAuthenticationChecks(user, token); context.setAuthentication(oriAuth); }
From source file:architecture.ee.web.community.spring.controller.SocialConnectController.java
private void createSecurityContext(User userToUse, HttpServletRequest request) { if (userToUse.getUserId() > 0) { UserDetails details = userDetailsService.loadUserByUsername(userToUse.getUsername()); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(details, null, details.getAuthorities()); SecurityContextImpl context = new SecurityContextImpl(); context.setAuthentication(authentication); SecurityContextHolder.setContext(context); HttpSession httpsession = request.getSession(true); httpsession.setAttribute("SPRING_SECURITY_CONTEXT", context); }//from www .j ava 2 s . co m }
From source file:org.orcid.api.t2.server.delegator.T2OrcidApiServiceVersionedDelegatorTest.java
private void setUpSecurityContextForClientOnly(String clientId, Set<String> scopes) { SecurityContextImpl securityContext = new SecurityContextImpl(); OrcidOAuth2Authentication mockedAuthentication = mock(OrcidOAuth2Authentication.class); securityContext.setAuthentication(mockedAuthentication); SecurityContextHolder.setContext(securityContext); when(mockedAuthentication.getPrincipal()).thenReturn(new ProfileEntity(clientId)); when(mockedAuthentication.isClientOnly()).thenReturn(true); OAuth2Request authorizationRequest = new OAuth2Request(Collections.<String, String>emptyMap(), clientId, Collections.<GrantedAuthority>emptyList(), true, scopes, Collections.<String>emptySet(), null, Collections.<String>emptySet(), Collections.<String, Serializable>emptyMap()); when(mockedAuthentication.getOAuth2Request()).thenReturn(authorizationRequest); }
From source file:org.orcid.api.t2.server.delegator.T2OrcidApiServiceVersionedDelegatorTest.java
private void setUpSecurityContext(String userOrcid) { SecurityContextImpl securityContext = new SecurityContextImpl(); OrcidOAuth2Authentication mockedAuthentication = mock(OrcidOAuth2Authentication.class); securityContext.setAuthentication(mockedAuthentication); SecurityContextHolder.setContext(securityContext); when(mockedAuthentication.getPrincipal()).thenReturn(new ProfileEntity(userOrcid)); Set<String> scopes = new HashSet<String>(); scopes.add(ScopePathType.ACTIVITIES_UPDATE.value()); scopes.add(ScopePathType.READ_LIMITED.value()); OAuth2Request authorizationRequest = new OAuth2Request(Collections.<String, String>emptyMap(), userOrcid, Collections.<GrantedAuthority>emptyList(), true, scopes, Collections.<String>emptySet(), null, Collections.<String>emptySet(), Collections.<String, Serializable>emptyMap()); when(mockedAuthentication.getOAuth2Request()).thenReturn(authorizationRequest); }