Lucas Freitas
Lucas Freitas

Reputation: 1038

Spring Security Access denied 403 after post

I've tried almost everything in the others posts about it, nothing is related with my problem.

If I try to recover my URL via GET (ex: path/users/edit/1 ) everything works fine and I get redirected to the user edit page, but If I try to access this page via POST, the spring security deny my access to the page.

Both of the methods are mapped in my controller class.

@RequestMapping(value="/users/edit/{id}", method={RequestMethod.POST,RequestMethod.GET})
public ModelAndView login(ModelAndView model, @PathVariable("id") int id ) {
    model.addObject("user", this.userService.getUserById(id));
    model.setViewName("/users/add"); //add.jsp
    return model;
}

My form which I use post

<f:form method="post" action="/users/edit/${user.id}">
     <button type="submit">Edit</button>
</f:form>

Spring security.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">

<!-- enable use-expressions -->
<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/secure**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')" />
    <intercept-url pattern="/secure/users**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')" />

    <!-- access denied page -->
    <access-denied-handler error-page="/denied" />
    <form-login 
        login-page="/home" 
        default-target-url="/secure" 
        authentication-failure-url="/home?error" 
        username-parameter="inputEmail"
        password-parameter="inputPassword" />
    <logout logout-success-url="/home?logout"  />
    <!-- enable csrf protection -->
    <csrf/>
</http>

<!-- Select users and user_roles from database -->
<authentication-manager>
    <authentication-provider>
        <password-encoder hash="md5" /> 
        <jdbc-user-service data-source-ref="dataSource"
            users-by-username-query=
            "SELECT login, senha, ativo
               FROM usuarios 
              WHERE login = ?"
            authorities-by-username-query=
            "SELECT u.login, r.role
               FROM usuarios_roles r, usuarios u
              WHERE u.id = r.usuario_id
                AND u.login = ?" />
    </authentication-provider>
</authentication-manager>

Upvotes: 18

Views: 23022

Answers (3)

Grant Murphy
Grant Murphy

Reputation: 35

I know this is an old post but I want to post in case anyone comes across this while searching like I did.

Make sure you have the proper annotations on the class that extends WebSecurityConfigurerAdapter

@Configuration

@EnableWebSecurity

I had missed these and spent a few hours working on a problem that did not exist.

Upvotes: 0

Ahmad Al-Kurdi
Ahmad Al-Kurdi

Reputation: 2305

You can simply use <sec:csrfInput/> tag as the following :

    <f:form method="post" action="/users/edit/${user.id}">
        <button type="submit">Edit</button>
        <sec:csrfInput/>
    </f:form>

And please make sure to import spring security tags

<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec" %>

Upvotes: 0

ikumen
ikumen

Reputation: 11643

I noticed you're using csrf protection, which by default protects any HTTP verb that modifies a resource (e.g. PUT, POST, DELETE,...). If you're using Spring's form tag, a csrf token should be automatically included as a hidden input in your form. You should check the source in your browser to verify the csrf token is there, otherwise you'll need something like this:

<input type="hidden"
    name="${_csrf.parameterName}"
    value="${_csrf.token}"/> 

You can read more about csrf protection/configuration in Spring reference.

Upvotes: 36

Related Questions