Java Code Examples for org.springframework.security.core.context.SecurityContextImpl#setAuthentication()

The following examples show how to use org.springframework.security.core.context.SecurityContextImpl#setAuthentication() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LDSecurityContextRepository.java    From document-management-software with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public SecurityContext loadContext(HttpRequestResponseHolder request) {
	String sid = SessionManager.get().getSessionId(request.getRequest());
	if (sid == null || !SessionManager.get().isOpen(sid))
		sid = null;

	if (sid == null)
		return SecurityContextHolder.createEmptyContext();

	Session session = SessionManager.get().get(sid);

	LDAuthenticationToken token = new LDAuthenticationToken(session.getUsername(), "", null);
	token.setSid(sid);

	SecurityContextImpl context = new SecurityContextImpl();
	context.setAuthentication(token);

	HttpSession servletSession = request.getRequest().getSession(false);
	if (servletSession != null)
		servletSessionMapping.put(sid, servletSession);

	return context;
}
 
Example 2
Source File: AuditSetUpControllerTest.java    From Asqatasun with GNU Affero General Public License v3.0 6 votes vote down vote up
private void setUpMockAuthenticationContext(){
    // initialise the context with the user identified by the email 
    // "[email protected]" seen as authenticated
    
    Collection<GrantedAuthority> gac = new ArrayList<GrantedAuthority>();
    TgolUserDetails tud = new TgolUserDetails("[email protected]", "", true, false, true, true, gac, mockUser);
    
    mockAuthentication = createMock(Authentication.class);
    SecurityContextImpl securityContextImpl = new SecurityContextImpl();
    securityContextImpl.setAuthentication(mockAuthentication);
    SecurityContextHolder.setContext(securityContextImpl);
    
    expect(mockAuthentication.getName()).andReturn("[email protected]").anyTimes();
    expect(mockAuthentication.getPrincipal()).andReturn(tud).anyTimes();
    expect(mockAuthentication.getAuthorities()).andReturn(null).anyTimes();
    
    replay(mockAuthentication);
    
    mockAuthenticationDetails = createMock(AuthenticationDetails.class);
    expect(mockAuthenticationDetails.getContext()).andReturn("[email protected]").anyTimes();
    replay(mockAuthenticationDetails);
}
 
Example 3
Source File: HomeControllerTest.java    From Asqatasun with GNU Affero General Public License v3.0 6 votes vote down vote up
private void setUpMockAuthenticationContext(){
    // initialise the context with the user identified by the email 
    // "[email protected]" seen as authenticated
    Collection<GrantedAuthority> gac = new ArrayList<GrantedAuthority>();
    TgolUserDetails tud = new TgolUserDetails("[email protected]", "", true, false, true, true, gac, mockUser);
    
    mockAuthentication = createMock(Authentication.class);
    SecurityContextImpl securityContextImpl = new SecurityContextImpl();
    securityContextImpl.setAuthentication(mockAuthentication);
    SecurityContextHolder.setContext(securityContextImpl);
    
    expect(mockAuthentication.getName()).andReturn("[email protected]").anyTimes();
    expect(mockAuthentication.getPrincipal()).andReturn(tud).anyTimes();
    expect(mockAuthentication.getAuthorities()).andReturn(null).anyTimes();
    replay(mockAuthentication);
    
    mockAuthenticationDetails = createMock(AuthenticationDetails.class);
    expect(mockAuthenticationDetails.getContext()).andReturn("[email protected]").anyTimes();
    replay(mockAuthenticationDetails);
}
 
Example 4
Source File: ContractControllerTest.java    From Asqatasun with GNU Affero General Public License v3.0 6 votes vote down vote up
private void setUpMockAuthenticationContext(){
    // initialise the context with the user identified by the email 
    // "[email protected]" seen as authenticated
    Collection<GrantedAuthority> gac = new ArrayList();
    TgolUserDetails tud = new TgolUserDetails("[email protected]", "", true, false, true, true, gac, mockUser);
    
    mockAuthentication = createMock(Authentication.class);
    SecurityContextImpl securityContextImpl = new SecurityContextImpl();
    securityContextImpl.setAuthentication(mockAuthentication);
    SecurityContextHolder.setContext(securityContextImpl);
    expect(mockAuthentication.getName()).andReturn("[email protected]").anyTimes();
    expect(mockAuthentication.getPrincipal()).andReturn(tud).anyTimes();
    expect(mockAuthentication.getAuthorities()).andReturn(null).anyTimes();
    replay(mockAuthentication);
    
    mockAuthenticationDetails = createMock(AuthenticationDetails.class);
    expect(mockAuthenticationDetails.getContext()).andReturn("[email protected]").anyTimes();
    replay(mockAuthenticationDetails);
}
 
