Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to evaluate expression with spring security

I have a Spring rest service, and I'm trying to add security to it. I followed this tutorial, but when I try to access the service directly I get the following error:

There was an unexpected error (type=Internal Server Error, status=500). Failed to evaluate expression 'ROLE_USER'

Here's my security configuration:

webSecurityConfig.xml

<http entry-point-ref="restAuthenticationEntryPoint">
      <intercept-url pattern="/**" access="ROLE_USER"/>

      <form-login
         authentication-success-handler-ref="mySuccessHandler"
         authentication-failure-handler-ref="myFailureHandler"
      />

      <logout />
   </http>

   <beans:bean id="mySuccessHandler"
      class="com.eficid.cloud.security.rest.AuthenticationSuccessHandler"/>
   <beans:bean id="myFailureHandler" class=
     "org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"/>


      <authentication-manager>
        <authentication-provider>
          <user-service>
            <user name="temp" password="temp" authorities="ROLE_USER" />
          </user-service>
        </authentication-provider>
      </authentication-manager> 

SpringSecurityConfig:

public class SpringSecurityConfig {

    public SpringSecurityConfig() {
        super();
    }

}

I'm also getting this error when trying to use curl to log in:

{
"timestamp":1460399841286,
"status":403,"error":"Forbidden",
"message":"Could not verify the provided CSRF token because your session was not found.",
"path":"/spring-security-rest/login"
}

Do I need to add the csrf token manually to the command? The service has a self-signed certificate, if that makes any difference.

like image 319
napstablook Avatar asked Dec 25 '22 07:12

napstablook


1 Answers

If you don't need CRF to be enabled, then you can disable it in webSecurityConfig.xml file like below:

        <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/login.html" access="hasRole('ANONYMOUS')" />
        <intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
        <!-- This form is a default form that used to login  
            <http-basic/>
         -->
         <form-login login-page="/login.html"/>
         <csrf disabled="true"/>
    </http>

If CSRF is enabled, you have to include a _csrf.token in the page you want to login or logout.The below code needs to be added to the form:

<input type="hidden" name="${_csrf.parameterName}"
            value="${_csrf.token}" />
like image 196
Saif Masadeh Avatar answered Dec 28 '22 05:12

Saif Masadeh