本文整理汇总了Java中org.springframework.security.userdetails.UserDetails类的典型用法代码示例。如果您正苦于以下问题:Java UserDetails类的具体用法?Java UserDetails怎么用?Java UserDetails使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
UserDetails类属于org.springframework.security.userdetails包,在下文中一共展示了UserDetails类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: shouldBeAbleToAuthenticateUserUsingAnyOfTheAuthorizationPlugins
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
@Test
public void shouldBeAbleToAuthenticateUserUsingAnyOfTheAuthorizationPlugins() {
String pluginId1 = "plugin-id-1";
String pluginId2 = "plugin-id-2";
addPluginSupportingPasswordBasedAuthentication(pluginId1);
addPluginSupportingPasswordBasedAuthentication(pluginId2);
securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("github", pluginId2));
securityConfig.addRole(new PluginRoleConfig("admin", "github", ConfigurationPropertyMother.create("foo")));
when(authorizationExtension.authenticateUser(pluginId1, "username", "password", securityConfig.securityAuthConfigs().findByPluginId(pluginId1), null)).thenReturn(NULL_AUTH_RESPONSE);
AuthenticationResponse response = new AuthenticationResponse(new User("username", "display-name", "[email protected]"), Collections.emptyList());
when(authorizationExtension.authenticateUser(pluginId2, "username", "password", securityConfig.securityAuthConfigs().findByPluginId(pluginId2), securityConfig.getPluginRoles(pluginId2))).thenReturn(response);
UserDetails userDetails = provider.retrieveUser("username", authenticationToken);
assertThat(userDetails, is(instanceOf(GoUserPrinciple.class)));
GoUserPrinciple goUserPrincipal = (GoUserPrinciple) userDetails;
assertThat(goUserPrincipal.getUsername(), is("username"));
assertThat(goUserPrincipal.getDisplayName(), is("display-name"));
assertThat(goUserPrincipal.getAuthorities().length, is(1));
assertThat(goUserPrincipal.getAuthorities()[0], is(userAuthority));
}
示例2: shouldTryAuthenticatingAgainstEachAuthorizationPluginInCaseOfErrors
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
@Test
public void shouldTryAuthenticatingAgainstEachAuthorizationPluginInCaseOfErrors() throws Exception {
SecurityAuthConfig fileAuthConfig = new SecurityAuthConfig("file_based", "file");
SecurityAuthConfig ldapAuthConfig = new SecurityAuthConfig("ldap_based", "ldap");
addPluginSupportingPasswordBasedAuthentication("file");
addPluginSupportingPasswordBasedAuthentication("ldap");
securityConfig.securityAuthConfigs().add(fileAuthConfig);
securityConfig.securityAuthConfigs().add(ldapAuthConfig);
when(authorizationExtension.authenticateUser("file", "username", "password", Collections.singletonList(fileAuthConfig), Collections.emptyList())).
thenThrow(new RuntimeException());
when(authorizationExtension.authenticateUser("ldap", "username", "password", Collections.singletonList(ldapAuthConfig), Collections.emptyList())).
thenReturn(new AuthenticationResponse(new User("username", null, null), Collections.emptyList()));
UserDetails bob = provider.retrieveUser("username", authenticationToken);
assertThat(bob.getUsername(), is("username"));
}
示例3: shouldUpdatePluginRolesForAUserPostAuthentication
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
@Test
public void shouldUpdatePluginRolesForAUserPostAuthentication() {
securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("ldap", "cd.go.ldap"));
securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("github", "cd.go.github"));
String pluginId1 = "cd.go.ldap";
String pluginId2 = "cd.go.github";
addPluginSupportingPasswordBasedAuthentication(pluginId1);
addPluginSupportingPasswordBasedAuthentication(pluginId2);
when(authorizationExtension.authenticateUser(pluginId1, "username", "password", securityConfig.securityAuthConfigs().findByPluginId(pluginId1), securityConfig.getPluginRoles(pluginId1))).thenReturn(
new AuthenticationResponse(
new User("username", "bob", "[email protected]"),
Arrays.asList("blackbird", "admins")
)
);
when(authorizationExtension.authenticateUser(pluginId2, "username", "password", securityConfig.securityAuthConfigs().findByPluginId(pluginId2), securityConfig.getPluginRoles(pluginId2))).thenReturn(NULL_AUTH_RESPONSE);
UserDetails userDetails = provider.retrieveUser("username", new UsernamePasswordAuthenticationToken(null, "password"));
assertNotNull(userDetails);
verify(pluginRoleService).updatePluginRoles("cd.go.ldap", "username", CaseInsensitiveString.caseInsensitiveStrings(Arrays.asList("blackbird", "admins")));
}
示例4: authenticatedUsersUsernameShouldBeUsedToAssignRoles
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
@Test
public void authenticatedUsersUsernameShouldBeUsedToAssignRoles() throws Exception {
String pluginId1 = "cd.go.ldap";
securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("ldap", "cd.go.ldap"));
addPluginSupportingPasswordBasedAuthentication(pluginId1);
when(authorizationExtension.authenticateUser(pluginId1, "[email protected]", "password", securityConfig.securityAuthConfigs().findByPluginId(pluginId1), securityConfig.getPluginRoles(pluginId1))).thenReturn(
new AuthenticationResponse(
new User("username", "bob", "[email protected]"),
Arrays.asList("blackbird", "admins")
)
);
UserDetails userDetails = provider.retrieveUser("[email protected]", new UsernamePasswordAuthenticationToken(null, "password"));
assertNotNull(userDetails);
verify(pluginRoleService).updatePluginRoles("cd.go.ldap", "username", CaseInsensitiveString.caseInsensitiveStrings(Arrays.asList("blackbird", "admins")));
}
示例5: reuthenticationUsingAuthorizationPlugins_shouldUseTheLoginNameAvailableInGoUserPrinciple
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
@Test
public void reuthenticationUsingAuthorizationPlugins_shouldUseTheLoginNameAvailableInGoUserPrinciple() throws Exception {
String pluginId1 = "cd.go.ldap";
securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("ldap", "cd.go.ldap"));
addPluginSupportingPasswordBasedAuthentication(pluginId1);
when(authorizationExtension.authenticateUser(pluginId1, "[email protected]", "password", securityConfig.securityAuthConfigs().findByPluginId(pluginId1), securityConfig.getPluginRoles(pluginId1))).thenReturn(
new AuthenticationResponse(
new User("username", "bob", "[email protected]"),
Arrays.asList("blackbird", "admins")
)
);
GoUserPrinciple principal = new GoUserPrinciple("username", "Display", "password", true, true, true, true, new GrantedAuthority[]{}, "[email protected]");
UserDetails userDetails = provider.retrieveUser("username", new UsernamePasswordAuthenticationToken(principal, "password"));
assertThat(userDetails, is(instanceOf(GoUserPrinciple.class)));
GoUserPrinciple goUserPrincipal = (GoUserPrinciple) userDetails;
assertThat(goUserPrincipal.getUsername(), is("username"));
assertThat(goUserPrincipal.getLoginName(), is("[email protected]"));
verify(pluginRoleService).updatePluginRoles("cd.go.ldap", "username", CaseInsensitiveString.caseInsensitiveStrings(Arrays.asList("blackbird", "admins")));
}
示例6: reuthenticationUsingAuthorizationPlugins_shouldFallbackOnUserNameInAbsenceOfGoUserPrinciple
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
@Test
public void reuthenticationUsingAuthorizationPlugins_shouldFallbackOnUserNameInAbsenceOfGoUserPrinciple() throws Exception {
String pluginId1 = "cd.go.ldap";
securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("ldap", "cd.go.ldap"));
addPluginSupportingPasswordBasedAuthentication(pluginId1);
when(authorizationExtension.authenticateUser(pluginId1, "username", "password", securityConfig.securityAuthConfigs().findByPluginId(pluginId1), securityConfig.getPluginRoles(pluginId1))).thenReturn(
new AuthenticationResponse(
new User("username", "bob", "[email protected]"),
Arrays.asList("blackbird", "admins")
)
);
UserDetails userDetails = provider.retrieveUser("username", new UsernamePasswordAuthenticationToken(null, "password"));
assertNotNull(userDetails);
verify(pluginRoleService).updatePluginRoles("cd.go.ldap", "username", CaseInsensitiveString.caseInsensitiveStrings(Arrays.asList("blackbird", "admins")));
}
示例7: SecurityAuthConfig
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
@Test
public void reuthenticationUsingAuthorizationPlugins_shouldFallbackOnUserNameInAbsenceOfLoginNameInGoUserPrinciple() throws Exception {
String pluginId1 = "cd.go.ldap";
securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("ldap", "cd.go.ldap"));
addPluginSupportingPasswordBasedAuthentication(pluginId1);
when(authorizationExtension.authenticateUser(pluginId1, "username", "password", securityConfig.securityAuthConfigs().findByPluginId(pluginId1), securityConfig.getPluginRoles(pluginId1))).thenReturn(
new AuthenticationResponse(
new User("username", "bob", "[email protected]"),
Arrays.asList("blackbird", "admins")
)
);
GoUserPrinciple principal = new GoUserPrinciple("username", "Display", "password", true, true, true, true, new GrantedAuthority[]{}, null);
UserDetails userDetails = provider.retrieveUser("username", new UsernamePasswordAuthenticationToken(principal, "password"));
assertNotNull(userDetails);
verify(pluginRoleService).updatePluginRoles("cd.go.ldap", "username", CaseInsensitiveString.caseInsensitiveStrings(Arrays.asList("blackbird", "admins")));
}
示例8: loadUserByUsername
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
/** {@inheritDoc} */
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException, DataAccessException {
if (m_userDao == null) {
// XXX there must be a better way to do this
throw new IllegalStateException("usersDao parameter must be set to a UsersDao bean");
}
UserDetails userDetails = m_userDao.getByUsername(username);
if (userDetails == null) {
throw new UsernameNotFoundException("User test_user is not a valid user");
}
return userDetails;
}
示例9: testGetUser
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
public void testGetUser() {
SpringSecurityUserDao userDao = createMock(SpringSecurityUserDao.class);
OpenNMSUserDetailsService detailsService = new OpenNMSUserDetailsService();
detailsService.setUserDao(userDao);
OnmsUser user = new OnmsUser();
expect(userDao.getByUsername("test_user")).andReturn(user);
replay(userDao);
UserDetails userDetails = detailsService.loadUserByUsername("test_user");
verify(userDao);
assertNotNull("user object from DAO not null", userDetails);
assertEquals("user objects", user, userDetails);
}
示例10: getUsername
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
/**
* <p>getUsername</p>
*
* @return a {@link java.lang.String} object.
*/
protected String getUsername() {
/*
* This should never be null, as the strategy should create a
* SecurityContext if one doesn't exist, but let's check anyway.
*/
SecurityContext context = SecurityContextHolder.getContext();
Assert.state(context != null, "No security context found when calling SecurityContextHolder.getContext()");
Authentication auth = context.getAuthentication();
Assert.state(auth != null, "No Authentication object found when calling getAuthentication on our SecurityContext object");
Object obj = auth.getPrincipal();
Assert.state(obj != null, "No principal object found when calling getPrinticpal on our Authentication object");
if (obj instanceof UserDetails) {
return ((UserDetails)obj).getUsername();
} else {
return obj.toString();
}
}
示例11: shouldReturnOAUTH_USERAsTheGrantedAuthority
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
@Test
public void shouldReturnOAUTH_USERAsTheGrantedAuthority() {
when(dataSource.findOauthTokenByAccessToken("token-string")).thenReturn(oauthTokenDto("user-id"));
GrantedAuthority[] grantedAuthorities = {GoAuthority.ROLE_OAUTH_USER.asAuthority()};
OauthAuthenticationToken authentication = provider.authenticate(new OauthAuthenticationToken("token-string"));
assertThat(authentication.isAuthenticated(), is(true));
UserDetails userDetails = authentication.getPrincipal();
assertThat(userDetails.getUsername(), is("user-id"));
assertThat(userDetails.getAuthorities(), is(grantedAuthorities));
assertThat(authentication.getAuthorities(), is(grantedAuthorities));
}
示例12: shouldReturnUserDetailsWithCorrectAuthorityIfAgentCertificateHasOu
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
@Test
public void shouldReturnUserDetailsWithCorrectAuthorityIfAgentCertificateHasOu() {
X509Certificate agentCertificate = new X509CertificateGenerator().createCertificateWithDn(
"CN=hostname, OU=agent").getFirstCertificate();
UserDetails userDetails = populator.getUserDetails(agentCertificate);
GrantedAuthority[] actual = userDetails.getAuthorities();
GrantedAuthority expected = new GrantedAuthorityImpl(ROLE_AGENT);
assertThat(actual.length, is(1));
assertThat(actual[0], is(expected));
assertThat(userDetails.getUsername(), is("_go_agent_hostname"));
}
示例13: getUserDetails
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
public UserDetails getUserDetails(X509Certificate clientCert) throws AuthenticationException {
X500Principal principal = clientCert.getSubjectX500Principal();
Matcher cnMatcher = CN_PATTERN.matcher(principal.getName());
Matcher ouMatcher = OU_PATTERN.matcher(principal.getName());
if (cnMatcher.find() && ouMatcher.find()) {
GrantedAuthorityImpl agentAuthority = new GrantedAuthorityImpl(role);
return new User("_go_agent_" + cnMatcher.group(1), "", true, true, true, true, new GrantedAuthority[]{agentAuthority});
}
throw new BadCredentialsException("Couldn't find CN and/or OU for the certificate");
}
示例14: retrieveUser
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
@Override
protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
assertPasswordNotBlank(authentication);
User user = getUserDetailsFromAuthorizationPlugins(username, authentication);
if (user == null) {
removeAnyAssociatedPluginRolesFor(username);
throw new UsernameNotFoundException("Unable to authenticate user: " + username);
}
userService.addUserIfDoesNotExist(toDomainUser(user));
GoUserPrinciple goUserPrinciple = new GoUserPrinciple(user.getUsername(), user.getDisplayName(), "",
authorityGranter.authorities(user.getUsername()), loginName(username, authentication));
return goUserPrinciple;
}
示例15: authenticate
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
public OauthAuthenticationToken authenticate(Authentication authentication) throws AuthenticationException {
OauthAuthenticationToken authenticationToken = (OauthAuthenticationToken) authentication;
String token = authenticationToken.getCredentials();
OauthDataSource.OauthTokenDTO oauthToken = oauthDataSource.findOauthTokenByAccessToken(token);
if (oauthToken == null) {
throw new BadCredentialsException("No match for OAuth token: " + token);
}
String username = oauthToken.getUserId();
UserDetails user = new User(username, token, true, true, true, true, oauthAuthority());
return new OauthAuthenticationToken(user);
}