Home  >  Forum  >  Spring
Post New Query

java.lang.IllegalArgumentException: Failed to evaluate expression 'USER'


joined on
May 13,2015
Asked on May 13,2015
Hi all,

I am creating Spring JUnit testing. My service method is

    @PreAuthorize("USER")
    public void someMethod();


and my test method is

    @Test
    @WithMockUser(roles={"USER"})
    public void testMethod() {
        service.someMethod();
    }


I am getting run time exception as follows

java.lang.IllegalArgumentException: Failed to evaluate expression 'USER'
    at org.springframework.security.access.expression.ExpressionUtils.evaluateAsBoolean(ExpressionUtils.java:14)
    at org.springframework.security.access.expression.method.ExpressionBasedPreInvocationAdvice.before(ExpressionBasedPreInvocationAdvice.java:44)
    at org.springframework.security.access.prepost.PreInvocationAuthorizationAdviceVoter.vote(PreInvocationAuthorizationAdviceVoter.java:57)
    at org.springframework.security.access.prepost.PreInvocationAuthorizationAdviceVoter.vote(PreInvocationAuthorizationAdviceVoter.java:25)
    at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:62)
    at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:232)
    at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:64)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy33.addUser(Unknown Source)
joined on
November 18,2013
Replied on May 13,2015

Do not use expression with @PreAuthorize. Change your code using hasRole()

@PreAuthorize("hasRole('USER')")
public void someMethod();

Issue will be resolved.
joined on
May 13,2015
Replied on May 13,2015

Working Thanks.

Write Answer








Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us