【问题标题】:Provide authorisation using apache shiro for Rest service method in java application在 java 应用程序中使用 apache shiro 为 Rest 服务方法提供授权
【发布时间】:2017-04-16 21:29:55
【问题描述】:

在我的简单 Web 应用程序中,我有两种 Web 服务方法。一个是storeName(),另一个是viewAllNames() 方法。

管理员角色可以访问这两种方法。但用户角色应该是访问 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();
}}

感谢您阅读我的帖子。 帮我, 提前致谢。

【问题讨论】:

    标签: apache rest authorization shiro


    【解决方案1】:

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

    从 Shiro 1.4.0-RC2 开始,有官方的 JAX-RS 支持,看看sample

    【讨论】:

      猜你喜欢
      • 2015-05-13
      • 2014-07-17
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      • 2016-03-07
      • 2013-11-28
      • 2016-10-05
      相关资源
      最近更新 更多