public class Auth extends Object implements Jooby.Module
Authentication module via: pac4j.
ClientsWebContext as RequestScopedRoute.Filter per each registered ClientRoute.Filter
{
get("/public", () -> ..);
use(new Auth());
get("/private", () -> ..);
}
Previous example adds a very basic but ready to use form login auth every time you try to
access to /private or any route defined below the auth module.
pac4j is a powerful library that supports multiple clients and/or authentication protocols. In the next example, we will see how to configure the most basic of them, but also some complex protocols.
If basic auth is all you need, then:
{
use(new Auth().basic());
}
A BasicAuthClient depends on UsernamePasswordAuthenticator, default is
SimpleTestUsernamePasswordAuthenticator which is great for development, but nothing good
for other environments. Next example setup a basic auth with a custom:
UsernamePasswordAuthenticator:
{
use(new Auth().basic("*", MyUsernamePasswordAuthenticator.class));
}
Form authentication will be activated by calling form():
{
use(new Auth().form());
}
Form is the default authentication method so previous example is the same as:
{
use(new Auth());
}
Like basic auth, form auth depends UsernamePasswordAuthenticator and a
UsernameProfileCreator.
A login form will be ready under the path: /login. Again, it is a very basic login
form useful for development. If you need a custom login page, just add a route before the
Auth module, like:
{
get("/login", () -> Results.html("login"));
use(new Auth());
}
Simply and easy!
Twitter, example:
{
use(new Auth()
.client(conf ->
new TwitterClient(conf.getString("twitter.key"), conf.getString("twitter.secret"))));
}
Keep in mind you will have to add the require Maven dependency to your project, beside that it is pretty straight forward.
By default a Client will protect all the urls defined below the module, because routes in
Jooby are executed in the order they where defined.
You can customize what urls are protected by specifying a path pattern:
{
use(new Auth().form("/private/**"));
get("/hello", () -> "no auth");
get("/private", () -> "auth");
}
Here the /hello path is un-protected, because the client will intercept everything
under /private.
Jooby relies on AuthStore for saving and retrieving a UserProfile. By default,
the UserProfile is stored in the Session via AuthSessionStore.
After a successful authentication the UserProfile is accessible as a request scoped
attribute:
{
use(new Auth().form());
get("/private", req -> req.require(HttpProfile.class));
}
facebook (or any oauth, openid, etc...)
{
use(new Auth().client(new FacebookClient(key, secret));
get("/private", req -> req.require(FacebookProfile.class));
}
Custom AuthStore is provided via store(Class) method:
{
use(new Auth().store(MyDbStore.class));
get("/private", req -> req.require(HttpProfile.class));
}
A default /logout handler is provided it too. The handler will remove the profile
from AuthStore by calling the AuthStore.unset(String) method. The default login
will redirect to /.
A custom logout and redirect urls can be set via .conf file or programmatically:
{
use(new Auth().logout("/mylogout", "/redirectTo"));
}
| Constructor and Description |
|---|
Auth() |
| Modifier and Type | Method and Description |
|---|---|
Auth |
basic()
Add a basic auth client, protecting all the urls
*. |
Auth |
basic(String pattern)
Add a basic auth client.
|
Auth |
basic(String pattern,
Class<? extends org.pac4j.http.credentials.UsernamePasswordAuthenticator> authenticator)
Add a basic auth client.
|
Auth |
basic(String pattern,
Class<? extends org.pac4j.http.credentials.UsernamePasswordAuthenticator> authenticator,
Class<? extends org.pac4j.http.profile.UsernameProfileCreator> profileCreator)
Add a basic auth client.
|
<C extends org.pac4j.core.credentials.Credentials,U extends org.pac4j.core.profile.UserProfile> |
client(org.pac4j.core.client.Client<C,U> client)
Add an auth client, like facebook, twitter, github, etc...Please note the require dependency
must be in the classpath.
|
<C extends org.pac4j.core.credentials.Credentials,U extends org.pac4j.core.profile.UserProfile> |
client(Function<com.typesafe.config.Config,org.pac4j.core.client.Client<C,U>> provider)
Add an auth client, like facebook, twitter, github, etc...Please note the require dependency
must be in the classpath.
|
<C extends org.pac4j.core.credentials.Credentials,U extends org.pac4j.core.profile.UserProfile> |
client(String pattern,
org.pac4j.core.client.Client<C,U> client)
Add an auth client, like facebook, twitter, github, etc...Please note the require dependency
must be in the classpath.
|
<C extends org.pac4j.core.credentials.Credentials,U extends org.pac4j.core.profile.UserProfile> |
client(String pattern,
Function<com.typesafe.config.Config,org.pac4j.core.client.Client<C,U>> provider)
Add an auth client, like facebook, twitter, github, etc...Please note the require dependency
must be in the classpath.
|
com.typesafe.config.Config |
config() |
void |
configure(Env env,
com.typesafe.config.Config config,
com.google.inject.Binder binder) |
Auth |
form()
Add a form auth client, protecting all the urls
*. |
Auth |
form(String pattern)
Add a form auth client.
|
Auth |
form(String pattern,
Class<? extends org.pac4j.http.credentials.UsernamePasswordAuthenticator> authenticator)
Add a form auth client.
|
Auth |
form(String pattern,
Class<? extends org.pac4j.http.credentials.UsernamePasswordAuthenticator> authenticator,
Class<? extends org.pac4j.http.profile.UsernameProfileCreator> profileCreator)
Add a form auth client.
|
Auth |
logout(String logoutUrl)
Set the logout and redirect URL patterns.
|
Auth |
logout(String logoutUrl,
String redirecTo)
Set the logout and redirect URL patterns.
|
<U extends org.pac4j.core.profile.UserProfile> |
store(Class<? extends AuthStore<U>> store)
Setup the
AuthStore to use. |
public static final String ID
public Auth form(String pattern, Class<? extends org.pac4j.http.credentials.UsernamePasswordAuthenticator> authenticator, Class<? extends org.pac4j.http.profile.UsernameProfileCreator> profileCreator)
pattern - URL pattern to protect.authenticator - Authenticator to use.profileCreator - Profile creator to use.public Auth form(String pattern, Class<? extends org.pac4j.http.credentials.UsernamePasswordAuthenticator> authenticator)
UsernameProfileCreator.pattern - URL pattern to protect.authenticator - Authenticator to use.public Auth form(String pattern)
SimpleTestUsernamePasswordAuthenticator and a
UsernameProfileCreator. Useful for development.pattern - URL pattern to protect.public Auth form()
*. It setup a
SimpleTestUsernamePasswordAuthenticator and a UsernameProfileCreator. Useful
for development.public Auth basic(String pattern, Class<? extends org.pac4j.http.credentials.UsernamePasswordAuthenticator> authenticator, Class<? extends org.pac4j.http.profile.UsernameProfileCreator> profileCreator)
pattern - URL pattern to protect.authenticator - Authenticator to use.profileCreator - Profile creator to use.public Auth basic(String pattern, Class<? extends org.pac4j.http.credentials.UsernamePasswordAuthenticator> authenticator)
UsernameProfileCreator.pattern - URL pattern to protect.authenticator - Authenticator to use.public Auth basic(String pattern)
SimpleTestUsernamePasswordAuthenticator and a
UsernameProfileCreator. Useful for development.pattern - URL pattern to protect.public Auth basic()
*. It setup a
SimpleTestUsernamePasswordAuthenticator and a UsernameProfileCreator. Useful
for development.public <C extends org.pac4j.core.credentials.Credentials,U extends org.pac4j.core.profile.UserProfile> Auth client(org.pac4j.core.client.Client<C,U> client)
C - Credentials.U - UserProfile.client - Client to add.public <C extends org.pac4j.core.credentials.Credentials,U extends org.pac4j.core.profile.UserProfile> Auth client(String pattern, org.pac4j.core.client.Client<C,U> client)
C - Credentials.U - UserProfile.pattern - URL pattern to protect.client - Client to add.public <C extends org.pac4j.core.credentials.Credentials,U extends org.pac4j.core.profile.UserProfile> Auth client(Function<com.typesafe.config.Config,org.pac4j.core.client.Client<C,U>> provider)
C - Credentials.U - UserProfile.provider - Client to add.public <C extends org.pac4j.core.credentials.Credentials,U extends org.pac4j.core.profile.UserProfile> Auth client(String pattern, Function<com.typesafe.config.Config,org.pac4j.core.client.Client<C,U>> provider)
C - Credentials.U - UserProfile.pattern - URL pattern to protect.provider - Client to add.public <U extends org.pac4j.core.profile.UserProfile> Auth store(Class<? extends AuthStore<U>> store)
AuthStore to use. Keep in mind the store is binded it as singleton.store - Store to use.public Auth logout(String logoutUrl, String redirecTo)
logoutUrl - Logout url, default is /logout.redirecTo - Redirect url, default is /.public Auth logout(String logoutUrl)
logoutUrl - Logout url, default is /logout.public void configure(Env env, com.typesafe.config.Config config, com.google.inject.Binder binder)
configure in interface Jooby.Modulepublic com.typesafe.config.Config config()
config in interface Jooby.ModuleCopyright © 2015. All rights reserved.