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();
}}
感謝您閱讀我的文章。 幫我, 在此先感謝。