本文整理汇总了Java中org.springframework.security.userdetails.UserDetails类的典型用法代码示例。如果您正苦于以下问题:Java UserDetails类的具体用法?Java UserDetails怎么用?Java UserDetails使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
UserDetails类属于org.springframework.security.userdetails包,在下文中一共展示了UserDetails类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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));
}
开发者ID:gocd,项目名称:gocd,代码行数:25,代码来源:PluginAuthenticationProviderTest.java
示例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"));
}
开发者ID:gocd,项目名称:gocd,代码行数:20,代码来源:PluginAuthenticationProviderTest.java
示例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")));
}
开发者ID:gocd,项目名称:gocd,代码行数:26,代码来源:PluginAuthenticationProviderTest.java
示例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")));
}
开发者ID:gocd,项目名称:gocd,代码行数:20,代码来源:PluginAuthenticationProviderTest.java
示例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")));
}
开发者ID:gocd,项目名称:gocd,代码行数:24,代码来源:PluginAuthenticationProviderTest.java
示例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")));
}
开发者ID:gocd,项目名称:gocd,代码行数:20,代码来源:PluginAuthenticationProviderTest.java
示例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")));
}
开发者ID:gocd,项目名称:gocd,代码行数:21,代码来源:PluginAuthenticationProviderTest.java
示例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;
}
开发者ID:vishwaabhinav,项目名称:OpenNMS,代码行数:17,代码来源:OpenNMSUserDetailsService.java
示例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);
}
开发者ID:vishwaabhinav,项目名称:OpenNMS,代码行数:18,代码来源:OpenNMSUserDetailsServiceTest.java
示例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();
}
}
开发者ID:vishwaabhinav,项目名称:OpenNMS,代码行数:27,代码来源:DefaultSurveillanceService.java
示例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));
}
开发者ID:gocd,项目名称:gocd,代码行数:13,代码来源:OauthAuthenticationProviderTest.java
示例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"));
}
开发者ID:gocd,项目名称:gocd,代码行数:12,代码来源:X509AuthoritiesPopulatorTest.java
示例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");
}
开发者ID:gocd,项目名称:gocd,代码行数:11,代码来源:X509AuthoritiesPopulator.java
示例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;
}
开发者ID:gocd,项目名称:gocd,代码行数:16,代码来源:PluginAuthenticationProvider.java
示例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);
}
开发者ID:gocd,项目名称:gocd,代码行数:13,代码来源:OauthAuthenticationProvider.java
示例16: doAuthenticate
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
private Authentication doAuthenticate(PreAuthenticatedAuthenticationToken preAuthToken) {
String pluginId = preAuthToken.getPluginId();
AuthenticationResponse response = null;
try {
response = authenticateUser(preAuthToken);
} catch (Exception e) {
handleUnSuccessfulAuthentication(preAuthToken);
}
if (!isAuthenticated(response)) {
handleUnSuccessfulAuthentication(preAuthToken);
}
validateUser(response.getUser());
assignRoles(pluginId, response.getUser().getUsername(), response.getRoles());
UserDetails userDetails = getUserDetails(response.getUser());
userService.addUserIfDoesNotExist(toDomainUser(response.getUser()));
PreAuthenticatedAuthenticationToken result =
new PreAuthenticatedAuthenticationToken(userDetails, preAuthToken.getCredentials(), pluginId, userDetails.getAuthorities());
result.setAuthenticated(true);
return result;
}
开发者ID:gocd,项目名称:gocd,代码行数:30,代码来源:PreAuthenticatedAuthenticationProvider.java
示例17: getUserName
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
public static Username getUserName(Authentication authentication) {
Object principal = authentication.getPrincipal();
if (principal instanceof GoUserPrinciple) {
GoUserPrinciple userPrincipleDetails = (GoUserPrinciple) principal;
return new Username(new CaseInsensitiveString(userPrincipleDetails.getUsername()), userPrincipleDetails.getDisplayName());
}
if (principal instanceof UserDetails) {
UserDetails userDetails = (UserDetails) principal;
return new Username(new CaseInsensitiveString(userDetails.getUsername()));
}
return ANONYMOUS;
}
开发者ID:gocd,项目名称:gocd,代码行数:13,代码来源:UserHelper.java
示例18: onAuthenticationSuccess
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
protected void onAuthenticationSuccess(AuthenticationSuccessEvent event) {
// on success - principal is a UserDetails
UserDetails details = (UserDetails) event.getAuthentication().getPrincipal();
String username = details.getUsername();
if (!StringUtils.isBlank(username)) {
Long orgId = organizationManager.getOrganization().getId();
User user = userDao.findUserByOrganizationAndUsername(orgId, username);
if (user != null) {
user.setLoginFailureCount(0);
userDao.persist(user);
}
}
}
开发者ID:vactowb,项目名称:jbehave-core,代码行数:14,代码来源:AuthenticationEventListener.java
示例19: loadUserByUsername
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
Organization org = organizationManager.getOrganization();
User user = userDao.findUserByOrganizationAndUsername(org.getId(), username);
if (user == null) {
throw new UsernameNotFoundException(username);
}
return new UserDetailsImpl(user, org.getAuthenticationPolicy());
}
开发者ID:vactowb,项目名称:jbehave-core,代码行数:10,代码来源:UserDetailsServiceImpl.java
示例20: loadUserByUsername
import org.springframework.security.userdetails.UserDetails; //导入依赖的package包/类
/**
* {@inheritDoc}
*
* Load the user detail in the authentication phase
*/
public UserDetails loadUserByUsername(String username) {
try {
UserDetails user = super.loadUserByUsername(username);
Map<String, Object> userInfo = new HashMap<String, Object>();
// put in userInfo your custom objects
return new AclUser(user.getUsername(), user.getPassword(), user.isEnabled(), user.getAuthorities(), userInfo);
} catch (UsernameNotFoundException userEx) {
throw new UsernameNotFoundException("Username not found:" + username);
} catch (DataAccessException dataEx) {
throw new RepositoryException(dataEx.getMessage());
}
}
开发者ID:vishwaabhinav,项目名称:OpenNMS,代码行数:18,代码来源:AuthenticationJdbcDaoImpl.java
注:本文中的org.springframework.security.userdetails.UserDetails类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论