java - 自定义注释 JSF

标签 java jsf jsf-2 annotations jaas

我想创建一个自定义注释来检查我的 JSF 网络应用程序某些功能的安全性。为了安全起见,我将 Tomcat 安全性与 JaaS 一起使用,因此我没有可供我使用的应用程序管理的安全性。

实际上想要做的是在像 Spring Security (@Secured("role")) 这样的 Backing Beans 中为我的方法做一个注释。我的安全系统已实现,因此每个功能都是一个角色,您可以动态创建“用户角色”,这些角色存储在数据库中,当有人登录该“用户角色”中的所有(功能)角色时,将在 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 函数。所以我必须在某个时候扫描这个注释,或者做某种 Action 监听器。 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 中一页上的所有功能(又名角色见上文)配置为安全约束。只有当您对该组件具有权限或“function_role”时,我才会在页面上呈现这些组件。那么这是否足够安全?或者,如果某人有权使用页面上的某个功能,他可以自己呈现组件并因此黑掉我的网站吗?

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

最佳答案

您可以使用

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

问候

关于java - 自定义注释 JSF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10660273/

相关文章:

java - 服务器重启后 HttpSession 仍然存在

java - JSF 中的 boolean 值到自定义字符串转换器

jsf-2 - background-image: url ("") 在 CSS h:outputStylesheet 没有加载

javascript - jQuery 和 primefaces 冲突

java - 在 Windows 任务管理器中更改 java 进程描述

java - java中如何将字符串转换为字节?

java - 在 JAX-RS 中的每个 @POST 或 @DELETE 请求之前或之后调用方法

jsf - p :fileUpload uploaded file saved and how do I change it? 在哪里

jsf - 如何向 JSF 输入组件添加占位符属性?

validation - JSF:根据验证成功或失败有条件地更新