2016-12-02 51 views
0

中提供使用apache shiro进行Rest服务方法的授权在我的简单Web应用程序中,我有两种Web服务方法。一种是storeName(),另一种是viewAllNames()方法。在Java应用程序

管理员角色可以访问这两种方法。但用户角色应该访问viewAllNames()方法。

我打算使用Apache shiro,maven基于web的项目和休息服务。仅为这两种方法提供授权。

我的实际其余网址是:

http://localhost:8080/SimpleRest/simpleservice/store/jackreobert --> admin only 
http://localhost:8080/SimpleRest/simpleservice/viewall/   --> user only 

如何配置shiro.ini,web.xml和annotaion/XML。

对于shiro方法,我是否需要将任何其他信息传递到Web服务url,如何实现此目的。

SimpleService.java

package com.simple.rest; 
import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.Response; 
import org.apache.shiro.authz.annotation.RequiresRoles; 
import org.json.JSONException; 

@Path("/simpleservice") 
public class SimpleService { 
    @Path("/store/{name}") 
@GET 
@Produces("application/json") 
public Response storeName(@PathParam("name") String name) throws JSONException { 
/** 
* Here insert logic 
*/ 
String result = "Given name: " + name + " successfully stored"; 
return Respo}nse.status(200).entity(result).build(); 
} 
@Path("/viewall") 
@GET 
@Produces("application/json") 
public Response viewAllNames() throws JSONException { 
    /** 
* Here retrieve logic 
*/ 
String result = "All names are taken"; 
return Response.status(200).entity(result).build(); 
}} 

感谢您阅读我的文章。 帮我, 在此先感谢。

回答

0

我的建议是使用权限。使用权限保护您的应用程序资源。为角色分配权限并将角色分配给用户。

由于Shiro 1.4.0-RC2有官方的JAX-RS支持,请看sample