自定义登录表单无法使用Spring Security

时间:2018-06-16 19:43:46

标签: java spring spring-mvc spring-security

我正在学习Spring MVC并试图在我的应用程序中实现spring security。我创建了自定义登录并尝试使用自定义登录表单登录。当我正在运行应用程序登录页面正确显示但输入用户名和密码后它无法正常工作。提交登录表单是调用控制器操作,它既不会重定向到任何其他页面。我无法理解这里需要哪种配置。

  

SecurityConfig.Java

@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();
    }
}
  

SecurityWebAppInitializer.java

public class SecurityWebAppInitializer extends 
AbstractSecurityWebApplicationInitializer {

}
  

AuthController.java

@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";
 }
  

的login.jsp

<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>

所以我的登录页面正常运行但输入用户名密码后无效。我不知道我的项目结构是否对错误负责,因为有时在点击登录后它会重定向到资源。

2 个答案:

答案 0 :(得分:2)

...信用证到乍得达比...

根本原因是因为Maven项目是由原型生成的。不幸的是,原型会生成旧的配置文件。结果,该Web项目基于Servlet 2.3的旧版本。 Servlet 2.3规范的旧版本不支持Spring Security 5所需的功能。这就是您的应用最初无法运行的原因。

有多种解决方法。我将在下面使用一种较简单的技术。

  1. 删除web.xml文件

下一步,我们将需要手动编辑Eclipse配置文件。

  1. 右键单击项目,选择“属性”>“资源”。 “位置”字段最右边是一个按钮,它将打开文件系统上的项目目录。

  2. 退出Eclipse(您将需要退出,因为我们正在更新Eclipse项目配置)

  3. 在文件系统上,进入项目的.settings目录

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

  5. 将jst.web版本更改为4.0。例如,您的文件应该看起来像

<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>

保存文件

  1. 启动Eclipse

现在让我们刷新和清洁

  1. 右键单击您的项目,然后选择“刷新”

  2. 在Eclipse的“服务器”标签中,停止Tomcat服务器

  3. 扩展您的Tomcat服务器并删除所有现有的应用程序

  4. 右键单击服务器,然后选择“清理...”

  5. 再次右键单击服务器,然后选择“清理Tomcat工作目录...”

答案 1 :(得分:1)

您不必发送对象或验证密码,因为Spring负责为您处理或重定向。如果您希望在成功通过身份验证后重定向它们,则可以在配置中添加.defaultSuccessUrl(url),并自动将其重定向到该页面。此外,该方法必须是POST,并且操作必须是login - 这两个要求是必须的。以下是Baeldung http://www.baeldung.com/spring-security-login的教程,您也可以按照https://github.com/AritonV10/spring-security-demo

进行操作
相关问题