§OpenID Support in Play

OpenID is a protocol for users to access several services with a single account. As a web developer, you can use OpenID to offer users a way to log in using an account they already have, such as their Google account. In the enterprise, you may be able to use OpenID to connect to a company’s SSO server.

§The OpenID flow in a nutshell

  1. The user gives you his OpenID (a URL).
  2. Your server inspects the content behind the URL to produce a URL where you need to redirect the user.
  3. The user confirms the authorization on his OpenID provider, and gets redirected back to your server.
  4. Your server receives information from that redirect, and checks with the provider that the information is correct.

Step 1 may be omitted if all your users are using the same OpenID provider (for example if you decide to rely completely on Google accounts).


To use OpenID, first add javaWs to your build.sbt file:

libraryDependencies ++= Seq(

Now any controller or component that wants to use OpenID will have to declare a dependency on the OpenIdClient.

§OpenID in Play

The OpenID API has two important functions:

If the Promise fails, you can define a fallback, which redirects back the user to the login page or return a BadRequest.



GET     /openID/login               controllers.OpenIDController.login()
POST    /openID/login               controllers.OpenIDController.loginPost()
GET     /openID/callback            controllers.OpenIDController.openIDCallback()


import java.util.*;

import play.libs.F.Promise;
import play.libs.openid.*;
import play.mvc.*;

import javax.inject.Inject;

public class OpenIDController extends Controller {

  @Inject OpenIdClient openIdClient;

  public Result login() {
    return ok(views.html.login.render(""));

  public Promise<Result> loginPost() {

    // Form data
    final DynamicForm requestData = Form.form().bindFromRequest();
    final String openID = requestData.get("openID");

    final Promise<String> redirectUrlPromise =
            openIdClient.redirectURL(openID, routes.OpenIDController.openIDCallback().absoluteURL(request()));

    final Promise<Result> resultPromise = redirectUrlPromise
            .recover(throwable ->

    return resultPromise;

  public Promise<Result> openIDCallback() {

    final Promise<UserInfo> userInfoPromise = openIdClient.verifiedId();

    final Promise<Result> resultPromise = ->
            (Result) ok( + "\n" + userInfo.attributes())
    ).recover(throwable ->

    return resultPromise;

  public static class views {
      public static class html {
          public static class login {
              public static Html render(String msg) {


§Extended Attributes

The OpenID of a user gives you his identity. The protocol also supports getting extended attributes such as the e-mail address, the first name, or the last name.

You may request optional attributes and/or required attributes from the OpenID server. Asking for required attributes means the user cannot login to your service if he doesn’t provide them.

Extended attributes are requested in the redirect URL:

final Map<String, String> attributes = new HashMap<String, String>();
attributes.put("email", "");

final Promise<String> redirectUrlPromise = openIdClient.redirectURL(

Attributes will then be available in the UserInfo provided by the OpenID server.

Next: OAuth で保護されたリソースへのアクセス

このドキュメントの翻訳は Play チームによってメンテナンスされているものではありません。 間違いを見つけた場合、このページのソースコードを ここ で確認することができます。 ドキュメントガイドライン を読んで、お気軽にプルリクエストを送ってください。