如何在 Spring 安全性中禁用用户时向客户端显示用户禁用消息



我正在使用 spring 安全性来验证我的应用程序。我想通知客户其帐户已签名但尚未启用。这是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被禁用。我知道春季安全性存在DisabledException,但是当我尝试使用禁用user2登录时,我无法获得此异常。似乎 Spring 像任何其他登录失败的尝试一样考虑禁用的用户。

我使用postman作为客户端。

任何人都可以帮助解决这个问题吗?

提前致谢

在这种情况下,您应该尝试使用AuthenticationFailureHandler(例如TokenAuthenticationFailureHandler等(来实现Spring安全性。 此处理程序可用于修改区分所发生错误类型的每种情况的错误消息。

最新更新