Example 5
Source File: UserManagementControllerTest.java    From Asqatasun with GNU Affero General Public License v3.0 6 votes vote down vote up
private void setUpMockAuthenticationContext(){
    // initialise the context with the user identified by the email 
    // "[email protected]" seen as authenticated
    Collection<GrantedAuthority> gac = new ArrayList<GrantedAuthority>();
    TgolUserDetails tud = new TgolUserDetails("[email protected]", "", true, false, true, true, gac, mockAdminUser);
    mockAuthentication = createMock(Authentication.class);
    
    SecurityContextImpl securityContextImpl = new SecurityContextImpl();
    securityContextImpl.setAuthentication(mockAuthentication);
    SecurityContextHolder.setContext(securityContextImpl);
    
    expect(mockAuthentication.getName()).andReturn("[email protected]").anyTimes();
    expect(mockAuthentication.getPrincipal()).andReturn(tud).anyTimes();
    expect(mockAuthentication.getAuthorities()).andReturn(null).anyTimes();
    
    replay(mockAuthentication);
}
 
Example 6
Source File: PageListControllerTest.java    From Asqatasun with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 
 */
private void setUpMockAuthenticationContext(){
    // initialise the context with the user identified by the email 
    // "[email protected]" seen as authenticated
    Collection<GrantedAuthority> gac = new ArrayList();
    TgolUserDetails tud = new TgolUserDetails("[email protected]", "", true, false, true, true, gac, mockUser);
    mockAuthentication = createMock(Authentication.class);
    SecurityContextImpl securityContextImpl = new SecurityContextImpl();
    securityContextImpl.setAuthentication(mockAuthentication);
    SecurityContextHolder.setContext(securityContextImpl);
    expect(mockAuthentication.getName()).andReturn("[email protected]").anyTimes();
    expect(mockAuthentication.getPrincipal()).andReturn(tud).anyTimes();
    expect(mockAuthentication.getAuthorities()).andReturn(null).anyTimes();
    replay(mockAuthentication);
    
    mockAuthenticationDetails = createMock(AuthenticationDetails.class);
    expect(mockAuthenticationDetails.getContext()).andReturn("[email protected]").anyTimes();
    replay(mockAuthenticationDetails);
}
 
Example 7
Source File: SystemSecurityContext.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
private void setCustomSecurityContext(final String tenantId, final Object principal,
        final Collection<? extends GrantedAuthority> authorities) {
    final AnonymousAuthenticationToken authenticationToken = new AnonymousAuthenticationToken(
            UUID.randomUUID().toString(), principal, authorities);
    authenticationToken.setDetails(new TenantAwareAuthenticationDetails(tenantId, true));
    final SecurityContextImpl securityContextImpl = new SecurityContextImpl();
    securityContextImpl.setAuthentication(authenticationToken);
    SecurityContextHolder.setContext(securityContextImpl);
}
 
Example 8
Source File: LoginControllerTest.java    From Asqatasun with GNU Affero General Public License v3.0 5 votes vote down vote up
private void setUpMockAuthenticationContext(){
    // initialise the context with the user identified by the email 
    // "[email protected]" seen as authenticated
    
    mockAuthentication = createMock(Authentication.class);
    SecurityContextImpl securityContextImpl = new SecurityContextImpl();
    securityContextImpl.setAuthentication(mockAuthentication);
    SecurityContextHolder.setContext(securityContextImpl);
    expect(mockAuthentication.isAuthenticated()).andReturn(Boolean.FALSE);
    replay(mockAuthentication);
}
 
Example 9
Source File: SpringSessionBackedSessionRegistryTest.java    From spring-session with Apache License 2.0 5 votes vote down vote up
private Session createSession(String sessionId, String userName, Instant lastAccessed) {
	MapSession session = new MapSession(sessionId);
	session.setLastAccessedTime(lastAccessed);
	Authentication authentication = mock(Authentication.class);
	when(authentication.getName()).thenReturn(userName);
	SecurityContextImpl securityContext = new SecurityContextImpl();
	securityContext.setAuthentication(authentication);
	session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext);
	return session;
}
 
Example 10
Source File: HeaderAuthenticationFilter.java    From spring-rest-server with GNU Lesser General Public License v3.0 5 votes vote down vote up
private SecurityContext createSecurityContext(UserDetails userDetails) {
    if (userDetails != null) {
        SecurityContextImpl securityContext = new SecurityContextImpl();
        Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails.getUsername(), userDetails.getPassword(), userDetails.getAuthorities());
        securityContext.setAuthentication(authentication);
        return securityContext;
    }
    return SecurityContextHolder.createEmptyContext();
}
 
Example 11
Source File: AmqpMessageHandlerService.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
private static void setSecurityContext(final Authentication authentication) {
    final SecurityContextImpl securityContextImpl = new SecurityContextImpl();
    securityContextImpl.setAuthentication(authentication);
    SecurityContextHolder.setContext(securityContextImpl);
}
 
Example 12
Source File: SecurityContextTenantAware.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
private static SecurityContext buildSecurityContext(final String tenant) {
    final SecurityContextImpl securityContext = new SecurityContextImpl();
    securityContext.setAuthentication(
            new AuthenticationDelegate(SecurityContextHolder.getContext().getAuthentication(), tenant));
    return securityContext;
}
 
Example 13
Source File: SystemSecurityContext.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
private static void setSystemContext(final SecurityContext oldContext) {
    final Authentication oldAuthentication = oldContext.getAuthentication();
    final SecurityContextImpl securityContextImpl = new SecurityContextImpl();
    securityContextImpl.setAuthentication(new SystemCodeAuthentication(oldAuthentication));
    SecurityContextHolder.setContext(securityContextImpl);
}