use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.
the class AuthenticationConfigurationGh3935Tests method delegateUsesExisitingAuthentication.
@Test
public void delegateUsesExisitingAuthentication() {
String username = "user";
String password = "password";
User user = new User(username, password, AuthorityUtils.createAuthorityList("ROLE_USER"));
when(this.uds.loadUserByUsername(username)).thenReturn(user);
AuthenticationManager authenticationManager = this.adapter.authenticationManager;
assertThat(authenticationManager).isNotNull();
Authentication auth = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
verify(this.uds).loadUserByUsername(username);
assertThat(auth.getPrincipal()).isEqualTo(user);
}
use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.
the class AuthenticationManagerBeanDefinitionParserTests method eventsArePublishedByDefault.
@Test
public void eventsArePublishedByDefault() throws Exception {
setContext(CONTEXT);
AuthListener listener = new AuthListener();
appContext.addApplicationListener(listener);
ProviderManager pm = (ProviderManager) appContext.getBeansOfType(ProviderManager.class).values().toArray()[0];
Object eventPublisher = FieldUtils.getFieldValue(pm, "eventPublisher");
assertThat(eventPublisher).isNotNull();
assertThat(eventPublisher instanceof DefaultAuthenticationEventPublisher).isTrue();
pm.authenticate(new UsernamePasswordAuthenticationToken("bob", "bobspassword"));
assertThat(listener.events).hasSize(1);
}
use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.
the class JdbcUserServiceBeanDefinitionParserTests method cacheIsInjectedIntoAuthenticationProvider.
@Test
public void cacheIsInjectedIntoAuthenticationProvider() {
setContext("<authentication-manager>" + " <authentication-provider>" + " <jdbc-user-service cache-ref='userCache' data-source-ref='dataSource'/>" + " </authentication-provider>" + "</authentication-manager>" + DATA_SOURCE + USER_CACHE_XML);
ProviderManager mgr = (ProviderManager) appContext.getBean(BeanIds.AUTHENTICATION_MANAGER);
DaoAuthenticationProvider provider = (DaoAuthenticationProvider) mgr.getProviders().get(0);
assertThat(appContext.getBean("userCache")).isSameAs(provider.getUserCache());
provider.authenticate(new UsernamePasswordAuthenticationToken("rod", "koala"));
assertThat(provider.getUserCache().getUserFromCache("rod")).isNotNull().withFailMessage("Cache should contain user after authentication");
}
use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.
the class GlobalMethodSecurityBeanDefinitionParserTests method supportsExternalMetadataSource.
@Test
@SuppressWarnings("unchecked")
public void supportsExternalMetadataSource() throws Exception {
setContext("<b:bean id='target' class='" + ConcreteFoo.class.getName() + "'/>" + "<method-security-metadata-source id='mds'>" + " <protect method='" + Foo.class.getName() + ".foo' access='ROLE_ADMIN'/>" + "</method-security-metadata-source>" + "<global-method-security pre-post-annotations='enabled' metadata-source-ref='mds'/>" + AUTH_PROVIDER_XML);
// External MDS should take precedence over PreAuthorize
SecurityContextHolder.getContext().setAuthentication(bob);
Foo foo = (Foo) appContext.getBean("target");
try {
foo.foo(new SecurityConfig("A"));
fail("Bob can't invoke admin methods");
} catch (AccessDeniedException expected) {
}
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken("admin", "password"));
foo.foo(new SecurityConfig("A"));
}
use of org.springframework.security.authentication.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.
the class GlobalMethodSecurityBeanDefinitionParserTests method worksWithoutTargetOrClass.
// SEC-936
@Test(expected = AccessDeniedException.class)
public void worksWithoutTargetOrClass() {
setContext("<global-method-security secured-annotations='enabled'/>" + "<b:bean id='businessService' class='org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean'>" + " <b:property name='serviceUrl' value='http://localhost:8080/SomeService'/>" + " <b:property name='serviceInterface' value='org.springframework.security.access.annotation.BusinessService'/>" + "</b:bean>" + AUTH_PROVIDER_XML);
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test", "Password", AuthorityUtils.createAuthorityList("ROLE_SOMEOTHERROLE"));
SecurityContextHolder.getContext().setAuthentication(token);
target = (BusinessService) appContext.getBean("businessService");
target.someUserMethod1();
}
Aggregations