【问题标题】:Authorization in JAX-RSJAX-RS 中的授权
【发布时间】:2018-12-15 15:39:26
【问题描述】:

我正在开发一个使用 javaEE / Wildfly 和 JAX-RS 的应用程序来提供 restful 服务。

我有这种端点:

@POST
@Path("/add")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response addSchool(SchoolDto schoolDto, @HeaderParam("token") String userToken) {

    List<String> actionsNeeded = new ArrayList<String>(
            Arrays.asList(
                    "create school"
                    ));
    if (authService.userHasActionList(userToken, actionsNeeded) == false ) 
    {
        return authService.returnResponse(401);
    }

    Response addSchoolServiceResponse = schoolResponse.create(schoolDto);
    return addSchoolServiceResponse;
}

使用 Header 中的令牌,我的身份验证服务将检查用户帐户是否在其授权操作列表中具有使用检查点所必需的操作。

它正在工作,但我在每个检查点都重复这一点......我正在寻找一种方法来做到这一点:

@POST
@Path("/add")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Annotation("action 1 needed", "Action 2 needed")
public Response addSchool(SchoolDto schoolDto, @HeaderParam("token") String userToken) {        
    Response addSchoolServiceResponse = schoolResponse.create(schoolDto);
    return addSchoolServiceResponse;
}

一个注释,我可以在其中传递一些参数(我的操作,最重要的是能够拥有用户令牌),这些参数使用过滤器触发或任何安全检查返回 401 或在允许用户的情况下执行该方法在那里。

我发现了很多基于角色的安全性(@Secured 等...),但不是基于类似的操作

有人已经做过类似的事情了吗?

【问题讨论】:

  • 您是否真的尝试过为您的用例使用过滤器(如您遇到的帖子中所述)?如果是这样,您遇到了什么问题?如果没有,请至少尝试一下,让我们知道您遇到的问题。
  • 是的,有一种称为外部授权 (ABAC) 的模型可以满足您的需求。它允许您将授权表示为用 XACML 编写的策略。
  • 感谢您的回答,终于成功了!感谢 David Brossard 提供我不知道这种授权的信息,我将研究我的应用程序的未来发展!

标签: java rest authorization jax-rs


【解决方案1】:

最后我已经重新开始并且它正在工作,我的主要问题是访问标头中的令牌并使用注释,现在可以了(只需要坚持并再试一次我假设......)这里是它的样子:

@Provider
@Actions
public class AuthorizationFilter implements ContainerRequestFilter {

@EJB
AuthService authService;

@Context
private ResourceInfo resourceInfo;

List<String> actionsNeeded = new ArrayList<String>();

@Override
public void filter(ContainerRequestContext reqContext) throws IOException {
  Actions annotations = resourceInfo.getResourceMethod().getAnnotation(Actions.class);
  String token;

  try {
      token = reqContext.getHeaders().get("token").get(0);  
      for (String annotation : annotations.value()) {
          actionsNeeded.add(annotation);
      }
        if (authService.userHasActionList(token, actionsNeeded) == false ) 
        {
            reqContext.abortWith(authService.returnResponse(401));
            return;
        }
    } catch (Exception e) {
        System.out.println("Headers 'token' does not exist !");
        reqContext.abortWith(authService.returnResponse(400));
    }    

  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-04
    • 2015-01-22
    • 2018-02-01
    • 2012-02-10
    • 2012-07-27
    • 2018-11-07
    • 1970-01-01
    • 2014-08-08
    相关资源
    最近更新 更多