scalatrashiro


License

License

Categories

Categories

Scala Languages
GroupId

GroupId

com.github.sammyrulez
ArtifactId

ArtifactId

scalatrashiro_2.11
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

scalatrashiro
scalatrashiro
Project URL

Project URL

https://github.com/sammyrulez/ScalatraShiro
Project Organization

Project Organization

com.github.sammyrulez
Source Code Management

Source Code Management

https://github.com/sammyrulez/ScalatraShiro

Download scalatrashiro_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.sammyrulez/scalatrashiro_2.11/ -->
<dependency>
    <groupId>com.github.sammyrulez</groupId>
    <artifactId>scalatrashiro_2.11</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.sammyrulez/scalatrashiro_2.11/
implementation 'com.github.sammyrulez:scalatrashiro_2.11:1.1.0'
// https://jarcasting.com/artifacts/com.github.sammyrulez/scalatrashiro_2.11/
implementation ("com.github.sammyrulez:scalatrashiro_2.11:1.1.0")
'com.github.sammyrulez:scalatrashiro_2.11:jar:1.1.0'
<dependency org="com.github.sammyrulez" name="scalatrashiro_2.11" rev="1.1.0">
  <artifact name="scalatrashiro_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.sammyrulez', module='scalatrashiro_2.11', version='1.1.0')
)
libraryDependencies += "com.github.sammyrulez" % "scalatrashiro_2.11" % "1.1.0"
[com.github.sammyrulez/scalatrashiro_2.11 "1.1.0"]

Dependencies

compile (6)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.6
org.scalatra : scalatra_2.11 jar 2.3.0
org.scalatra : scalatra-scalate_2.11 jar 2.3.0
org.scalatra : scalatra-auth_2.11 jar 2.3.0
org.apache.shiro : shiro-core jar 1.2.4
org.apache.shiro : shiro-web jar 1.2.4

runtime (1)

Group / Artifact Type Version
ch.qos.logback : logback-classic jar 1.0.6

test (4)

Group / Artifact Type Version
org.scalatra : scalatra-scalatest_2.11 jar 2.3.0
junit : junit jar 4.12
org.eclipse.jetty : jetty-webapp jar 8.1.8.v20121106
org.eclipse.jetty.orbit : javax.servlet jar 3.0.0.v201112011016

Project Modules

There are no modules declared in this project.

ScalatraShiro

Build Status Coverage Status Dependency Status

ScalatraShiro is an integration to enable the use of Apache Shiro in Scalatra Application. It is not a replacement for scalatra auth (Scentry) but extends it with a authorization layer ( roles and permissions). Also Shiro provides several implementations for persistence and integrations with SSO systems ( CAS etc.)

##Installation

###Maven

<dependency>
			<groupId>com.github.sammyrulez</groupId>
			<artifactId>scalatrashiro_2.11</artifactId>
			<version>1.0.2</version>
</dependency>

SBT

libraryDependencies += "com.github.sammyrulez" %% "scalatrashiro" % "1.0.2"

##Usage

In order to make use of Shiro in our web application we must first define a Shiro servlet filter. Any requests that we want to secure must go through this Shiro filter.

<listener>
        <listenerclass>
          org.apache.shiro.web.env.EnvironmentLoaderListener
        </listener-class>
    </listener>

    <filter>
        <filter-name>securityFilter</filter-name>
        <filterclass>
          org.apache.shiro.web.servlet.ShiroFilter
        </filter-class>
    </filter>
    <filter-mapping>
            <filter-name>securityFilter</filter-name>
            <url-pattern>/*</url-pattern>
            <dispatcher>REQUEST</dispatcher>
            <dispatcher>FORWARD</dispatcher>
            <dispatcher>INCLUDE</dispatcher>
            <dispatcher>ERROR</dispatcher>
        </filter-mapping>

Configure Shiro through an INI file. For complete details refer to Shiro Documentation

This is an example of minimal configuration with two users ( 'testuser' and 'admin' ) and two roles ('user' and 'admin')

[main]
securityManager.rememberMeManager.cookie.name = rememberMe
# Create a Session Manager
sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
securityManager.sessionManager = $sessionManager

# Session Timeout = 1 hour (3600000 miliseconds)
securityManager.sessionManager.globalSessionTimeout = 3600000

sessionDAO = org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
securityManager.sessionManager.sessionDAO = $sessionDAO

# Login URL:
user.loginUrl = /login

[users]
testUser = password, user
admin = admin, admin

[roles]
user = BasicAccess
admin = *

Every controller you want to perform security checks must extends the Authentication Trait

class MainServlet extends ScalatraServlet with Authentication with ...

override protected val loginUrl: String = "/login"

You might choose to run the authorization checks in a before() filter in your controller, rather than hitting it in each action, to secure every method. As a best practice you should group routes with same access policies in one controller.

###Login and Logout

If you just have a user/password based authentication a UserAuthServlet is provided by ScalatraShiro. Just register it in your bootstrap class and point to the login/logout routes.

If you have complex / custom authentication you can use Apache Shiro authentication api directly

    val currentUser = SecurityUtils.getSubject()

    try {
      val token = // generate a  org.apache.shiro.authc.AuthenticationToken

        currentUser.login(token)
      } catch {
            case uae: UnknownAccountException => {
             ...
            }
            case ice: IncorrectCredentialsException => {
              ...
            }
            case lae: LockedAccountException => {
             ...
            }
            case ae: AuthenticationException => {
              ...
            }
          }

###Access control methods

You can check both roles and permissions

  • requiresAuthentication: current user must be authenticated

  • requiresRole: current user must have the role specified as parameter ( or at least one of the role if a List[String] is passed )

  • requiresAllRoles: current user must have all the roles in a List[String] specified as parameter

  • requiresPermission: current user must have the permission specified as parameter

  • requiresAllPermissions: current user must have all the permissions in a List[String] specified as parameter

##Credits

Original work on Scalatra / Apache Shiro integration by Ethan Way (blog post http://ethanway.com/securing-scalatra-with-apache-shiro/ source: https://github.com/waye929/ScalatraShiro)

Versions

Version
1.1.0
1.0.2