com.stormpath.sdk.application.Application.authenticateAccount()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(16.9k)|赞(0)|评价(0)|浏览(101)

本文整理了Java中com.stormpath.sdk.application.Application.authenticateAccount()方法的一些代码示例,展示了Application.authenticateAccount()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Application.authenticateAccount()方法的具体详情如下:
包路径:com.stormpath.sdk.application.Application
类名称:Application
方法名:authenticateAccount

Application.authenticateAccount介绍

[英]Authenticates an account's submitted principals and credentials (e.g. username and password). The account must be in one of the Application's assigned #getAccountStoreMappings(). If not in an assigned account store, the authentication attempt will fail. Example Consider the following username/password-based example:

AuthenticationRequest request = UsernamePasswordRequest.builder() 
.setUsernameOrEmail(username) 
.setPassword(submittedRawPlaintextPassword) 
.build(); 
Account authenticated = appToTest.authenticateAccount(request).getAccount();

Additionally, the Account can be requested to be expanded to avoid a new network transfer when obtaining it:

BasicAuthenticationOptions options = UsernamePasswordRequest.options().withAccount(); 
AuthenticationRequest request = UsernamePasswordRequest.builder() 
.setUsernameOrEmail(username) 
.setPassword(submittedRawPlaintextPassword) 
.withResponseOptions(options) 
.build(); 
Account authenticated = appToTest.authenticateAccount(request).getAccount();

[中]验证帐户提交的主体和凭据(例如用户名和密码)。帐户必须位于应用程序分配的#getAccountStoreMappings()之一。如果不在分配的帐户存储中,身份验证尝试将失败。示例考虑以下用户名/密码为例:

AuthenticationRequest request = UsernamePasswordRequest.builder() 
.setUsernameOrEmail(username) 
.setPassword(submittedRawPlaintextPassword) 
.build(); 
Account authenticated = appToTest.authenticateAccount(request).getAccount();

此外,在获取帐户时,可以请求扩展帐户以避免新的网络传输:

BasicAuthenticationOptions options = UsernamePasswordRequest.options().withAccount(); 
AuthenticationRequest request = UsernamePasswordRequest.builder() 
.setUsernameOrEmail(username) 
.setPassword(submittedRawPlaintextPassword) 
.withResponseOptions(options) 
.build(); 
Account authenticated = appToTest.authenticateAccount(request).getAccount();

代码示例

代码示例来源:origin: com.stormpath.sdk/stormpath-sdk-impl

protected ApiAuthenticationResult execute() {
  AuthenticationRequest request = FACTORY.createFrom(httpRequest);
  AuthenticationResult result = application.authenticateAccount(request);
  Assert.isInstanceOf(ApiAuthenticationResult.class, result);
  return (ApiAuthenticationResult) result;
}

代码示例来源:origin: stormpath/stormpath-sdk-java

public OAuthAuthenticationResult authenticate(HttpServletRequest httpRequest) {

    Assert.notNull(httpRequest, "httpRequest cannot be null.");

    Class httpRequestClass = httpRequest.getClass();

    if (HttpServletRequest.class.isAssignableFrom(httpRequestClass)) {
      this.httpServletRequest = httpRequest;
    } else {
      throw new IllegalArgumentException(String.format(HTTP_REQUEST_NOT_SUPPORTED_MSG, httpRequest.getClass(), HttpServletRequest.class.getName()));
    }

    OAuthAuthenticationRequestFactory factory = new OAuthAuthenticationRequestFactory();
    AuthenticationRequest request = factory.createFrom(httpServletRequest);
    AuthenticationResult result = application.authenticateAccount(request);
    Assert.isInstanceOf(OAuthAuthenticationResult.class, result);
    return (OAuthAuthenticationResult) result;
  }
}

代码示例来源:origin: com.stormpath.sdk/stormpath-sdk-oauth

