Example usage for org.springframework.security.access.expression SecurityExpressionHandler createEvaluationContext

List of usage examples for org.springframework.security.access.expression SecurityExpressionHandler createEvaluationContext

Introduction

In this page you can find the example usage for org.springframework.security.access.expression SecurityExpressionHandler createEvaluationContext.

Prototype

EvaluationContext createEvaluationContext(Authentication authentication, T invocation);

Source Link

Document

Provides an evaluation context in which to evaluate security expressions for the invocation type.

Usage

From source file:br.com.suricattus.surispring.spring.security.util.SecurityUtil.java

/**
 * Method that checks if the user has the given access expression.
 * //from ww  w. j  ava2s  .c  o  m
 * @see Spring Security Expression-Based Access Control 
 * @param access
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static boolean isAuthorized(String access) {
    Map<String, SecurityExpressionHandler> expressionHandlres = ApplicationContextUtil.getContext()
            .getBeansOfType(SecurityExpressionHandler.class);
    SecurityExpressionHandler handler = (SecurityExpressionHandler) expressionHandlres.values().toArray()[0];
    Expression accessExpression = handler.getExpressionParser().parseExpression(access);

    FilterInvocation f = new FilterInvocation(FacesUtils.getRequest(), FacesUtils.getResponse(),
            new FilterChain() {
                public void doFilter(ServletRequest request, ServletResponse response)
                        throws IOException, ServletException {
                    throw new UnsupportedOperationException();
                }
            });

    return ExpressionUtils.evaluateAsBoolean(accessExpression,
            handler.createEvaluationContext(SecurityContextHolder.getContext().getAuthentication(), f));
}