Package org.springframework.security.authentication

Examples of org.springframework.security.authentication.TestingAuthenticationToken


        for (Entitlement entitlement : entitlementDAO.findAll()) {
            authorities.add(new SimpleGrantedAuthority(entitlement.getName()));
        }

        UserDetails userDetails = new User(adminUser, "FAKE_PASSWORD", true, true, true, true, authorities);
        Authentication authentication = new TestingAuthenticationToken(userDetails, "FAKE_PASSWORD", authorities);
        SecurityContextHolder.getContext().setAuthentication(authentication);
    }
View Full Code Here


    @Test
    public void testGetAuthenticatedUser() throws Exception {
        List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
        grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER"));
        UserDetails userDetails = new UserImpl("1", "canonical");
        Authentication authentication = new TestingAuthenticationToken(userDetails, "canonical", grantedAuthorities);
        SecurityContextHolder.getContext().setAuthentication(authentication);

        User returnedUser = userService.getAuthenticatedUser();
        assertEquals(returnedUser, userDetails);
    }
View Full Code Here

        TestableResponse resp = tester.renderPageAndReturnResponse("SecuredMethod");
        EasyMock.verify(MockFactory.getInstance().getMockedObjects());
        assertEquals(500,resp.getStatus()); //err not allowed!

        // login..! (wrong role)
        SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken("user1","user1","ROLE_DENIED"));
       
        EasyMock.reset(MockFactory.getInstance().getMockedObjects());
        EasyMock.replay(MockFactory.getInstance().getMockedObjects());
       
       
        try {
          tester.renderPage("securedmethod");
          fail("Should not render a document");
        } catch (Exception e) {
         
        }
        EasyMock.verify(MockFactory.getInstance().getMockedObjects());
        // status = 200 , output = '', coz of redirect to login page
       
        assertEquals(200,resp.getStatus()); //err not allowed!
       
        SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken("user1","user1","ROLE_LOGGEDIN"));
       
        EasyMock.reset(MockFactory.getInstance().getMockedObjects());
        EasyMock.replay(MockFactory.getInstance().getMockedObjects());
       
        assertTrue(tester.renderPage("securedmethod").toString().contains("Welcome back user !"));
View Full Code Here

    private UserDetails createPrincipal(List<GrantedAuthority> authorities ) {
        return new User(USERNAME, PASSWORD, authorities);
    }

    private Authentication createAuthentication(UserDetails principal, List<GrantedAuthority> authorities) {
        return new TestingAuthenticationToken(principal, USERNAME, authorities);
    }
View Full Code Here

    @Test
    public void onLogoutSuccess() throws IOException, ServletException {
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        Authentication authentication = new TestingAuthenticationToken(null, null);

        logoutSuccessHandler.onLogoutSuccess(request, response, authentication);

        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    }
View Full Code Here

    @Test
    public void onAuthenticationSuccess() throws ServletException, IOException {
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        Authentication authentication = new TestingAuthenticationToken(null, null);

        successHandler.onAuthenticationSuccess(request, response, authentication);

        assertEquals(MockHttpServletResponse.SC_OK, response.getStatus());
    }
View Full Code Here

            .andExpect(authenticated().withAuthenticationPrincipal(user));
    }

    @Test
    public void requestProtectedUrlWithAuthentication() throws Exception {
        Authentication authentication = new TestingAuthenticationToken("test", "notused", "ROLE_USER");
        mvc
            .perform(get("/").with(authentication(authentication)))
            // Ensure we got past Security
            .andExpect(status().isNotFound())
            // Ensure it appears we are authenticated with user
View Full Code Here

        cap.setStatelessTicketCache(new MockStatelessTicketCache());
        cap.setTicketValidator(new MockTicketValidator(true));
        cap.setServiceProperties(makeServiceProperties());
        cap.afterPropertiesSet();

        TestingAuthenticationToken token = new TestingAuthenticationToken("user", "password", "ROLE_A");
        assertFalse(cap.supports(TestingAuthenticationToken.class));

        // Try it anyway
        assertEquals(null, cap.authenticate(token));
    }
View Full Code Here

                    throw new UsernameNotFoundException("No user for principal "+principal);
                }
                if(!authentication.getCredentials().equals(user.getPassword())) {
                    throw new BadCredentialsException("Invalid password");
                }
                return new TestingAuthenticationToken(principal, null, "ROLE_USER");
            }
        };
    }
View Full Code Here

        assertFalse(filter.requiresAuthentication(request, response));
        request.setParameter(properties.getArtifactParameter(), "value");
        assertTrue(filter.requiresAuthentication(request, response));
        SecurityContextHolder.getContext().setAuthentication(new AnonymousAuthenticationToken("key", "principal", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS")));
        assertTrue(filter.requiresAuthentication(request, response));
        SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken("un", "principal", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS")));
        assertTrue(filter.requiresAuthentication(request, response));
        SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken("un", "principal", "ROLE_ANONYMOUS"));
        assertFalse(filter.requiresAuthentication(request, response));
    }
View Full Code Here

TOP

Related Classes of org.springframework.security.authentication.TestingAuthenticationToken

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.