public class Pac4j extends Object implements Jooby.Module
Authentication module via: pac4j.
Display a basic login-form and restrict access to all the routes defined after the
Pac4j module:
{
get("/public", () {@literal ->} {
...
});
use(new Pac4j());
get("/private", () {@literal ->} {
...
});
}
A Client represents an authentication mechanism. It performs the login process and returns (if successful) a user profile
Clients are configured at bootstrap time using the Pac4j DSL:
{
use(new Pac4j()
.client(conf -> {
return new FacebookClient(conf.getString("fb.key"), conf.getString("fb.secret"));
})
);
}
You can chain calls to add multiple clients:
{
use(new Pac4j()
.client(conf -> {
return new FormClient("/login", new SimpleTestSimpleTestUsernamePasswordAuthenticator());
})
.client(conf -> {
return new FacebookClient(conf.getString("fb.key"), conf.getString("fb.secret"));
})
.client(conf -> {
return new TwitterClient(conf.getString("twitter.key"), conf.getString("twitter.secret"));
})
);
}
By default Pac4j restrict access to all the routes defined after the Pac4j module. You can specify what url must be protected using a path pattern:
{
use(new Pac4j()
.client("/admin/**", conf -> {
return new FormClient("/login", new SimpleTestSimpleTestUsernamePasswordAuthenticator());
}));
}
Now all the routes under /admin are protected by Pac4j.
After login the user profile (current logged user) is accessible via require calls:
{
use(new Pac4j().form());
get("/profile", () -> {
CommonProfile profile = require(CommonProfile.class);
...
});
}
Access to specific profile type depends on the authentication client:
{
use(new Pac4j()
.client(conf -> {
return new FacebookClient(conf.getString("fb.key"), conf.getString("fb.secret"));
})
);
get("/profile", () -> {
FacebookProfile profile = require(FacebookProfile.class);
...
});
}
Or if you prefer the pac4j API:
{
use(new Pac4j()
.client(conf -> {
return new FacebookClient(conf.getString("fb.key"), conf.getString("fb.secret"));
})
);
get("/profile", req -> {
ProfileManager pm = require(ProfileManager.class);
List<CommonProfile> profiles = pm.getAll(req.ifSession().isPresent());
...
});
}
Authorizers are provided via client DSL. You can provider an instance of an
auhtorizer or class reference to an authorizer.
{
use(new Pac4j()
.client("*", MyAuthorizer.class, conf -> {
return new FacebookClient(conf.getString("fb.key"), conf.getString("fb.secret"));
})
);
}
Here MyAuthorizer will be provisioned by Guice.
For advanced usage is available via doWith(Consumer) method:
{
use(new Pac4j()
.doWith(pac4j -> {
pac4j.setSecurityLogic(...);
pac4j.setHttpActionAdapter(...);
})
);
}
| Constructor and Description |
|---|
Pac4j()
Creates a new Pac4j module.
|
| Modifier and Type | Method and Description |
|---|---|
<C extends org.pac4j.core.credentials.Credentials,U extends org.pac4j.core.profile.CommonProfile> |
client(Function<com.typesafe.config.Config,org.pac4j.core.client.Client<C,U>> client)
Add a pac4j client and protected all the routes defined after the module:
|
<C extends org.pac4j.core.credentials.Credentials,U extends org.pac4j.core.profile.CommonProfile> |
client(String pattern,
org.pac4j.core.authorization.authorizer.Authorizer<U> authorizer,
Function<com.typesafe.config.Config,org.pac4j.core.client.Client<C,U>> client)
Add a pac4j client, protected all the routes defined after that matches the pattern and attach
an authorizer:
|
<C extends org.pac4j.core.credentials.Credentials,U extends org.pac4j.core.profile.CommonProfile> |
client(String pattern,
Class<? extends org.pac4j.core.authorization.authorizer.Authorizer> authorizer,
Function<com.typesafe.config.Config,org.pac4j.core.client.Client<C,U>> client)
Add a pac4j client, protected all the routes defined after that matches the pattern and attach
an authorizer:
|
<C extends org.pac4j.core.credentials.Credentials,U extends org.pac4j.core.profile.CommonProfile> |
client(String pattern,
Function<com.typesafe.config.Config,org.pac4j.core.client.Client<C,U>> client)
Add a pac4j client and protected all the routes defined after that matches the pattern:
|
com.typesafe.config.Config |
config() |
void |
configure(Env env,
com.typesafe.config.Config conf,
com.google.inject.Binder binder) |
Pac4j |
doWith(BiConsumer<org.pac4j.core.config.Config,com.typesafe.config.Config> configurer)
Configurer pa4j options, only necessary it you want to provide your own pac4j components.
|
Pac4j |
doWith(Consumer<org.pac4j.core.config.Config> configurer)
Configurer pa4j options, only necessary it you want to provide your own pac4j components.
|
Pac4j |
form()
Add a simple login form.
|
Pac4j |
form(String pattern)
Add a simple login form.
|
Pac4j |
multiProfile(boolean multiProfile)
Set pac4j option multiProfile.
|
Pac4j |
unauthenticated(Function<Request,org.pac4j.core.profile.UserProfile> provider)
Set a default action which is execute when no user is logged in.
|
Pac4j |
unauthenticated(Supplier<org.pac4j.core.profile.UserProfile> provider)
Set a default action which is execute when no user is logged in.
|
public Pac4j doWith(Consumer<org.pac4j.core.config.Config> configurer)
{
use(new Pac4j()
.doWith(pac4j -> {
pac4j.setSecurityLogic(...);
pac4j.setHttpActionAdapter(...);
})
);
}
configurer - Configurer callback.public Pac4j doWith(BiConsumer<org.pac4j.core.config.Config,com.typesafe.config.Config> configurer)
{
use(new Pac4j()
.doWith((pac4j, conf) -> {
pac4j.setSecurityLogic(...);
pac4j.setHttpActionAdapter(...);
})
);
}
configurer - Configurer callback.public <C extends org.pac4j.core.credentials.Credentials,U extends org.pac4j.core.profile.CommonProfile> Pac4j client(Function<com.typesafe.config.Config,org.pac4j.core.client.Client<C,U>> client)
{
use(new Pac4j()
.client(conf -> {
return new FacebookClient(conf.getString("fb.key"), conf.getString("fb.secret"));
})
);
// protected routes
}
C - Credential type.U - User profile type.client - Client provider.public <C extends org.pac4j.core.credentials.Credentials,U extends org.pac4j.core.profile.CommonProfile> Pac4j client(String pattern, Function<com.typesafe.config.Config,org.pac4j.core.client.Client<C,U>> client)
{
use(new Pac4j()
.client("/admin/**", conf -> {
return new FacebookClient(conf.getString("fb.key"), conf.getString("fb.secret"));
})
);
// all routes at /admin are now protected.
}
C - Credential type.U - User profile type.pattern - Pattern to protect.client - Client provider.public <C extends org.pac4j.core.credentials.Credentials,U extends org.pac4j.core.profile.CommonProfile> Pac4j client(String pattern, Class<? extends org.pac4j.core.authorization.authorizer.Authorizer> authorizer, Function<com.typesafe.config.Config,org.pac4j.core.client.Client<C,U>> client)
{
use(new Pac4j()
.client("*", MyAuthorizer.class, conf -> {
return new FacebookClient(conf.getString("fb.key"), conf.getString("fb.secret"));
})
);
}
The authorizer will be provisioned by Guice.
C - Credential type.U - User profile type.pattern - Pattern to protect.authorizer - Authorizer.client - Client provider.public <C extends org.pac4j.core.credentials.Credentials,U extends org.pac4j.core.profile.CommonProfile> Pac4j client(String pattern, org.pac4j.core.authorization.authorizer.Authorizer<U> authorizer, Function<com.typesafe.config.Config,org.pac4j.core.client.Client<C,U>> client)
{
use(new Pac4j()
.client("*", new MyAuthorizer(), conf -> {
return new FacebookClient(conf.getString("fb.key"), conf.getString("fb.secret"));
})
);
}
C - Credential type.U - User profile type.pattern - Pattern to protect.authorizer - Authorizer.client - Client provider.public Pac4j unauthenticated(Function<Request,org.pac4j.core.profile.UserProfile> provider)
{
use(new Pac4j()
.unauthenticated(req -> {
UserProfile anonymous = ...
return anonymous;
})
);
get("/", () -> {
// might or might not be anonymous
UserProfile profile = require(UserProfile.class);
return ...;
}
}
The default action throws a 403 error.provider - Unauthenticated user provider.public Pac4j unauthenticated(Supplier<org.pac4j.core.profile.UserProfile> provider)
{
use(new Pac4j()
.unauthenticated(() -> {
UserProfile anonymous = ...
return anonymous;
})
);
get("/", () -> {
// might or might not be anonymous
UserProfile profile = require(UserProfile.class);
return ...;
}
}
The default action throws a 403 error.provider - Unauthenticated user provider.public Pac4j multiProfile(boolean multiProfile)
multiProfile - True for multiprofile.public Pac4j form()
public Pac4j form(String pattern)
pattern - Pattern to protect.verpublic void configure(Env env, com.typesafe.config.Config conf, com.google.inject.Binder binder) throws Throwable
configure in interface Jooby.ModuleThrowablepublic com.typesafe.config.Config config()
config in interface Jooby.ModuleCopyright © 2018. All rights reserved.