Java Spring Security自定义登录处理URL始终重定向到failurehandler
我正在使用基于注释的配置开发一个具有spring安全性的代码 但是在点击WebSecurityConfig类(扩展了WebSecurityConfigureAdapter)的配置方法中定义的登录处理url后,它总是重定向到。failureHandler()httpSecurity的方法,而不是。successHandler(),即使提供了正确的用户名和密码 下面是WebSecurityConfig类的configure方法和configureGlobal方法Java Spring Security自定义登录处理URL始终重定向到failurehandler,java,spring,spring-security,Java,Spring,Spring Security,我正在使用基于注释的配置开发一个具有spring安全性的代码 但是在点击WebSecurityConfig类(扩展了WebSecurityConfigureAdapter)的配置方法中定义的登录处理url后,它总是重定向到。failureHandler()httpSecurity的方法,而不是。successHandler(),即使提供了正确的用户名和密码 下面是WebSecurityConfig类的configure方法和configureGlobal方法 @Override protecte
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeRequests()
.antMatchers("/login.success").access("hasRole('ROLE_USER')")
.and()
.formLogin()
.loginPage("/login.home")
.usernameParameter("username")
.passwordParameter("password")
.loginProcessingUrl("/login.do")
.successHandler(applicationLoginSuccessHandler)
.failureHandler(applicationLoginFailureHandler)
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder.inMemoryAuthentication().withUser("user").password("pass").roles("USER");
}
下面是login.jsp的片段
<c:url value="login.do" var="loginUrl"/>
<form action="${loginUrl}" method="POST">
<c:if test="${param.error != null}">
<p>
Invalid username and password.
</p>
</c:if>
<c:if test="${param.logout != null}">
<p>
You have been logged out.
</p>
</c:if>
<p>
<label for="username">Username</label>
<input type="text" id="username" name="username"/>
</p>
<p>
<label for="password">Password</label>
<input type="password" id="password" name="password"/>
</p>
<input type="hidden"
name="${_csrf.parameterName}"
value="${_csrf.token}"/>
<button type="submit" class="btn">Log in</button>
</form>
无效的用户名和密码。
您已注销。
用户名
密码
登录
我错过了什么
我使用的是spring security 5.1.2。发布版登录页面和注销页面必须对每个人都可访问。在登录配置和注销配置之后,您错过了.pemitAll()方法。每个人都必须可以访问登录页面和注销页面。在登录配置和注销配置之后,您错过了.pemitAll()方法。它一直在工作。实际上我错过了一个密码编码器。而我给了一个它的重定向到成功页面。它一直在工作。实际上我错过了一个密码编码器。当我给一个重定向到成功页面时。我尝试了.loginPage(“/login.home”).permitAll().usernameParameter(“用户名”).passwordParameter(“密码”),但没有成功。我认为这是因为用户凭据。选中此项,将configureGlobal(…)方法中的.password(…)方法更改为.password(“{noop}pass”)。
@Override
验证成功后的公共void(HttpServletRequest HttpServletRequest,HttpServletResponse HttpServletResponse,验证验证)
抛出IOException,ServletException{
httpServletResponse.setStatus(httpServletResponse.SC_OK);
httpServletResponse.sendRedirect(./login.success”);
HttpSession-HttpSession=httpServletRequest.getSession();
这是successHandler的onAuthenticationSuccess方法。是否与发送重定向方法有关???我尝试了.loginPage(“/login.home”).permitAll().usernameParameter(“用户名”).passwordParameter(“密码”),但运气不佳。我认为这是因为用户凭据。检查此项,更改.password(…)方法在configureGlobal(…)方法中使用.password({noop}pass”)。@Override
authenticationsuccess(HttpServletRequest HttpServletRequest,HttpServletResponse HttpServletResponse,Authentication Authentication)时公共无效(HttpServletResponse HttpServletResponse,Authentication Authentication Authentication)
抛出IOException,ServletException{
(HttpServletResponse.SC_OK);
HttpServletResponse.sendRedirect(“/login.success”);
HttpSession HttpSession=httpServletRequest.getSession();
}
这是成功处理程序的onAuthenticationSuccess方法。是否与发送重定向方法有关???