Play Framework's Authorization Middleware based on Casbin

Clone this repo:
  1. 5373294 feat: upgrade CI Node.js version to 20 (#13) by Liao Xin · 1 year, 5 months ago master v1.2.0
  2. 7c00c43 feat: fix pom.xml to fix CI for publishing to https://central.sonatype.com (#12) by Liao Xin · 1 year, 5 months ago
  3. 0429abd fix: fix broken links (#10) by YunShu · 2 years, 7 months ago v1.1.2
  4. 5094605 fix: update node version (#8) by YunShu · 2 years, 8 months ago v1.1.1
  5. efba870 docs: replace gitter links with discord (#7) by YunShu · 2 years, 8 months ago

play-authz

codebeat badge Build Status codecov javadoc Maven Central Discord

About The Project

This provides jcasbin support to Play Framework.

Policy can be defined either in a .conf file or through JDBC adapter.

Getting Started

Dependency

(versions used for the project -sbt)

  "org.casbin" % "jcasbin" % "1.24.0",
  "org.casbin" % "jdbc-adapter" % "2.3.3",
  "org.postgresql" % "postgresql" % "42.5.0"

Configuration

  1. Default:
casbin {
  model = conf/casbin/model.conf
  policy = conf/casbin/policy.csv
  storeType = jdbc
  
  ...
  
}
  1. To use model, policy files:
  • Make sure to add your model and policy files in casbin directory which is under the conf directory.
  • Set storeType = file

Note: If model file is not provided then default model will be used.

  1. To use JDBC Adapter for defining policy:
  • Add your rules in casbin_rule table (will be created by default if not present).

Config example:

db.default {
  driver = org.someDriver.Driver
  url = "jdbc:example:example:play"
  username = user
  password = password
  
  ...
  
}

Usage

You can use enforcer by doing dependency injection in your controller.

@Inject
public PostResourceHandler(... other params, CasbinEnforcer enforcer) {
    ...
    this.enforcer = enforcer;
}

Then everything else stays the same.

if(enforcer.enforce(role, resource, action)) {
    // some logic
}