GithubHelp home page GithubHelp logo

tmarwen / shiro-jersey Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cemo/shiro-jersey

0.0 2.0 0.0 134 KB

Extension for Apache Shiro to work with Sun Jersey.

License: Apache License 2.0

Java 100.00%

shiro-jersey's Introduction

Apache Shiro Jersey Extension

Extension for Apache Shiro to work with [Sun Jersey] (http://jersey.java.net "Sun Jersey"). This extension enables Shiro annotations in the JAX-RS implementation [Sun Jersey] (http://jersey.java.net "Sun Jersey").

If the user is not authenticated Http Status Code 401 is returned. If the user has insufficient privileges Status Code 403 is returned. This extension provides security using Apache Shiro similar to the standard JAX-WS-RS security for Jersey.

The following Shiro Annotations are supported:

  • RequiresPermissions
  • RequiresRoles (combined with AND only)

Examples

Checking Permissions with @RequiresPermissions

@Path("/changelog") 
@RequiresPermissions("repository:read") 
public class ChangelogResourceImpl { 

   @POST 
   @Consumes(MediaType.APPLICATION_JSON) 
   @Path("/addObject") 
   @RequiresPermissions("repository:write") 
   public Response addObject(ObjectJson objectJson) { 
      someService.addObject(object); 
      return Response.ok().build(); 
   }

} 

Checking Roles with @RequiresRoles

@Path("/changelog") 
@RequiresRoles("Admin") 
public class ChangelogResourceImpl { 

   @POST 
   @Consumes(MediaType.APPLICATION_JSON) 
   @Path("/addObject") 
   @RequiresRoles("Chief") 
   public Response addObject(ObjectJson objectJson) { 
      someService.addObject(object); 
      return Response.ok().build(); 
   }

} 

Sample Project

A sample project is provided in the directory jersey-sample. The sample project contains a Jersey application with REST resources protected by the Jersey extension for Shiro. The sample project uses HTTP Basic authentication.

Users

  • root/secret with role admin
  • guest/guest with role guest

Resources

Hello Resource

The hello resource takes one parameter and displays a plain text message with the given parameter.

http://localhost:9080/api/hello/buddy

Admin Resource

The admin resource can only be accessed by users with role admin.

http://localhost:9080/api/admin

Running the Sample Project

You can run the sample project either using maven with the command:

maven jetty:run

You can also use the Eclipse Web Tooling to run the sample project from Eclipse.

Getting Started

Using the Jersey Extension for Shiro is pretty easy. Just add an init parameter to the configuration of your Jersey servlet as shown below.

<servlet>
	<servlet-name>jersey-servlet</servlet-name>
	<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
	<init-param>
		<param-name>com.sun.jersey.config.property.packages</param-name>
		<param-value>org.apache.shiro.jersey.sample</param-value>
	</init-param>
    <init-param>
        <param-name>com.sun.jersey.spi.container.ResourceFilters</param-name>
        <param-value>org.apache.shiro.jersey.ShiroResourceFilterFactory</param-value>
    </init-param>
	<load-on-startup>1</load-on-startup>
</servlet>

OSGi Support

The Apache Shiro Jersey Extension fully supports OSGi and is provided as valid OSGi bundle.

shiro-jersey's People

Contributors

tmarwen avatar

Watchers

 avatar  avatar

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.