public OAuthAuthenticationResult authenticate(HttpServletRequest httpRequest) {

    Assert.notNull(httpRequest, "httpRequest cannot be null.");

    Class httpRequestClass = httpRequest.getClass();

    if (HttpServletRequest.class.isAssignableFrom(httpRequestClass)) {
      this.httpServletRequest = httpRequest;
    } else {
      throw new IllegalArgumentException(String.format(HTTP_REQUEST_NOT_SUPPORTED_MSG, httpRequest.getClass(), HttpServletRequest.class.getName()));
    }

    OAuthAuthenticationRequestFactory factory = new OAuthAuthenticationRequestFactory();
    AuthenticationRequest request = factory.createFrom(httpServletRequest);
    AuthenticationResult result = application.authenticateAccount(request);
    Assert.isInstanceOf(OAuthAuthenticationResult.class, result);
    return (OAuthAuthenticationResult) result;
  }
}

代码示例来源:origin: stormpath/stormpath-sdk-java

protected ApiAuthenticationResult execute() {
  AuthenticationRequest request = FACTORY.createFrom(httpRequest);
  AuthenticationResult result = application.authenticateAccount(request);
  Assert.isInstanceOf(ApiAuthenticationResult.class, result);
  return (ApiAuthenticationResult) result;
}

代码示例来源:origin: com.stormpath.sdk/stormpath-sdk-oauth

/**
 * @since 1.0.RC4.6
 */
@Override
public OAuthAuthenticationResult authenticate(HttpRequest httpRequest) {
  Assert.notNull(httpRequest, "httpRequest cannot be null.");
  Class httpRequestClass = httpRequest.getClass();
  if (HttpServletRequest.class.isAssignableFrom(httpRequestClass)) {
    this.httpServletRequest = (HttpServletRequest) httpRequest;
  } else if (HttpRequest.class.isAssignableFrom(httpRequestClass)) {
    this.httpServletRequest = new OAuthHttpServletRequest(httpRequest);
  } else {
    throw new IllegalArgumentException(String.format(HTTP_REQUEST_NOT_SUPPORTED_MSG, httpRequest.getClass(), HttpRequest.class.getName(), HttpServletRequest.class.getName()));
  }
  OAuthAuthenticationRequestFactory factory = new OAuthAuthenticationRequestFactory();
  AuthenticationRequest request = factory.createFrom(httpServletRequest);
  AuthenticationResult result = application.authenticateAccount(request);
  Assert.isInstanceOf(OAuthAuthenticationResult.class, result);
  return (OAuthAuthenticationResult) result;
}

代码示例来源:origin: stormpath/stormpath-sdk-java

/**
 * @since 1.0.RC4.6
 */
@Override
public OAuthAuthenticationResult authenticate(HttpRequest httpRequest) {
  Assert.notNull(httpRequest, "httpRequest cannot be null.");
  Class httpRequestClass = httpRequest.getClass();
  if (HttpServletRequest.class.isAssignableFrom(httpRequestClass)) {
    this.httpServletRequest = (HttpServletRequest) httpRequest;
  } else if (HttpRequest.class.isAssignableFrom(httpRequestClass)) {
    this.httpServletRequest = new OAuthHttpServletRequest(httpRequest);
  } else {
    throw new IllegalArgumentException(String.format(HTTP_REQUEST_NOT_SUPPORTED_MSG, httpRequest.getClass(), HttpRequest.class.getName(), HttpServletRequest.class.getName()));
  }
  OAuthAuthenticationRequestFactory factory = new OAuthAuthenticationRequestFactory();
  AuthenticationRequest request = factory.createFrom(httpServletRequest);
  AuthenticationResult result = application.authenticateAccount(request);
  Assert.isInstanceOf(OAuthAuthenticationResult.class, result);
  return (OAuthAuthenticationResult) result;
}

代码示例来源:origin: stormpath/stormpath-sdk-java

