Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Spring安全性时,自定义登录表单不起作用_Java_Spring_Spring Mvc_Spring Security - Fatal编程技术网

Java 使用Spring安全性时,自定义登录表单不起作用

Java 使用Spring安全性时,自定义登录表单不起作用,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我正在学习SpringMVC,并试图在我的应用程序中实现SpringSecurity。我已经创建了自定义登录,并尝试使用自定义登录表单登录。当我运行应用程序时,登录页面正确显示,但在输入用户名和密码后,该页面不起作用。提交登录表单是调用控制器操作,也不是重定向到任何其他页面。我无法理解此处需要哪种配置 SecurityConfig.Java SecurityWebAppInitializer.java AuthController.java login.jsp 记得我吗 因此,我的登录页面是

我正在学习SpringMVC,并试图在我的应用程序中实现SpringSecurity。我已经创建了自定义登录,并尝试使用自定义登录表单登录。当我运行应用程序时,登录页面正确显示,但在输入用户名和密码后,该页面不起作用。提交登录表单是调用控制器操作,也不是重定向到任何其他页面。我无法理解此处需要哪种配置

SecurityConfig.Java

SecurityWebAppInitializer.java

AuthController.java

login.jsp


记得我吗

因此,我的登录页面是正确的,但输入用户名后,密码不起作用。我不知道我的项目结构是否要为错误负责,因为有时单击“登录”后,它会重定向到资源。

您不必发送对象或验证密码,因为Spring会为您处理这些,也不会重定向。如果您希望在它们成功通过身份验证后重定向它们,那么您可以在配置中添加
.defaultSuccessUrl(url)
,它会自动将它们重定向到该页面。此外,方法必须是
POST
,操作必须是
login
——这两个要求是必须的。这是贝尔东的一篇教程,你也可以按照这篇教程来学习。

…这要归功于查德·达比

根本原因是Maven项目是由原型生成的。不幸的是,原型会生成遗留的配置文件。因此,web项目基于Servlet2.3的旧版本。Servlet2.3规范的旧版本不支持SpringSecurity5所需的特性。这就是你的应用程序最初无法运行的原因

有很多方法可以解决这个问题。我将使用下面一种更简单的技术

  • 删除web.xml文件
  • 接下来,我们需要手动编辑Eclipse配置文件

  • 右键单击项目,选择属性>资源。在“位置”字段的最右边是一个按钮,它将打开文件系统上的项目目录。
  • 退出Eclipse(您需要退出,因为我们正在更新Eclipse项目配置)

  • 在文件系统上,移动到项目的.settings目录

  • 使用文本编辑器编辑文件:org.eclipse.wst.common.project.facet.core.xml

  • 将jst.web版本更改为4.0。例如,您的文件应该如下所示

  • 
    
    保存文件

  • 启动Eclipse
  • 现在让我们刷新和清理

  • 右键单击项目并选择“刷新”

  • 在Eclipse中的服务器选项卡中,停止Tomcat服务器

  • 展开Tomcat服务器并删除所有现有应用程序

  • 右键单击服务器并选择“清除…”

  • 再次右键单击服务器并选择“清理Tomcat工作目录…”

  • @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        public void configure(AuthenticationManagerBuilder auth) throws Exception 
        {
            auth.inMemoryAuthentication()
            .withUser("test").password("test").roles("admin");
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
            http.authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/loginPage")
            .permitAll();
        }
    }
    
    public class SecurityWebAppInitializer extends 
    AbstractSecurityWebApplicationInitializer {
    
    }
    
    @RequestMapping(value = { "/loginPage" }, method = RequestMethod.GET)
    public String loginPage(ModelMap model) {
        model.addAttribute("user",new User());
        return "Login";
    }
    
    @RequestMapping(value = { "/success/login" }, method = RequestMethod.POST)
     public String login(@ModelAttribute("user") @Validated User user, BindingResult result, Model model, HttpServletRequest request) {
        System.out.println("Login is called.");
    
        return "UserDashboard";
     }
    
    <form:form method="post" action="./success/login" id="login" 
    modelAttribute="user" role="form" style="display: block;">
        <div class="form-group">
            <form:input name= "username" path="username" id="username" type="text" placeholder="Username" tabindex="1" class="form-control"/>
            <form:errors path="username" cssStyle="color: red;"/>
        </div>
        <div class="form-group">
            <form:input name="password" path="password" id="password" type="password" placeholder="Password" tabindex="2" class="form-control"/>
            <form:errors path="password" cssStyle="color: red;"/>
        </div>
        <div class="form-group text-center">
            <input type="checkbox" tabindex="3" class="" name="remember" id="remember">
            <label for="remember"> Remember Me</label>
        </div>
        <div class="form-group">
            <div class="row">
                <div class="col-sm-6 col-sm-offset-3">
                    <input type="submit" name="login-submit" id="submit" tabindex="4" class="form-control btn btn-login" value="Log In">
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="row">
                <div class="col-lg-12">
                    <div class="text-center">
                        <a href="https://phpoll.com/recover" tabindex="5" class="forgot-password">Forgot Password?</a>
                    </div>
                </div>
            </div>
        </div>
    </form:form>
    
    <faceted-project>
    <fixed facet="wst.jsdt.web"/>
    <installed facet="wst.jsdt.web" version="1.0”/>
    <installed facet="jst.web" version="4.0"/>
    <installed facet="java" version="1.8”/>
    </faceted-project>