You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by bo...@apache.org on 2006/07/23 14:48:15 UTC
svn commit: r424728 -
/myfaces/tobago/trunk/example/security/src/main/java/org/apache/myfaces/tobago/example/security/CheckAuthorisationMethodBinding.java
Author: bommel
Date: Sun Jul 23 05:48:14 2006
New Revision: 424728
URL: http://svn.apache.org/viewvc?rev=424728&view=rev
Log:
next steps secured methodbinding
Modified:
myfaces/tobago/trunk/example/security/src/main/java/org/apache/myfaces/tobago/example/security/CheckAuthorisationMethodBinding.java
Modified: myfaces/tobago/trunk/example/security/src/main/java/org/apache/myfaces/tobago/example/security/CheckAuthorisationMethodBinding.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/security/src/main/java/org/apache/myfaces/tobago/example/security/CheckAuthorisationMethodBinding.java?rev=424728&r1=424727&r2=424728&view=diff
==============================================================================
--- myfaces/tobago/trunk/example/security/src/main/java/org/apache/myfaces/tobago/example/security/CheckAuthorisationMethodBinding.java (original)
+++ myfaces/tobago/trunk/example/security/src/main/java/org/apache/myfaces/tobago/example/security/CheckAuthorisationMethodBinding.java Sun Jul 23 05:48:14 2006
@@ -26,11 +26,16 @@
import javax.faces.context.FacesContext;
import javax.faces.component.StateHolder;
import javax.faces.component.UIComponentBase;
+import javax.faces.application.FacesMessage;
import javax.annotation.security.RolesAllowed;
import javax.annotation.security.DenyAll;
import javax.annotation.security.PermitAll;
import java.util.Arrays;
+import java.util.Map;
+import java.util.HashMap;
import java.lang.reflect.Method;
+import java.lang.reflect.AnnotatedElement;
+import java.lang.annotation.Annotation;
/**
* Created by IntelliJ IDEA.
@@ -41,6 +46,8 @@
*/
public class CheckAuthorisationMethodBinding extends MethodBinding implements StateHolder {
private static final Log LOG = LogFactory.getLog(CheckAuthorisationMethodBinding.class);
+ private static final Map<String,Annotation> authorisationCache = new HashMap<String,Annotation>();
+
private MethodBinding methodBinding;
public CheckAuthorisationMethodBinding() {
@@ -60,59 +67,98 @@
public Object invoke(FacesContext facesContext, Object[] objects)
throws EvaluationException, MethodNotFoundException {
- // TODO check Authorisation and don't invoke method binding if user is not authorised
- // TODO add FacesMessage if user is not authorised
if (LOG.isDebugEnabled()) {
LOG.debug("MethodBinding invoke " + getExpressionString());
}
- String expression = getExpressionString();
+ // TODO methodbinding with objects.length == 0 should only checked?
+ if (isAuthorized(facesContext)) {
+ return methodBinding.invoke(facesContext, objects);
+ } else {
+ // TODO better message
+ facesContext.addMessage(null, new FacesMessage("Not authorised"));
+ return null;
+ }
+ }
- // TODO find a better way
+ private boolean isAuthorized(FacesContext facesContext) {
- if (expression.startsWith("#{") && expression.endsWith("}")) {
- expression = expression.substring(2, expression.length()-1);
- int index = expression.lastIndexOf('.');
- if (index != -1) {
- String methodExpression = expression.substring(index+1, expression.length());
- String beanExpression = expression.substring(0, index);
- // TODO find a better way
- Object bean = facesContext.getApplication().getVariableResolver().resolveVariable(facesContext, beanExpression);
- if (bean != null) {
- // TODO cache the security constraints for the expression
- logRolesAllowed(bean.getClass().getAnnotation(RolesAllowed.class));
- logDenyAll(bean.getClass().getAnnotation(DenyAll.class));
- logPermitAll(bean.getClass().getAnnotation(PermitAll.class));
- try {
- Method method = bean.getClass().getMethod(methodExpression);
- logRolesAllowed(method.getAnnotation(RolesAllowed.class));
- logDenyAll(method.getAnnotation(DenyAll.class));
- logPermitAll(method.getAnnotation(PermitAll.class));
+ Annotation securityAnnotation = getSecurityAnnotation(facesContext);
+ if (securityAnnotation == null) {
+ return true;
+ }
- } catch (NoSuchMethodException e) {
- LOG.error("No Method " + methodExpression + " in class " + bean.getClass(), e);
- }
+ if (securityAnnotation instanceof DenyAll) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("DenyAll");
+ }
+ return false;
+ }
+ if (securityAnnotation instanceof RolesAllowed) {
+ String [] roles = ((RolesAllowed)securityAnnotation).value();
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("RolesAllowed " + Arrays.asList(((RolesAllowed)securityAnnotation).value()));
+ }
+ for (String role : roles) {
+ boolean authorised = facesContext.getExternalContext().isUserInRole(role);
+ if (authorised) {
+ return true;
}
-
}
+ return false;
}
- return methodBinding.invoke(facesContext, objects);
- }
- private void logRolesAllowed(RolesAllowed rolesAllowed) {
- if (rolesAllowed != null) {
- LOG.debug("RolesAllowed " + Arrays.asList(rolesAllowed.value()));
+ if (securityAnnotation instanceof PermitAll) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("PermitAll");
+ }
+ return true;
}
+ return true;
}
-
- private void logPermitAll(PermitAll permitAll) {
- if (permitAll != null) {
- LOG.debug("PermitAll");
+ private Annotation getSecurityAnnotation(FacesContext facesContext) {
+ String expression = getExpressionString();
+ if (authorisationCache.containsKey(expression)) {
+ return authorisationCache.get(expression);
+ } else {
+ Annotation securityAnnotation = null;
+ if (expression.startsWith("#{") && expression.endsWith("}")) {
+ expression = expression.substring(2, expression.length()-1);
+ int index = expression.lastIndexOf('.');
+ if (index != -1) {
+ String methodExpression = expression.substring(index+1, expression.length());
+ String beanExpression = expression.substring(0, index);
+ // TODO find a better way
+ Object bean = facesContext.getApplication().getVariableResolver().resolveVariable(facesContext, beanExpression);
+ if (bean != null) {
+ try {
+ Method method = bean.getClass().getMethod(methodExpression);
+ securityAnnotation = getSecurityAnnotations(method);
+ if (securityAnnotation == null) {
+ securityAnnotation = getSecurityAnnotations(bean.getClass());
+ }
+ } catch (NoSuchMethodException e) {
+ LOG.error("No Method " + methodExpression + " in class " + bean.getClass(), e);
+ }
+ }
+ }
+ }
+ authorisationCache.put(expression, securityAnnotation);
+ return securityAnnotation;
}
}
-
- private void logDenyAll(DenyAll denyAll) {
- if (denyAll != null) {
- LOG.debug("DenyAll");
+ private Annotation getSecurityAnnotations(AnnotatedElement annotatedElement) {
+ Annotation annotation = annotatedElement.getAnnotation(RolesAllowed.class);
+ if (annotation != null) {
+ return annotation;
+ }
+ annotation = annotatedElement.getAnnotation(DenyAll.class);
+ if (annotation != null) {
+ return annotation;
+ }
+ annotation = annotatedElement.getAnnotation(PermitAll.class);
+ if (annotation != null) {
+ return annotation;
}
+ return null;
}
public Object saveState(FacesContext facesContext) {