@Test public void testAuthenticationEmptyUser() { expect(mockAuthPropsConfig.isAuthFileEnabled()).andReturn(true); replay(mockAuthPropsConfig); Authentication authentication = new TestingAuthenticationToken("", "credentials"); try { provider.authenticate(authentication); fail("Should have thrown BadCredentialsException"); } catch(BadCredentialsException e) { assertEquals("Username can't be null or empty.", e.getMessage()); } verify(mockAuthPropsConfig); }
@Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!authPropsConfig.isAuthFileEnabled()) { logger.debug("File auth is disabled."); return authentication; } String username = authentication.getName(); String password = (String) authentication.getCredentials(); if (StringUtils.isBlank(username)) { throw new BadCredentialsException("Username can't be null or empty."); } if (StringUtils.isBlank(password)) { throw new BadCredentialsException("Password can't be null or empty."); } password = StringEscapeUtils.unescapeHtml(password); username = StringEscapeUtils.unescapeHtml(username); UserDetails user = userDetailsService.loadUserByUsername(username); if (user == null) { logger.error("Username not found."); throw new BadCredentialsException("User not found."); } if (StringUtils.isEmpty(user.getPassword())) { logger.error("Password can't be null or empty."); throw new BadCredentialsException("Password can't be null or empty."); } if (!passwordEncoder.matches(password, user.getPassword())) { logger.error("Wrong password for user=" + username); throw new BadCredentialsException("Wrong password."); } Collection<? extends GrantedAuthority> authorities = user.getAuthorities(); authentication = new UsernamePasswordAuthenticationToken(username, user.getPassword(), authorities); return authentication; }
LogsearchFileAuthenticationProvider extends LogsearchAbstractAuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!authPropsConfig.isAuthFileEnabled()) { logger.debug("File auth is disabled."); return authentication; } String username = authentication.getName(); String password = (String) authentication.getCredentials(); if (StringUtils.isBlank(username)) { throw new BadCredentialsException("Username can't be null or empty."); } if (StringUtils.isBlank(password)) { throw new BadCredentialsException("Password can't be null or empty."); } password = StringEscapeUtils.unescapeHtml(password); username = StringEscapeUtils.unescapeHtml(username); UserDetails user = userDetailsService.loadUserByUsername(username); if (user == null) { logger.error("Username not found."); throw new BadCredentialsException("User not found."); } if (StringUtils.isEmpty(user.getPassword())) { logger.error("Password can't be null or empty."); throw new BadCredentialsException("Password can't be null or empty."); } if (!passwordEncoder.matches(password, user.getPassword())) { logger.error("Wrong password for user=" + username); throw new BadCredentialsException("Wrong password."); } Collection<? extends GrantedAuthority> authorities = user.getAuthorities(); authentication = new UsernamePasswordAuthenticationToken(username, user.getPassword(), authorities); return authentication; } }
LogsearchFileAuthenticationProvider extends LogsearchAbstractAuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!authPropsConfig.isAuthFileEnabled()) { logger.debug("File auth is disabled."); return authentication; } String username = authentication.getName(); String password = (String) authentication.getCredentials(); if (StringUtils.isBlank(username)) { throw new BadCredentialsException("Username can't be null or empty."); } if (StringUtils.isBlank(password)) { throw new BadCredentialsException("Password can't be null or empty."); } password = StringEscapeUtils.unescapeHtml(password); username = StringEscapeUtils.unescapeHtml(username); UserDetails user = userDetailsService.loadUserByUsername(username); if (user == null) { logger.error("Username not found."); throw new BadCredentialsException("User not found."); } if (StringUtils.isEmpty(user.getPassword())) { logger.error("Password can't be null or empty."); throw new BadCredentialsException("Password can't be null or empty."); } if (!passwordEncoder.matches(password, user.getPassword())) { logger.error("Wrong password for user=" + username); throw new BadCredentialsException("Wrong password."); } Collection<? extends GrantedAuthority> authorities = user.getAuthorities(); authentication = new UsernamePasswordAuthenticationToken(username, user.getPassword(), authorities); return authentication; }  }
LogsearchFileAuthenticationProvider extends LogsearchAbstractAuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!authPropsConfig.isAuthFileEnabled()) { logger.debug("File auth is disabled."); return authentication; } String username = authentication.getName(); String password = (String) authentication.getCredentials(); if (StringUtils.isBlank(username)) { throw new BadCredentialsException("Username can't be null or empty."); } if (StringUtils.isBlank(password)) { throw new BadCredentialsException("Password can't be null or empty."); } password = StringEscapeUtils.unescapeHtml(password); username = StringEscapeUtils.unescapeHtml(username); UserDetails user = userDetailsService.loadUserByUsername(username); if (user == null) { logger.error("Username not found."); throw new BadCredentialsException("User not found."); } if (StringUtils.isEmpty(user.getPassword())) { logger.error("Password can't be null or empty."); throw new BadCredentialsException("Password can't be null or empty."); } if (!passwordEncoder.matches(password, user.getPassword())) { logger.error("Wrong password for user=" + username); throw new BadCredentialsException("Wrong password."); } Collection<? extends GrantedAuthority> authorities = user.getAuthorities(); authentication = new UsernamePasswordAuthenticationToken(username, user.getPassword(), authorities); return authentication; }  @Override Authentication authenticate(Authentication authentication); @VisibleForTesting void setPasswordEncoder(PasswordEncoder passwordEncoder); }
LogsearchFileAuthenticationProvider extends LogsearchAbstractAuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!authPropsConfig.isAuthFileEnabled()) { logger.debug("File auth is disabled."); return authentication; } String username = authentication.getName(); String password = (String) authentication.getCredentials(); if (StringUtils.isBlank(username)) { throw new BadCredentialsException("Username can't be null or empty."); } if (StringUtils.isBlank(password)) { throw new BadCredentialsException("Password can't be null or empty."); } password = StringEscapeUtils.unescapeHtml(password); username = StringEscapeUtils.unescapeHtml(username); UserDetails user = userDetailsService.loadUserByUsername(username); if (user == null) { logger.error("Username not found."); throw new BadCredentialsException("User not found."); } if (StringUtils.isEmpty(user.getPassword())) { logger.error("Password can't be null or empty."); throw new BadCredentialsException("Password can't be null or empty."); } if (!passwordEncoder.matches(password, user.getPassword())) { logger.error("Wrong password for user=" + username); throw new BadCredentialsException("Wrong password."); } Collection<? extends GrantedAuthority> authorities = user.getAuthorities(); authentication = new UsernamePasswordAuthenticationToken(username, user.getPassword(), authorities); return authentication; }  @Override Authentication authenticate(Authentication authentication); @VisibleForTesting void setPasswordEncoder(PasswordEncoder passwordEncoder);  }