Giter VIP home page Giter VIP logo

acceptto-sso-java-client's Introduction

What is this project?

This is a Play 2.3 Java project to demo Acceptto CAS service.

Quick start

  1. Install Play 2.3 (Activator)
  2. git clone https://github.com/acceptto-corp/acceptto-sso-java-client
  3. cd acceptto-sso-java-client
  4. activator run
  5. Open a browser and navigate to localhost:9000

Activator will automatically download all dependencies which may take some time depending on your internet connection.

Create your own client

You can use this sample and change it to meet your needs or you can create your own client from scratch. We use pac4j as CAS client and play-pac4j for Play integration.

Create a new Play project

Create a new play-java project:

activator new

Add dependencies

Add the following dependencies:

libraryDependencies ++= Seq(
  "org.pac4j" % "play-pac4j_java" % "1.3.0-SNAPSHOT",
  "org.pac4j" % "pac4j-cas" % "1.6.0-SNAPSHOT"
)

Add the following resolver:

resolvers ++= Seq(
  "Sonatype snapshots repository" at "https://oss.sonatype.org/content/repositories/snapshots/"
)

Configuration

Config CAS client in onStart method of Global settings class.

import org.pac4j.cas.client.CasClient;
import org.pac4j.core.client.Clients;
import org.pac4j.play.Config;
import play.Application;
import play.GlobalSettings;
import play.Play;

public class Global extends GlobalSettings {
    @Override
    public void onStart(final Application app) {
        final String baseUrl = Play.application().configuration().getString("baseUrl");
        final String casUrl = Play.application().configuration().getString("casUrl");
        // CAS
        final CasClient casClient = new CasClient();
        casClient.setCasLoginUrl(casUrl);
        final Clients clients = new Clients(baseUrl + "/callback", casClient);
        
        Config.setClients(clients);
    }
}

Controller and Action

Authentication controller should inherit from JavaController then you can use:

  • getRedirectAction to obtain the redirection action
  • getUserProfile to obtain user profile if it was available
package controllers;

import play.*;
import play.mvc.*;

import views.html.*;

import org.pac4j.play.java.*;
import org.pac4j.core.profile.CommonProfile;

public class Application extends JavaController {
    public static Result index() {
        final CommonProfile profile = getUserProfile();
        final String url = getRedirectAction("CasClient", "/").getLocation();
        return ok(views.html.index.render(profile, url));
    }

    @RequiresAuthentication(clientName = "CasClient")
    public static Result protectedIndex() {
      // profile
      final CommonProfile profile = getUserProfile();
      return ok(views.html.protectedIndex.render(profile));
    }
}

View

If the user is not authorized by Acceptto he or she will see the Authenticate with Acceptto CAS button, otherwise he or she will see the authorized profile information.

@(profile : org.pac4j.core.profile.CommonProfile, urlCas: String)

<div>
@profile match {
    case _: org.pac4j.cas.profile.CasProfile => {
        <p>
            <a href="logout">Logout</a>
        </p>
        @profile
    }
    case _ => {
        <a href="@urlCas">Authenticate with Acceptto CAS</a><br />
    }
}
</div>

Routes

The final step is to set your routes.

GET   /                       controllers.Application.index()
GET   /protected/index.html   controllers.Application.protectedIndex()
GET   /callback               org.pac4j.play.CallbackController.callback()
POST  /callback               org.pac4j.play.CallbackController.callback()
GET   /logout                 org.pac4j.play.CallbackController.logoutAndRedirect()

acceptto-sso-java-client's People

Contributors

acceptto-dev avatar

Watchers

James Cloos avatar  avatar  avatar

acceptto-sso-java-client's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.