我正在使用Spring Security验证我的应用程序。我想通知客户其帐户已签名但尚未启用。这是WebSecurityConfigurerAdapter
中的用户配置:
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user1")
.password(passwordEncoder().encode("1"))
.authorities("ROLE_USER1")
.and()
.withUser("user2")
.password(passwordEncoder().encode("1"))
.authorities("ROLE_USER2")
.disabled(true)
;
}
我的授权配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/api/public").permitAll()
.antMatchers("/api/private1").hasAuthority("ROLE_USER1")
.antMatchers("/api/private2").hasAuthority("ROLE_USER2")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/loginNeeded").permitAll()
.loginProcessingUrl("/login")
.defaultSuccessUrl("/")
.failureForwardUrl("/login/failed")
.successForwardUrl("/login/succeed")
.and()
.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/").deleteCookies("JSESSIONID", "account")
;
}
user2
被禁用。我知道Spring Security中有一个DisabledException
,但是当我尝试使用禁用的user2
登录时却无法获得此异常。似乎Spring像其他登录失败的尝试一样认为禁用的用户。我正在使用
postman
作为客户端。谁能解决这个问题?
提前致谢
最佳答案
在这种情况下,您应尝试将AuthenticationFailureHandler(例如TokenAuthenticationFailureHandler等)用于Spring安全。
对于每种情况,您都可以使用此处理程序来修改错误消息,在这种情况下您可以区分发生的错误的类型。