Spring:添加注册后,表单上的错误按钮操作(Spring Security)

时间:2019-07-01 10:34:14

标签: spring spring-mvc spring-security

我以登录页面-Spring Security为例。

简短地:

  1. 有一个根页面(“ localhost:8080 /”)-这是主页上的链接。
  2. 单击主页上的链接转到main.html(localhost:8080 / main /
  3. 如果用户未授权,则将其重定向到登录页面
  4. 当用户授权主页打开时
  5. 主页显示消息并按标签过滤
  6. 我在输入中输入标签,然后按搜索按钮(Найти),邮件按标签过滤

添加授权过滤器后,我将停止工作。

这是我的源代码:

根页面-主页上具有链接

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Gretting start: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <div>Hello, user</div>
    <a th:href="@{/main}">Main page</a>
</body>
</html>

主页

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <div>
        <form method="post">
            <input type="text" name="text" placeholder="Введите сообщение" />
            <input type="text" name="tag" placeholder="Тэг">
            <button type="submit">Добавить</button>     
        </form>
    </div>
    <div>Список сообщений</div>
    <form method="post" action="filter">
        <input type="text" name="filter">
        <button type="submit">Найти</button>
    </form>
    <div th:each = "message : ${messages}">
        <b th:text = "${message.id}"></b>
        <span th:text = "${message.text}"></span>
        <i th:text = "${message.tag}"></i>
    </div>
</body>
</html>

控制器处理所有映射

package com.example.sweater;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.example.sweater.domain.Message;
import com.example.sweater.repos.MessageRepo;

@Controller
public class GreetingController {

    @Autowired
    private MessageRepo messageRepo;

    @GetMapping("/")
    public String greeting(Model model) {

        return "greeting";

    }

    @GetMapping("/main")
    public String main(Model model) {

        Iterable<Message> messages = messageRepo.findAll();

        model.addAttribute("messages", messages);

        return "main";
    }

    @PostMapping("/main")
    public String add(@RequestParam String text, @RequestParam String tag, Model model) {

        Message message = new Message(text, tag);

        messageRepo.save(message);

        Iterable<Message> messages = messageRepo.findAll();

        model.addAttribute("messages", messages);

        return "main";
    }

    @PostMapping("/filter")
    public String filter(@RequestParam String filter, Model model) {

        Iterable<Message> messages;

        if (filter != null && !filter.isEmpty()) {
            messages = messageRepo.findByTag(filter);
        } else {
            messages = messageRepo.findAll();
        }

        model.addAttribute("messages", messages);

        return "main";

    }
}

WebSecurityConfig具有一个“内存中用户”。 antMathcers(“ /”)permitAll和anyRequest已验证

package com.example.sweater.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/").permitAll()
                .anyRequest().authenticated()
            .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
            .and()
                .logout()
                .permitAll();
    }

    @Bean
    @Override
    public UserDetailsService userDetailsService() {
        UserDetails user =
             User.withDefaultPasswordEncoder()
                .username("u")
                .password("p")
                .roles("USER")
                .build();

        return new InMemoryUserDetailsManager(user);
    }
}

我的屏幕截图 根页面

enter image description here

主页

enter image description here

输入过滤器标签并按“Найти”按钮(=搜索)时,出现错误:

enter image description here

@PostMapping("/filter")未捕获表单中的操作。我检查了调试器。我找不到错误,也不知道为什么会发生。

我有GitHub存储库:https://github.com/aalopatin/sweater

提交评论“添加消息”-过滤工作

输入注释“添加远程存储库并登录”-过滤器不起作用并添加登录

1 个答案:

答案 0 :(得分:0)

我找到了解决方法。在主页上的表单中需要添加'th'属性,因为我使用Thymeleaf这样的模板引擎。如果您使用Thymeleaf,则_csrf捍卫以表格形式自动插入令牌是必需的:

<form method="post" th:action="filter">
   <input type="text" name="filter">
   <button type="submit">Найти</button>
</form>
相关问题