java - How add FacesMessage to CDI security interceptor? -
i've created security check couple examples:
but unfortunatly can't see how add facesmesagges exception in case check fails.
my files:
checkaction
@inherited @interceptorbinding @retention(retentionpolicy.runtime) @target({ elementtype.method, elementtype.type }) public @interface checkaction { @nonbinding public esysobject object() default esysobject.none; @nonbinding public eaction action() default eaction.none; }
checkactioninterceptor
@interceptor @checkaction public class checkactioninterceptor implements serializable { private static final long serialversionuid = 1l; @aroundinvoke public object checkpermissions(invocationcontext context) throws exception { final checkaction annotation = context.getmethod().getannotation(checkaction.class); if (!isactionallowed(annotation.object(), annotation.action())) { throw new permissionexception("sorry don't have needed permissions"); } return context.proceed(); }
mybean
@named @viewscoped @logged public class pagecontroller implements serializable { private static final long serialversionuid = 1l; @checkaction(object = esysobject.dictionary, action = eaction.write) public string save() { switch (action) { case "create": case "edit": service.saveorupdate(cursor); break; } return "page?faces-redirect=true"; }
it's work.
but how handle permissionexception right? how facescontext.getcurrentinstance().addmessage("security check", new facesmessage("permission error", "you don't have needed permissions"));
so, i've done question.
in case i've found ansver:
checkactioninterceptor
@interceptor @checkaction public class checkactioninterceptor implements serializable { private static final long serialversionuid = 1l; @aroundinvoke public object checkpermissions(invocationcontext context) throws exception { final checkaction annotation = context.getmethod().getannotation(checkaction.class); if (!isactionallowed(annotation.object(), annotation.action())) { facescontext.addmessage("error", new facesmessage("permission error", text)); log.error(text); return null; } return context.proceed(); }
i not thow error, return null. , programm go further, not permit execute needed action/method.
Comments
Post a Comment