【发布时间】:2018-04-20 03:11:52
【问题描述】:
我已经对此进行了研究,我认为作为评论(实际上在此线程中的答案下方:ProviderManager.authenticate called twice for BadCredentialsException)将是我的解决方案......但我仍然收到双重提交/调用以进行身份验证。第二个每次都有一个空密码。第一个调用有凭据。
下面是Java Config类和CustomAuthProvider类..
@Configuration
@EnableWebSecurity
public class UserWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//@formatter:off
http.antMatcher("/**")
.authorizeRequests()
.antMatchers("/", "/home**", "/login**","/create_user")
.permitAll().anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login")
.failureUrl("/login?error")
.and()
.logout()
.logoutSuccessUrl("/login?logout")
.permitAll()
.and()
.exceptionHandling().accessDeniedPage("/login?denied") //in this simple case usually due to a InvalidCsrfTokenException after session timeout
.and()
.csrf()
.ignoringAntMatchers("/rest/**")
.and()
.sessionManagement().enableSessionUrlRewriting(false)
.and()
.headers().frameOptions().deny();
}
... 然后是 customAuthProvider ...
@Component
public class CustomAuthProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
if (authentication.getName() == null) {
logger.warn("empty userName");
return null;
}
if (authentication.getCredentials() == null) {
logger.warn("empty password");
return null;
}
// code to check credentials etc ...
if (!(user != null && userHash.equals(storedHash))) {
System.out.println("fail");
return null;
}
return new UsernamePasswordAuthenticationToken(user,password);
}
【问题讨论】:
-
修改了授权提供者代码以一个空的授权列表结束...
标签: java authentication spring-boot custom-authentication