@Override
  public OAuthAuthenticationResult authenticate(HttpRequest httpRequest) {
    RequestLocation[] locations = this.locations != null ? this.locations :
        new RequestLocation[]{RequestLocation.HEADER, RequestLocation.BODY};

    Class httpRequestClass = httpRequest.getClass();

    if (HttpServletRequest.class.isAssignableFrom(httpRequestClass)) {
      this.httpServletRequest = (HttpServletRequest) httpRequest;
    } else if (HttpRequest.class.isAssignableFrom(httpRequestClass)) {
      this.httpServletRequest = new OAuthHttpServletRequest(httpRequest);
    } else {
      throw new IllegalArgumentException(String.format(HTTP_REQUEST_NOT_SUPPORTED_MSG, httpRequest.getClass(), HttpRequest.class.getName(), HttpServletRequest.class.getName()));
    }
    AuthenticationRequest request;

    try {
      request = new ResourceAuthenticationRequest(httpServletRequest, locations);
    } catch (Exception e) {
      throw ApiAuthenticationExceptionFactory.newOAuthException(OAuthAuthenticationException.class,
          OAuthAuthenticationException.INVALID_REQUEST);
    }

    AuthenticationResult result = application.authenticateAccount(request);
    Assert.isInstanceOf(OAuthAuthenticationResult.class, result);
    return (OAuthAuthenticationResult) result;
  }
}

代码示例来源:origin: com.stormpath.sdk/stormpath-sdk-oauth

@Override
  public OAuthAuthenticationResult authenticate(HttpRequest httpRequest) {
    RequestLocation[] locations = this.locations != null ? this.locations :
        new RequestLocation[]{RequestLocation.HEADER, RequestLocation.BODY};

    Class httpRequestClass = httpRequest.getClass();

    if (HttpServletRequest.class.isAssignableFrom(httpRequestClass)) {
      this.httpServletRequest = (HttpServletRequest) httpRequest;
    } else if (HttpRequest.class.isAssignableFrom(httpRequestClass)) {
      this.httpServletRequest = new OAuthHttpServletRequest(httpRequest);
    } else {
      throw new IllegalArgumentException(String.format(HTTP_REQUEST_NOT_SUPPORTED_MSG, httpRequest.getClass(), HttpRequest.class.getName(), HttpServletRequest.class.getName()));
    }
    AuthenticationRequest request;

    try {
      request = new ResourceAuthenticationRequest(httpServletRequest, locations);
    } catch (Exception e) {
      throw ApiAuthenticationExceptionFactory.newOAuthException(OAuthAuthenticationException.class,
          OAuthAuthenticationException.INVALID_REQUEST);
    }

    AuthenticationResult result = application.authenticateAccount(request);
    Assert.isInstanceOf(OAuthAuthenticationResult.class, result);
    return (OAuthAuthenticationResult) result;
  }
}

代码示例来源:origin: stormpath/stormpath-sdk-java

AuthenticationResult authenticationResult = application.authenticateAccount(request);

代码示例来源:origin: com.stormpath.sdk/stormpath-sdk-oauth

AuthenticationResult authenticationResult = application.authenticateAccount(request);

代码示例来源:origin: stormpath/stormpath-sdk-java

private Account handleUsernamePasswordAuthentication(Authentication authentication) throws AuthenticationException {
  AuthenticationRequest request = createAuthenticationRequest(authentication);
  Account account;
  try {
    account = application.authenticateAccount(request).getAccount();
  } finally {
    //Clear the request data to prevent later memory access
    request.clear();
  }
  return account;
}

代码示例来源:origin: org.pac4j/pac4j-stormpath

protected Account authenticateAccount(final UsernamePasswordCredentials credentials) throws ResourceException {
  return this.application.authenticateAccount(
      new UsernamePasswordRequest(credentials.getUsername(), credentials.getPassword())).getAccount();
}

代码示例来源:origin: net.unicon.cas/cas-addons

public Account authenticateAccount(final UsernamePasswordCredentials credentials) throws ResourceException {
    return this.application.authenticateAccount(new UsernamePasswordRequest(credentials.getUsername(), credentials.getPassword())).getAccount();
  }
}

