Я использую пользовательскую безопасность аннотаций, как описано в этой ссылке. Я обновляю свои управляемые компоненты jsf с @ManagedBean на @Named (CDI). Поскольку CDI не поддерживает область @ViewScoped, я использую org.omnifaces.cdi.ViewScoped. Однако в этом случае эта проверка не проходит if method.isAnnotationPresent(Permissao.class){}
. Как мне адаптировать проверку безопасности для использования с CDI + ViewScoped от Omnifaces?
Изменить
Использование @ManagedBean/ViewScoped(jsf) вместо @Named/ViewScoped(cdi + omnifaces) работает. Проблема возникает в этой строке if (metodo.isAnnotationPresent(Permissao.class)) {}
public void verificaPermissaoAcesso(ActionEvent event) {
final FacesContext facesContext = FacesContext.getCurrentInstance();
UIComponent source = event.getComponent();
ActionSource2 actionSource = (ActionSource2) source;
MethodExpression methodExpression = actionSource.getActionExpression();
String expressao = methodExpression.getExpressionString(); // #{nomeManagedBean.nomeMetodo(param)}
String nomeManagedBean = expressao.substring(0, expressao.indexOf('.')).substring(2);
String nomeMetodoComParenteses = expressao.substring(expressao.indexOf('.') + 1);
String nomeMetodo = nomeMetodoComParenteses.substring(0, nomeMetodoComParenteses.indexOf("("));
ELContext elContext = facesContext.getELContext();
ExpressionFactory factory = facesContext.getApplication().getExpressionFactory();
ValueExpression ve = factory.createValueExpression(elContext, "#{" + nomeManagedBean + '}', Object.class);
//Object jsfManagedBean = ve.getValue(elContext);
Context ctx = bm.getContext(org.omnifaces.cdi.ViewScoped.class);
Bean bean = bm.resolve(bm.getBeans(nomeManagedBean));
Object jsfManagedBeanProxy = ctx.get(bean);
List<Method> listaMetodos = Arrays.asList(jsfManagedBeanProxy.getClass().getMethods());
for (Method metodo : listaMetodos) {
if (nomeMetodo.equals(metodo.getName())) {
if (metodo.isAnnotationPresent(Permissao.class)) {
Permissao anotacaoSeguranca = metodo.getAnnotation(Permissao.class);
SegurancaUtil.verificar(anotacaoSeguranca.acoes());
break;
}
}
}
}
Мой класс аннотаций
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Permissao {
String[] acoes();
}
Мой аннотированный метод в управляемом компоненте jsf
@Permissao(acoes={"permission1", "permission2"})
public void myMethod(long id) {}
Редактировать 2 — Решение от @meriton
Context ctx = bm.getContext(org.omnifaces.cdi.ViewScoped.class);
Bean bean = bm.resolve(bm.getBeans(nomeManagedBean));
Object jsfManagedBeanProxy = ctx.get(bean);