为了账号安全,请及时绑定邮箱和手机立即绑定

Spring Security 自定义登录处理 URL 始终重定向到故障处理程序

Spring Security 自定义登录处理 URL 始终重定向到故障处理程序

哆啦的时光机 2022-06-15 15:37:58
我正在开发一个带有基于注释的配置的 spring 安全代码。但是,在从登录页面点击WebSecurityConfig类(扩展WebSecurityConfigurerAdapter )的配置方法中定义的登录处理 url 后,即使提供了正确的用户名和密码,它也总是重定向到httpSecurity 的.failureHandler()方法而不是.successHandler ( ) 方法。下面是WebSecurityConfig类的configure方法和configureGlobal方法。@Overrideprotected 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"));}@Autowiredpublic 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>  我错过了什么?我正在使用弹簧安全 5.1.2.release
查看完整描述

2 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

它一直在工作。实际上,我为此错过了密码编码器。虽然我给了一个重定向到成功页面。



查看完整回答
反对 回复 2022-06-15
?
慕容森

TA贡献1853条经验 获得超18个赞

登录页面和注销页面必须可供所有人访问。 在登录配置和注销配置之后,您错过了 .pemitAll() 方法。



查看完整回答
反对 回复 2022-06-15
  • 2 回答
  • 0 关注
  • 453 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号