代码示例来源:origin: com.stormpath.sdk/stormpath-sdk-servlet

protected HttpAuthenticationResult authenticateUsernamePassword(HttpAuthenticationAttempt attempt,
                                String usernameOrEmail, String password) {
  HttpServletRequest request = attempt.getRequest();
  HttpServletResponse response = attempt.getResponse();
  AuthenticationResult result;
  try {
    AuthenticationRequest authcRequest = createAuthenticationRequest(attempt, usernameOrEmail, password);
    Application app = getApplication(attempt.getRequest());
    result = app.authenticateAccount(authcRequest);
  } catch (Exception e) {
    String msg = "Unable to authenticate usernameOrEmail and password-based request for usernameOrEmail [" +
           usernameOrEmail + "]: " + e.getMessage();
    throw new HttpAuthenticationException(msg, e);
  }
  attempt.getRequest().setAttribute(StormpathHttpServletRequest.AUTH_TYPE_REQUEST_ATTRIBUTE_NAME,
                   HttpServletRequest.BASIC_AUTH);
  return new DefaultHttpAuthenticationResult(request, response, result);
}

代码示例来源:origin: stormpath/stormpath-sdk-java

protected HttpAuthenticationResult authenticateUsernamePassword(HttpAuthenticationAttempt attempt,
                                String usernameOrEmail, String password) {
  HttpServletRequest request = attempt.getRequest();
  HttpServletResponse response = attempt.getResponse();
  AuthenticationResult result;
  try {
    AuthenticationRequest authcRequest = createAuthenticationRequest(attempt, usernameOrEmail, password);
    Application app = getApplication(attempt.getRequest());
    result = app.authenticateAccount(authcRequest);
  } catch (Exception e) {
    String msg = "Unable to authenticate usernameOrEmail and password-based request for usernameOrEmail [" +
           usernameOrEmail + "]: " + e.getMessage();
    throw new HttpAuthenticationException(msg, e);
  }
  attempt.getRequest().setAttribute(StormpathHttpServletRequest.AUTH_TYPE_REQUEST_ATTRIBUTE_NAME,
                   HttpServletRequest.BASIC_AUTH);
  return new DefaultHttpAuthenticationResult(request, response, result);
}

代码示例来源:origin: stackoverflow.com

// Instantiate a builder for your client and set required properties
ClientBuilder builder = Clients.builder();    

// Build the client instance that you will use throughout your application code
Client client = builder.build();

Tenant tenant = client.getCurrentTenant();
ApplicationList applications = tenant.getApplications(
    Applications.where(Applications.name().eqIgnoreCase("My Application"))
);

Application application = applications.iterator().next();

//Capture the username and password, such as via an SSL-encrypted web HTML form. 
//We'll just simulate a form lookup and use the values we used above:
String usernameOrEmail = "tk421@stormpath.com";
String rawPassword = "Changeme1";

//Create an authentication request using the credentials
AuthenticationRequest request = new UsernamePasswordRequest(usernameOrEmail, rawPassword);

//Now let's authenticate the account with the application:
try {
  AuthenticationResult result = application.authenticateAccount(request);
  Account account = result.getAccount();
} catch (ResourceException ex) {
  System.out.println(ex.getStatus() + " " + ex.getMessage());
}

代码示例来源:origin: stormpath/stormpath-sdk-java

