我正在开发一个带有基于注释的配置的 spring 安全代码。
但是,在从登录页面点击WebSecurityConfig类(扩展WebSecurityConfigurerAdapter )的配置方法中定义的登录处理 url 后,即使提供了正确的用户名和密码,它也总是重定向到httpSecurity 的.failureHandler()方法而不是.successHandler ( ) 方法。
下面是WebSecurityConfig类的configure方法和configureGlobal方法。
@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>
我错过了什么?
我正在使用弹簧安全 5.1.2.release
繁星点点滴滴
慕容森
相关分类