Spring Security-POST方法不起作用

时间:2020-09-26 14:47:16

标签: spring post spring-security http-post

问候,亲爱的论坛成员。

请告诉我为什么POST方法不起作用?我无法捕获通过POST收到的参数。它不给出任何错误,仅返回 null 。我已经为这个问题苦苦挣扎了好几天了。

SecurityConfig.class

    package com.myfilter.security;

    import com.myfilter.filter.CustomFilter;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpMethod;
    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.web.authentication.AnonymousAuthenticationFilter;

    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {

        // I connect my CustomFilter where I will catch POST parameters
        @Autowired
        CustomFilter customFilter;

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

            http.

                    // I add a filter where I will catch POST parameters
                    addFilterBefore(customFilter, AnonymousAuthenticationFilter.class)

                    .authorizeRequests()
                    .mvcMatchers("/login", "/").permitAll()

                    // I indicate that the POST method will be used
                    .mvcMatchers(HttpMethod.POST,"/login").permitAll()


                    .and()
                    .csrf().disable()
                    .formLogin()
                    .loginPage("/login")
                    .loginProcessingUrl("/login")
                    .defaultSuccessUrl("/");

        }

    }

CustomFilter.class (这是一个过滤器,我将在其中捕获POST参数)

    package com.myfilter.filter;

    import org.springframework.stereotype.Component;
    import org.springframework.web.filter.GenericFilterBean;

    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import java.io.IOException;

    @Component
    public class CustomFilter extends GenericFilterBean {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

            HttpServletRequest httpServletRequest = (HttpServletRequest) request;

            // I get the parameter
            String email = httpServletRequest.getParameter("email");

            // Display to the console
            System.out.println(email);

            chain.doFilter(request, response);
        }
    }

login.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
     
        <meta charset="utf-8">
        <title>Sign in</title>
     
    </head>
    <body>
     
        <form name="user" method="post" action="/login">
     
                <input type="text" name="email">
                <input type="password" name="password">
            
            <button type="submit">Sign in</button>
            
        </form>
        
    </body>
    </html>

HomeController

    package com.myfilter.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;

    @Controller
    public class HomeController {

        @GetMapping(value = "/")
        public String home () {
                return "/home";
        }


        @GetMapping(value = "/login")
        public String login () {
            return "/login";
        }


        @PostMapping(value = "/login")
        public String loginPost () {
            return "redirect:/";
        }


    }

在github上查看此项目 https://github.com/romanych2021/MyFilter

0 个答案:

没有答案