我想制作一个自定义注释来检查我的JSF Web应用程序的某些功能的安全性。为了安全起见,我将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函数。所以我必须在某一点扫描这个注释,或者做一些actionlistener。 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安全性一起使用,因为安全性是在tomcat lib文件夹中作为独立JAR部署的单独项目中。
但实际上我不知道我必须保护我的豆子。因为我已经将Web.xml中位于1页的所有函数(也就是上面的角色,如上所述)配置为安全性约束。只有当你需要对该组件拥有权限或“function_role”时,才会在页面上呈现组件。这样安全吗?或者,如果某人有权在页面上使用某个功能,他是否可以自己渲染组件并破解我的网站?
我不熟悉JSF知道这一点,在Controller和View之间的额外JSF抽象层中发生了什么? (我更像是一个Spring MVC开发人员,但由于要求我必须使用JSF,但扩展我的知识很好。)
答案 0 :(得分:1)
您可以使用
“扫描您的注释”http://code.google.com/p/reflections/
此致