Search in sources :

Example 31 with AnonymousAuthenticationToken

use of org.springframework.security.authentication.AnonymousAuthenticationToken in project spring-security by spring-projects.

the class SpringSecurityAuthenticationSourceTests method principalIsEmptyForAnonymousUser.

@Test
public void principalIsEmptyForAnonymousUser() {
    AuthenticationSource source = new SpringSecurityAuthenticationSource();
    SecurityContextHolder.getContext().setAuthentication(new AnonymousAuthenticationToken("key", "anonUser", AuthorityUtils.createAuthorityList("ignored")));
    assertThat(source.getPrincipal()).isEqualTo("");
}
Also used : SpringSecurityAuthenticationSource(org.springframework.security.ldap.authentication.SpringSecurityAuthenticationSource) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) AuthenticationSource(org.springframework.ldap.core.AuthenticationSource) SpringSecurityAuthenticationSource(org.springframework.security.ldap.authentication.SpringSecurityAuthenticationSource) Test(org.junit.Test)

Example 32 with AnonymousAuthenticationToken

use of org.springframework.security.authentication.AnonymousAuthenticationToken in project spring-security by spring-projects.

the class AuthenticationSimpleHttpInvokerRequestExecutorTests method testNullContextHolderWhenAnonymous.

// SEC-1975
@Test
public void testNullContextHolderWhenAnonymous() throws Exception {
    AnonymousAuthenticationToken anonymous = new AnonymousAuthenticationToken("key", "principal", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
    SecurityContextHolder.getContext().setAuthentication(anonymous);
    // Create a connection and ensure our executor sets its
    // properties correctly
    AuthenticationSimpleHttpInvokerRequestExecutor executor = new AuthenticationSimpleHttpInvokerRequestExecutor();
    HttpURLConnection conn = new MockHttpURLConnection(new URL("http://localhost/"));
    executor.prepareConnection(conn, 10);
    // Check connection properties (shouldn't be an Authorization header)
    assertThat(conn.getRequestProperty("Authorization")).isNull();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) URL(java.net.URL) Test(org.junit.Test)

Example 33 with AnonymousAuthenticationToken

use of org.springframework.security.authentication.AnonymousAuthenticationToken in project spring-security by spring-projects.

the class SecurityContextChannelInterceptorTests method assertAnonymous.

private void assertAnonymous() {
    Authentication currentAuthentication = SecurityContextHolder.getContext().getAuthentication();
    assertThat(currentAuthentication).isInstanceOf(AnonymousAuthenticationToken.class);
    AnonymousAuthenticationToken anonymous = (AnonymousAuthenticationToken) currentAuthentication;
    assertThat(anonymous.getName()).isEqualTo(expectedAnonymous.getName());
    assertThat(anonymous.getAuthorities()).containsOnlyElementsOf(expectedAnonymous.getAuthorities());
    assertThat(anonymous.getKeyHash()).isEqualTo(expectedAnonymous.getKeyHash());
}
Also used : Authentication(org.springframework.security.core.Authentication) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken)

Example 34 with AnonymousAuthenticationToken

use of org.springframework.security.authentication.AnonymousAuthenticationToken in project spring-security by spring-projects.

the class DefaultMessageSecurityExpressionHandlerTests method setup.

@Before
public void setup() {
    handler = new DefaultMessageSecurityExpressionHandler<Object>();
    message = new GenericMessage<Object>("");
    authentication = new AnonymousAuthenticationToken("key", "anonymous", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
}
Also used : AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) Before(org.junit.Before)

Example 35 with AnonymousAuthenticationToken

use of org.springframework.security.authentication.AnonymousAuthenticationToken in project spring-security by spring-projects.

the class SecurityContextChannelInterceptorTests method setup.

@Before
public void setup() {
    authentication = new TestingAuthenticationToken("user", "pass", "ROLE_USER");
    messageBuilder = MessageBuilder.withPayload("payload");
    expectedAnonymous = new AnonymousAuthenticationToken("key", "anonymous", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
    interceptor = new SecurityContextChannelInterceptor();
}
Also used : SecurityContextChannelInterceptor(org.springframework.security.messaging.context.SecurityContextChannelInterceptor) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) Before(org.junit.Before)

Aggregations

AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)42 Test (org.junit.Test)20 Authentication (org.springframework.security.core.Authentication)15 GrantedAuthority (org.springframework.security.core.GrantedAuthority)8 ArrayList (java.util.ArrayList)7 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)7 Before (org.junit.Before)6 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)5 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)5 SecurityContext (org.springframework.security.core.context.SecurityContext)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 AccessDeniedException (org.springframework.security.access.AccessDeniedException)2 AnonymousAuthenticationProvider (org.springframework.security.authentication.AnonymousAuthenticationProvider)2 User (org.springframework.security.core.userdetails.User)2 UserDetails (org.springframework.security.core.userdetails.UserDetails)2 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)2 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)2