java - 自定义登录表单无法使用 Spring Security
问题描述
我正在学习 Spring MVC 并尝试在我的应用程序中实现 spring 安全性。我创建了自定义登录并尝试使用自定义登录表单登录。当我运行应用程序时,登录页面正常显示,但输入用户名和密码后它无法正常工作。提交登录表单正在调用控制器操作,也不会重定向到任何其他页面。我无法理解这里需要哪种配置。
安全配置.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";
}
登录.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>
所以我的登录页面正常,但输入用户名密码后不起作用。我不知道我的项目结构是否对错误负责,因为有时单击登录后它会重定向到资源。
解决方案
...信贷归于乍得·达比...
根本原因是因为 Maven 项目是由原型生成的。不幸的是,原型会生成遗留配置文件。因此,该 Web 项目基于旧版本的 Servlet 2.3。旧版本的 Servlet 2.3 规范不支持 Spring Security 5 所需的功能。这就是您的应用程序最初无法运行的原因。
有很多方法可以解决这个问题。我将使用下面一种更简单的技术。
- 删除 web.xml 文件
接下来我们需要手动编辑一个 Eclipse 配置文件。
右键单击项目,选择属性 > 资源。Location 字段的最右侧是一个按钮,用于打开文件系统上的项目目录。
退出 Eclipse(您需要退出,因为我们正在更新 Eclipse 项目配置)
在您的文件系统上,进入项目的 .settings 目录
使用文本编辑器编辑文件:org.eclipse.wst.common.project.facet.core.xml
将 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>
保存文件
- 启动 Eclipse
现在让我们刷新和清洁
右键单击您的项目并选择刷新
在 Eclipse 的服务器选项卡中,停止 Tomcat 服务器
扩展您的 Tomcat 服务器并删除所有现有应用程序
右键单击服务器并选择“清理...”
再次右键单击服务器并选择“清理 Tomcat 工作目录...”
推荐阅读
- c# - EF Core 迁移成功但未创建数据库的问题
- sql - 编写 1 个 SQL 查询,将 3 个独立的表与函数组合起来:group by 和 case when
- assembly - 在汇编中添加两个三字数字
- json - Apollo graphQL:在 iOS swift 中使用自定义标量时解码数组时出错
- css - 使用带有 auto-fit 和 minmax 的 repeat():1fr 有最大宽度吗?
- asp.net - 错误水晶报表隐藏所有空格字符
- python - 计算文件spyder python中的出现次数
- javascript - 如何更新/设置具有相同值 JavaScript/Typescript 的多个对象(具有相同参数)的值
- amazon-web-services - AWS SES - SMTP 与 SES 客户端
- android - 如何在 Flutter 中多次安排单个本地通知