Search in sources :

Example 21 with AnonymousAuthenticationToken

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

the class AnonymousAuthenticationTokenTests method testNotEqualsDueToDifferentAuthenticationClass.

@Test
public void testNotEqualsDueToDifferentAuthenticationClass() {
    AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key", "Test", ROLES_12);
    UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken("Test", "Password", ROLES_12);
    assertThat(token1.equals(token2)).isFalse();
}
Also used : UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) Test(org.junit.Test)

Example 22 with AnonymousAuthenticationToken

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

the class AnonymousAuthenticationTokenMixinTests method serializeAnonymousAuthenticationTokenMixinAfterEraseCredentialTest.

@Test
public void serializeAnonymousAuthenticationTokenMixinAfterEraseCredentialTest() throws JsonProcessingException, JSONException {
    User user = createDefaultUser();
    AnonymousAuthenticationToken token = new AnonymousAuthenticationToken(HASH_KEY, user, user.getAuthorities());
    token.eraseCredentials();
    String actualJson = mapper.writeValueAsString(token);
    JSONAssert.assertEquals(ANONYMOUS_JSON.replace(UserDeserializerTests.USER_PASSWORD, "null"), actualJson, true);
}
Also used : User(org.springframework.security.core.userdetails.User) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) Test(org.junit.Test)

Example 23 with AnonymousAuthenticationToken

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

the class AnonymousAuthenticationTokenMixinTests method deserializeAnonymousAuthenticationTokenTest.

@Test
public void deserializeAnonymousAuthenticationTokenTest() throws IOException {
    AnonymousAuthenticationToken token = mapper.readValue(ANONYMOUS_JSON, AnonymousAuthenticationToken.class);
    assertThat(token).isNotNull();
    assertThat(token.getKeyHash()).isEqualTo(HASH_KEY.hashCode());
    assertThat(token.getAuthorities()).isNotNull().hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) Test(org.junit.Test)

Example 24 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 25 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)

Aggregations

AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)40 Test (org.junit.Test)20 Authentication (org.springframework.security.core.Authentication)13 GrantedAuthority (org.springframework.security.core.GrantedAuthority)7 ArrayList (java.util.ArrayList)6 Before (org.junit.Before)6 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)6 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)5 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)3 SecurityContext (org.springframework.security.core.context.SecurityContext)3 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 SecurityQuestionsAuthenticationContext (com.evolveum.midpoint.model.api.context.SecurityQuestionsAuthenticationContext)1 PrismObject (com.evolveum.midpoint.prism.PrismObject)1