gpt4 book ai didi

java - 自定义注释 JSF

转载 作者:行者123 更新时间:2023-11-30 11:44:41 28 4
gpt4 key购买 nike

我想创建一个自定义注释来检查我的 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com