FuSsA
FuSsA

Reputation: 4297

Request method 'POST' not supported - SPRING

login.jsp

<div id="login" class="animate form">
                    <form action="${loginUrl}" method="POST">
                        <h1>Log in</h1>

                        <c:url var="loginUrl" value="/login" />
                        <c:if test="${param.error != null}">
                            <input type="text" class="alert-danger" id="danger" name="danger"
                                placeholder="Invalid username and password." disabled />
                            <br />
                        </c:if>
                        <c:if test="${param.logout != null}">
                            <input type="text" class="alert-success" id="success"
                                name="success"
                                placeholder="You have been logged out successfully." disabled />
                            <br />
                        </c:if>


                        <p>
                            <label for="username" class="uname" data-icon="u"> Your
                                email </label> <input id="username" name="login" required="required"
                                type="text" placeholder="[email protected]" />
                        </p>
                        <p>
                            <label for="password" class="youpasswd" data-icon="p">
                                Your password </label> <input id="password" name="password"
                                required="required" type="password" placeholder="eg. X8df!90EO" />
                        </p>
                        <p class="keeplogin">
                            <input type="checkbox" name="remember-me" id="rememberme"
                                value="rememberme" /> <label for="rememberme">Remember
                                Me</label>
                        </p>
                        <p class="login button">
                            <input type="submit" value="Login" />
                        </p>
                        <p class="change_link"></p>
                    </form>
                </div>

userlist.jsp

<div class="generic-container">
        <%@include file="authheader.jsp" %>   
        <div class="panel panel-default">
              <!-- Default panel contents -->
            <div class="panel-heading"><span class="lead">List of Users </span></div>
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Prenom</th>
                        <th>Nom</th>
                        <th>Matricule</th>
                        <th>Login</th>
                        <sec:authorize access="hasRole('ADMIN') or hasRole('READ')">
                            <th width="100"></th>
                        </sec:authorize>
                        <sec:authorize access="hasRole('ADMIN')">
                            <th width="100"></th>
                        </sec:authorize>

                    </tr>
                </thead>
                <tbody>
                <c:forEach items="${users}" var="user">
                    <tr>
                        <td>${user.prenom}</td>
                        <td>${user.nom}</td>
                        <td>${user.matricule}</td>
                        <td>${user.login}</td>
                        <sec:authorize access="hasRole('ADMIN') or hasRole('READ')">
                            <td><a href="<c:url value='/edit-user-${user.login}' />" class="btn btn-success custom-width">edit</a></td>
                        </sec:authorize>
                        <sec:authorize access="hasRole('ADMIN')">
                            <td><a href="<c:url value='/delete-user-${user.login}' />" class="btn btn-danger custom-width">delete</a></td>
                        </sec:authorize>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
        <sec:authorize access="hasRole('ADMIN')">
            <div class="well">
                <a href="<c:url value='/newuser' />">Add New User</a>
            </div>
        </sec:authorize>
    </div>

AppController.java

@Controller
@RequestMapping("/")
@SessionAttributes("roles")
public class AppController {

    @Autowired
    IService_User<USER> userService;

    @Autowired
    IService<COMPTE> compteService;

    @Autowired
    MessageSource messageSource;

    @Autowired
    PersistentTokenBasedRememberMeServices persistentTokenBasedRememberMeServices;

    @Autowired
    AuthenticationTrustResolver authenticationTrustResolver;

@RequestMapping(value = { "/", "/list" }, method = { RequestMethod.GET, RequestMethod.POST })
    public String listUsers(ModelMap model) {

        List<USER> users = userService.findAllOBJECTS();
        model.addAttribute("users", users);
        model.addAttribute("loggedinuser", getPrincipal());
        return "userslist";
    }

@RequestMapping(value = {"/login"}, method = { RequestMethod.GET, RequestMethod.POST })
    public String loginPage() {
        if (isCurrentAuthenticationAnonymous()) {
            return "login";
        } else {
            return "redirect:/list";
        }
    }
}

There are 2 pages: login.jsp - start page which includes form to be populated with login and password - userlist.jsp list of results "display all users persisted in DB".. First the login page is shown, when i click on submit button i got this error: org.springframework.web.servlet.PageNotFound - Request method 'POST' not supported

Upvotes: 0

Views: 1625

Answers (4)

Chathuranga Tennakoon
Chathuranga Tennakoon

Reputation: 2179

if you are using spring security 4.x.x. , CSRF is enabled by default. therefore you have to provide the csrf filed in your form.

Adding the csrf token as hidden fields does the trick:

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

Upvotes: 0

meetarun
meetarun

Reputation: 569

In your login form you are explicitly making an POST request... and in your controller the the url is mapped to GET request.. this is the issue... Please make the controller as POST... like

@RequestMapping(value = {"/login"}, method = RequestMethod.POST)
    public String loginPage() {
        if (isCurrentAuthenticationAnonymous()) {
            return "login";
        } else {
            return "redirect:/list";
        }
    }

Upvotes: 1

Prakash Hari Sharma
Prakash Hari Sharma

Reputation: 1424

In your login.jsp you are using http method POST

<form action="${loginUrl}" method="POST">

and in controller, you are using http method GET

@RequestMapping(value = {"/login"}, method = RequestMethod.GET)
    public String loginPage() {
        if (isCurrentAuthenticationAnonymous()) {
            return "login";
        } else {
            return "redirect:/list";
        }
    }

Problem will be solved after changing method = RequestMethod.POST in your controller like this

@RequestMapping(value = {"/login"}, method = RequestMethod.POST)
    public String loginPage() {
        if (isCurrentAuthenticationAnonymous()) {
            return "login";
        } else {
            return "redirect:/list";
        }
    }

Upvotes: 1

Blank
Blank

Reputation: 12378

Add post method in @RequestMapping annotation, like following;)

@RequestMapping(value = {"/login"}, method = {RequestMethod.GET, RequestMethod.POST})
public String loginPage() {
    if (isCurrentAuthenticationAnonymous()) {
        return "login";
    } else {
        return "redirect:/list";
    }
}

Upvotes: 0

Related Questions