我正在尝试使用HTML表单将数据提交到我的Spring / Thymeleaf服务器。当我使用POST方法提交时,第二个请求将使用相同的URL发送,但带有附加的“?error”查询参数和GET方法。此后续请求导致错误,因为它不携带POST请求主体中存在的信息,并且我必须以某种方式处理此第二个请求。在我的检查器窗口中,它说第二个请求是由第一个请求以某种方式发起的。
将方法更改为GET后,请求可以正常工作。但是,此解决方案对我不起作用,因为我正在使用表单提交密码数据。
这是引起问题的形式(类等被省略):
<form action="/login" method="POST">
<div>
<div>
<span><i></i></span>
</div>
<input name="username" type="text" placeholder="username">
</div>
<div>
<div>
<span><i></i></span>
</div>
<input name="password" type="password" placeholder="password">
</div>
<input type="hidden" th:value="${_csrf.token}" th:name="${_csrf.parameterName}">
<div>
<input type="submit" value="Login" name="button">
</div>
</form>
这是处理对/ login的所有请求的控制器(此操作尚未完成,但是有可能我在这里缺少了一些必要的东西):
@Controller()
public class LoginController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String doGet() {
System.out.println("Login page accessed");
return "login";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String doPost() {
System.out.println("Login attempted");
return "redirect:home";
}
}
提交表单时的预期输出是“尝试登录”,但唯一的结果是“已访问登录页面”。
这是安全配置方法,为该终结点明确定义了一些额外的功能
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/about", "/login", "/css/**", "/js/**", "/assets/**" ).permitAll()
.anyRequest().permitAll()
.and()
.csrf()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/home")
.usernameParameter("username")
.passwordParameter("password")
.and()
.logout()
.logoutUrl("/logout")
.addLogoutHandler( new CookieClearingLogoutHandler() );
}
第一个请求似乎甚至没有到达控制器。如何避免发送第二个请求?
参考方案
每当发生登录失败时,Spring Security都会启动额外的/login?error
调用。这是默认表单登录authenticationFailureUrl
。
Spring Security有一个默认的/login
POST处理程序,您的代码可能与之冲突(即您的表单发布数据将转到Spring Security的处理程序而不是您的处理程序,然后在此处的身份验证失败并导致重定向到错误)。
您可能要签出DaoAuthenticationProvider
和CustomAuthenticationProvider
。
请原谅我;我对Struts有点陌生。我遇到一个问题,即页面加载而不是我实际提交表单时发生了验证。我整天都在论坛上搜寻和搜寻,没有任何运气。我显然做错了一些事情,应该很容易确定,但是我还没有发现问题所在。这是我的struts.xml的片段:<action name="*Test" method="{1}" clas…
DataSourceTransactionManager和JndiObjectFactoryBean和JdbcTemplate的用途是什么? - java以下的用途是什么:org.springframework.jdbc.core.JdbcTemplate org.springframework.jdbc.datasource.DataSourceTransactionManager org.springframework.jndi.JndiObjectFactoryBean <tx:annotatio…
在特定的Swing组件上设置自定义外观 - java是否可以在Swing GUI上仅针对特定组件或组件集设置用于Synth外观的文件,而无需为其他任何组件进行更改? java参考方案 是的,有可能。将类型指定为“区域”,将其更改为“名称”,将值更改为由setName(“ componentName”)给定的特定名称;应用程序中的方法。添加这个<style id="titleBarButtons…
SOAPFaultException部署在Tomcat上时,但在GlassFish中工作正常 - java朋友们,我一直在尝试很多,阅读了很多论坛,但无法理解为什么出现此问题。我使用契约优先方法创建了一个Jax-WS WebService。创建WSDL和XSD,然后使用wsimport工具生成其余工件,为SEI提供实现。将WebService应用程序部署到Eclipse Helios中的GlassFish(Glassfish适配器和Eclipse中安装的插件)。…
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…