【问题标题】:Custom Annotation JSF自定义注释 JSF
【发布时间】:2012-05-18 22:03:29
【问题描述】:

我想制作一个自定义注释来检查我的 JSF Web 应用程序的某些功能的安全性。对于安全性,我将 Tomcat 安全性与 JaaS 结合使用,因此我没有应用程序管理的安全性可供使用。

真正想要做的是在 Spring Security (@Secured("role")) 等 Backing Bean 中为我的方法做一个注解。我的安全系统已实现,因此每个功能都是一个角色,您可以动态创建“用户角色”,这些角色存储在数据库中,当有人登录该“用户角色”中的所有(功能)角色时,将在 tomcat 安全性中设置作为角色。

所以现在我有这段代码来检查我的用户是否可以访问该功能:

       public static void checkSecurity(final String function) {
    final FacesContext facesContext = FacesContext.getCurrentInstance();
    try {
        if (facesContext.getExternalContext().getRemoteUser() == null) {
            facesContext.getExternalContext().redirect("login.xhtml");
            return;
        }
        if (!facesContext.getExternalContext().isUserInRole(function)) {
            facesContext.getExternalContext().redirect("restricted.xhtml");
            return;
        }
    } catch (final Exception ex /* Mandatory "IOException e" will be caught + all other exceptions. */) {
        facesContext.getExternalContext().setResponseStatus(403); // HTTP Status 403: Forbidden. Can also throw 401.
        facesContext.responseComplete();
    }
}

现在我必须调用这个 SecurityUtil.checkSecurity("name_of_function");在每一种方法中。 但我想有这样的注释@CustomSecurity("function_name_role")。

   @Target(ElementType.METHOD)
   @Retention(RetentionPolicy.RUNTIME)
   public @interface CustomSecurity {
     // Single Element called value.
      String value();
   }

当一个方法有这个注解时, checkSecurity 函数必须自动执行。所以我必须在某一点上扫描这个注释,或者做一些动作监听器。 JSF 应该对此有一些选择,但我在这方面找到的所有论坛都没有真正的帮助。

有人有什么想法吗?

编辑: 我尝试了this blog 它可以工作,但仅适用于组件的操作(当您没有角色时,组件不会呈现)。那么当人们试图侵入 JSF 结构时,这是多么安全。我宁愿让它在每种方法上运行。

        public class SecurityActionListener extends ActionListenerImpl implements ActionListener {

    private static final Logger LOGGER = FacesLogger.APPLICATION.getLogger();

    @SuppressWarnings("unused")
    @Override
    public void processAction(final ActionEvent event) {

        final FacesContext context = FacesContext.getCurrentInstance();
        final Application application = context.getApplication();
        final ConfigurableNavigationHandler navHandler = (ConfigurableNavigationHandler) application.getNavigationHandler();

        // Action stuff
        final UIComponent source = event.getComponent();
        final ActionSource actionSource = (ActionSource) source;
        MethodBinding binding;

        binding = actionSource.getAction();
        final String expr = binding.getExpressionString();
        if (!expr.startsWith("#")) {
            super.processAction(event);
            return;
        }

        final int idx = expr.indexOf('.');
        final String target = expr.substring(0, idx).substring(2);
        final String t = expr.substring(idx + 1);
        final String method = t.substring(0, (t.length() - 1));

        final MethodExpression expression = new MethodExpressionMethodBindingAdapter(binding);
        final ELContext elContext = context.getELContext();
        final ExpressionFactory factory = context.getApplication().getExpressionFactory();

        final ValueExpression ve = factory.createValueExpression(elContext, "#{" + target + '}', Object.class);
        final Object result = ve.getValue(elContext);

        // Check if the target method is a secured method
        // and check security accordingly
        final Method[] methods = result.getClass().getMethods();
        for (final Method meth : methods) {
            if (meth.getName().equals(method)) {
                if (meth.isAnnotationPresent(CustomSecurity.class)) {
                    final CustomSecurity securityAnnotation = meth.getAnnotation(CustomSecurity.class);
                    System.out.println("Function to check security on: " + securityAnnotation.value()); // TODO TO LOG
                    SecurityUtil.checkSecurity(securityAnnotation.value());
                } else {
                    super.processAction(event);
                }
                break;
            }
        }
    }

}

这在 faces-config.xml 中: <action-listener> com.nielsr.randompackagebecauseofnda.SecurityActionListener </action-listener>

This blog 也可能是一个答案,但我不知道它如何与我的 JaaS Tomcat 安全性一起工作,因为安全性位于一个单独的项目中,作为独立 JAR 部署在 tomcat lib 文件夹中。

但我实际上并不知道我必须保护我的 Beans。因为我已将 Web.xml 中 1 页上的所有功能(又名角色见上文)配置为安全约束。并且仅当您必须对该组件具有权限或“function_role”时,我才会在页面上呈现组件。那么这是否足够安全?或者,如果有人有权使用页面上的功能,他是否可以自己渲染组件并入侵我的网站?

我对 JSF 不太熟悉,知道这一点,在 Controller 和 View 之间的那个额外的 JSF 抽象层中发生了什么? (我更像是一名 Spring MVC 开发人员,但由于要求我必须使用 JSF,但很高兴能拓宽我的知识。)

【问题讨论】:

  • 你用的是什么版本的Tomcat?
  • Tomcat 7. 但是安全性已经实现并且工作“很棒”;)

标签: java jsf jsf-2 annotations jaas


【解决方案1】:

您可以使用

“扫描您的注释”
http://code.google.com/p/reflections/

问候

【讨论】:

    猜你喜欢
    • 2013-05-21
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 2015-09-15
    相关资源
    最近更新 更多