@Override
public void login(String username, String password) throws ServletException {
  final AuthenticationRequest authcRequest = createAuthenticationRequest(username, password);
  if (hasAccount()) {
    Account account = getRequiredAccount();
    String msg = "The current request is already associated with an authenticated user [" + account.getEmail() +
        "].  Login attempt for submitted username [" + username + "] is denied.";
    ServletException ex = new ServletException(msg);
    FailedAuthenticationRequestEvent e = createEvent(authcRequest, ex);
    publish(e);
    throw ex;
  }
  AccessTokenResult result = (AccessTokenResult) this.getApplication().authenticateAccount(authcRequest);
  setAttribute(StormpathHttpServletRequest.AUTH_TYPE_REQUEST_ATTRIBUTE_NAME, "LOGIN_METHOD");
  Account account = result.getAccount();
  setAttribute(DefaultAccountResolver.REQUEST_ATTR_NAME, account);
  setAttribute(AccessTokenResolver.REQUEST_ATTR_NAME, result.getTokenResponse().getAccessToken());
  setAttribute(RefreshTokenResolver.REQUEST_ATTR_NAME, result.getTokenResponse().getRefreshToken());
  setAttribute(OAuthTokenResolver.REQUEST_ATTR_NAME, result);
  SuccessfulAuthenticationRequestEvent e = createEvent(authcRequest, result);
  publish(e);
}

代码示例来源:origin: stormpath/stormpath-shiro

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
  assertState();
  UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
  AuthenticationRequest request = createAuthenticationRequest(token);
  Application application = ensureApplicationReference();
  Account account;
  try {
    account = application.authenticateAccount(request).getAccount();
  } catch (ResourceException e) {
    //todo error code translation to throw more detailed exceptions
    String msg = StringUtils.clean(e.getMessage());
    if (msg == null) {
      msg = StringUtils.clean(e.getDeveloperMessage());
    }
    if (msg == null) {
      msg = "Invalid login or password.";
    }
    throw new AuthenticationException(msg, e);
  }
  PrincipalCollection principals;
  try {
    principals = createPrincipals(account);
  } catch (Exception e) {
    throw new AuthenticationException("Unable to obtain authenticated account properties.", e);
  }
  return new SimpleAuthenticationInfo(principals, null);
}

代码示例来源:origin: com.stormpath.shiro/stormpath-shiro-core

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
  assertState();
  UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
  AuthenticationRequest request = createAuthenticationRequest(token);
  Application application = ensureApplicationReference();
  Account account;
  try {
    account = application.authenticateAccount(request).getAccount();
  } catch (ResourceException e) {
    //todo error code translation to throw more detailed exceptions
    String msg = StringUtils.clean(e.getMessage());
    if (msg == null) {
      msg = StringUtils.clean(e.getDeveloperMessage());
    }
    if (msg == null) {
      msg = "Invalid login or password.";
    }
    throw new AuthenticationException(msg, e);
  }
  PrincipalCollection principals;
  try {
    principals = createPrincipals(account);
  } catch (Exception e) {
    throw new AuthenticationException("Unable to obtain authenticated account properties.", e);
  }
  return new SimpleAuthenticationInfo(principals, null);
}

代码示例来源:origin: com.stormpath.sdk/stormpath-sdk-servlet

@Override
public void login(String username, String password) throws ServletException {
  final AuthenticationRequest authcRequest = createAuthenticationRequest(username, password);
  if (hasAccount()) {
    Account account = getRequiredAccount();
    String msg = "The current request is already associated with an authenticated user [" + account.getEmail() +
        "].  Login attempt for submitted username [" + username + "] is denied.";
    ServletException ex = new ServletException(msg);
    FailedAuthenticationRequestEvent e = createEvent(authcRequest, ex);
    publish(e);
    throw ex;
  }
  AccessTokenResult result = (AccessTokenResult) this.getApplication().authenticateAccount(authcRequest);
  setAttribute(StormpathHttpServletRequest.AUTH_TYPE_REQUEST_ATTRIBUTE_NAME, "LOGIN_METHOD");
  Account account = result.getAccount();
  setAttribute(DefaultAccountResolver.REQUEST_ATTR_NAME, account);
  setAttribute(AccessTokenResolver.REQUEST_ATTR_NAME, result.getTokenResponse().getAccessToken());
  setAttribute(RefreshTokenResolver.REQUEST_ATTR_NAME, result.getTokenResponse().getRefreshToken());
  setAttribute(OAuthTokenResolver.REQUEST_ATTR_NAME, result);
  SuccessfulAuthenticationRequestEvent e = createEvent(authcRequest, result);
  publish(e);
}

相关文章