控制器重定向导致错误403'Type = Forbidden'

时间:2019-04-02 04:38:24

标签: html spring error-handling thymeleaf

我正在为uni项目建立一个基于服务器的网站,并且在添加新用户时遇到问题,该问题将用户添加到数据库中,然后将该用户重定向到个人资料页。但是,当我提交表单时,它会触发403错误,从我所做的阅读看来,这表明它由于某种原因无法访问该页面。

我相当有信心这与我的安全配置(目录“ src / java / pokedex / config / securityConfig.java”)无关,尽管我会在以下情况下添加它:

package pokedex.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class securityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    }
}

文件中的其他链接似乎正常工作,我可以从导航栏访问个人资料页面,只是重定向导致了问题。

这是控制器的(我认为)相关代码(目录“ src / java / pokedex / controllers / RegistrationController.java”):

    public String processRegistration(User user) {
        registrationService.addUser(user);
        return "redirect:/profile";
    }

    @RequestMapping(value = "/profile")
    public ModelAndView showProfile() {

        return new ModelAndView("/profile", "user",
                registrationService.getCurrentUser() != null
                        ? registrationService.getCurrentUser()
                        : new User("John", "Doe", "johndoe@placeholder.co.uk", "password"));
    }

通过以下方式提供registrationService的实现(目录“ src / java / pokedex / services / RegistrationServiceImpl.java”)

package pokedex.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pokedex.dao.UserRepository;
import pokedex.entities.User;

@Service
public class RegistrationServiceImpl implements RegistrationService {

    private UserRepository userRepository;
    private User currentUser;

    @Autowired
    public RegistrationServiceImpl(UserRepository userRepository) {
        this.userRepository = userRepository;

        userRepository.save(new User("john", "doe", "john@doe.com", "password"));
    }

    @Override
    public List<User> getUsers() {
        return userRepository.findAll();
    }

    @Override
    public void addUser(User user) {
        currentUser = user;
        userRepository.save(user);
    }

    @Override
    public User getCurrentUser() {
        return currentUser;
    }

    @Override
    public int getNumberOfUsers() {
        return (int) userRepository.count();
    }
}

对于页面,配置文件页面目录为“ src / resources / templates / profile.html”,注册页面为“ src / resources / templates / register.html”。

可能导致配置文件出现问题的部分是:

<article>
        <section class="profile">
            <fieldset>
                <legend>User Information</legend>
                <table>
                    <tr>
                        <td><label>Name:</label></td>
                        <td th:text="${user.firstName} ?: 'John'">John</td>
                        <td th:text="${user.lastName} ?: 'Doe'">Doe</td>
                    </tr>
                </table>
                <table>
                    <tr>
                        <td><label>Email:</label></td>
                        <td th:text="${user.email} ?: 'johndoe@placeholder.com'">johndoe@placeholder.com</td>
                    </tr>
                </table>
            </fieldset>
        </section>
    </article>

寄存器中的注册部分为:

<article>
        <section class="registration">
            <form action="/register" method="post" th:object="${user}">
                <fieldset>
                    <legend>User Information</legend>
                    <table>
                        <tr>
                            <td><label>First name:</label></td>
                            <td><input type="text" th:field="*{firstName}"/></td>
                        </tr>
                        <tr>
                            <td><label>Last name:</label></td>
                            <td><input type="text" th:field="*{lastName}"/></td>
                        </tr>
                        <tr>
                            <td><label>Email:</label></td>
                            <td><input type="email" th:field="*{email}"/></td>
                        </tr>
                        <tr>
                            <td><label>Password:</label></td>
                            <td><input type="password" th:field="*{password}"/></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td><input type="submit" value="register"/></td>
                        </tr>
                    </table>
                </fieldset>
            </form>
        </section>
    </article>

这就是我自己一个人设法解决的问题,如果有人有任何建议或希望看到我乐意提供的更多代码,我认为问题可能出在这里。

我确实在上载此文件时注意到IDE(Intellij IDEA)表示它无法识别配置文件中的用户字段(?),但对于其他对象则不存在此问题,但到目前为止我可以说它们在功能上是相同的,因此可能会失败。

如果有人能指出正确的方向摆脱错误,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

好吧,看来这是添加“读5分钟到讲座”这本书的又一次事件,事实证明这是安全性,要添加一个(简单的)修复程序:

http.csrf().disable();
http.headers().frameOptions().disable();

到httpSecurity配置方法

相关问题