Search in sources :

Example 1 with WebAuthenticationDetails

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

the class BasicAuthenticationFilterTests method setUp.

// ~ Methods
// ========================================================================================================
@Before
public void setUp() throws Exception {
    SecurityContextHolder.clearContext();
    UsernamePasswordAuthenticationToken rodRequest = new UsernamePasswordAuthenticationToken("rod", "koala");
    rodRequest.setDetails(new WebAuthenticationDetails(new MockHttpServletRequest()));
    Authentication rod = new UsernamePasswordAuthenticationToken("rod", "koala", AuthorityUtils.createAuthorityList("ROLE_1"));
    manager = mock(AuthenticationManager.class);
    when(manager.authenticate(rodRequest)).thenReturn(rod);
    when(manager.authenticate(not(eq(rodRequest)))).thenThrow(new BadCredentialsException(""));
    filter = new BasicAuthenticationFilter(manager, new BasicAuthenticationEntryPoint());
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) WebAuthenticationDetails(org.springframework.security.web.authentication.WebAuthenticationDetails) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Before(org.junit.Before)

Example 2 with WebAuthenticationDetails

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

the class CasAuthenticationProviderTests method authenticateAllAuthenticationIsSuccessful.

@Test
public void authenticateAllAuthenticationIsSuccessful() throws Exception {
    String serviceUrl = "https://service/context";
    ServiceAuthenticationDetails details = mock(ServiceAuthenticationDetails.class);
    when(details.getServiceUrl()).thenReturn(serviceUrl);
    TicketValidator validator = mock(TicketValidator.class);
    when(validator.validate(any(String.class), any(String.class))).thenReturn(new AssertionImpl("rod"));
    ServiceProperties serviceProperties = makeServiceProperties();
    serviceProperties.setAuthenticateAllArtifacts(true);
    CasAuthenticationProvider cap = new CasAuthenticationProvider();
    cap.setAuthenticationUserDetailsService(new MockAuthoritiesPopulator());
    cap.setKey("qwerty");
    cap.setTicketValidator(validator);
    cap.setServiceProperties(serviceProperties);
    cap.afterPropertiesSet();
    String ticket = "ST-456";
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(CasAuthenticationFilter.CAS_STATELESS_IDENTIFIER, ticket);
    Authentication result = cap.authenticate(token);
    verify(validator).validate(ticket, serviceProperties.getService());
    serviceProperties.setAuthenticateAllArtifacts(true);
    result = cap.authenticate(token);
    verify(validator, times(2)).validate(ticket, serviceProperties.getService());
    token.setDetails(details);
    result = cap.authenticate(token);
    verify(validator).validate(ticket, serviceUrl);
    serviceProperties.setAuthenticateAllArtifacts(false);
    serviceProperties.setService(null);
    cap.setServiceProperties(serviceProperties);
    cap.afterPropertiesSet();
    result = cap.authenticate(token);
    verify(validator, times(2)).validate(ticket, serviceUrl);
    token.setDetails(new WebAuthenticationDetails(new MockHttpServletRequest()));
    try {
        cap.authenticate(token);
        fail("Expected Exception");
    } catch (IllegalStateException success) {
    }
    cap.setServiceProperties(null);
    cap.afterPropertiesSet();
    try {
        cap.authenticate(token);
        fail("Expected Exception");
    } catch (IllegalStateException success) {
    }
}
Also used : ServiceAuthenticationDetails(org.springframework.security.cas.web.authentication.ServiceAuthenticationDetails) AssertionImpl(org.jasig.cas.client.validation.AssertionImpl) ServiceProperties(org.springframework.security.cas.ServiceProperties) TicketValidator(org.jasig.cas.client.validation.TicketValidator) Authentication(org.springframework.security.core.Authentication) WebAuthenticationDetails(org.springframework.security.web.authentication.WebAuthenticationDetails) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 3 with WebAuthenticationDetails

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

the class WebAuthenticationDetailsMixinTests method buildWebAuthenticationDetailsUsingDifferentConstructors.

// @formatter:on
@Test
public void buildWebAuthenticationDetailsUsingDifferentConstructors() throws IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("localhost");
    request.setSession(new MockHttpSession(null, "1"));
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    WebAuthenticationDetails authenticationDetails = mapper.readValue(AUTHENTICATION_DETAILS_JSON, WebAuthenticationDetails.class);
    assertThat(details.equals(authenticationDetails));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) WebAuthenticationDetails(org.springframework.security.web.authentication.WebAuthenticationDetails) MockHttpSession(org.springframework.mock.web.MockHttpSession) Test(org.junit.Test)

Example 4 with WebAuthenticationDetails

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

the class WebAuthenticationDetailsMixinTests method webAuthenticationDetailsDeserializeTest.

@Test
public void webAuthenticationDetailsDeserializeTest() throws IOException, JSONException {
    WebAuthenticationDetails details = mapper.readValue(AUTHENTICATION_DETAILS_JSON, WebAuthenticationDetails.class);
    assertThat(details).isNotNull();
    assertThat(details.getRemoteAddress()).isEqualTo("/localhost");
    assertThat(details.getSessionId()).isEqualTo("1");
}
Also used : WebAuthenticationDetails(org.springframework.security.web.authentication.WebAuthenticationDetails) Test(org.junit.Test)

Example 5 with WebAuthenticationDetails

use of org.springframework.security.web.authentication.WebAuthenticationDetails in project libresonic by Libresonic.

the class LibresonicApplicationEventListener method onApplicationEvent.

@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof AbstractAuthenticationFailureEvent) {
        if (event.getSource() instanceof AbstractAuthenticationToken) {
            AbstractAuthenticationToken token = (AbstractAuthenticationToken) event.getSource();
            Object details = token.getDetails();
            if (details instanceof WebAuthenticationDetails) {
                loginFailureLogger.log(((WebAuthenticationDetails) details).getRemoteAddress(), String.valueOf(token.getPrincipal()));
            }
        }
    }
}
Also used : AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) WebAuthenticationDetails(org.springframework.security.web.authentication.WebAuthenticationDetails) AbstractAuthenticationFailureEvent(org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent)

Aggregations

WebAuthenticationDetails (org.springframework.security.web.authentication.WebAuthenticationDetails)18 Authentication (org.springframework.security.core.Authentication)11 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 Date (java.util.Date)3 Test (org.junit.Test)3 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)3 AbstractAuthenticationToken (org.springframework.security.authentication.AbstractAuthenticationToken)3 AuthenticationException (org.springframework.security.core.AuthenticationException)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpSession (javax.servlet.http.HttpSession)2 UserconnectionEntity (org.orcid.persistence.jpa.entities.UserconnectionEntity)2 MockHttpSession (org.springframework.mock.web.MockHttpSession)2 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)2 AuthenticationFailureBadCredentialsEvent (org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent)2 GrantedAuthority (org.springframework.security.core.GrantedAuthority)2 User (org.springframework.security.core.userdetails.User)2 UserDetails (org.springframework.security.core.userdetails.UserDetails)2 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)2