@Test public void testAuthenticationNullPassword() { expect(mockAuthPropsConfig.isAuthFileEnabled()).andReturn(true); replay(mockAuthPropsConfig); Authentication authentication = new TestingAuthenticationToken("principal", null); try { provider.authenticate(authentication); assertTrue("Should have thrown BadCredentialsException", false); } catch(BadCredentialsException e) { assertEquals("Password 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."); } String encPassword = CommonUtil.encryptPassword(username, password); if (!encPassword.equals(user.getPassword())) { logger.error("Wrong password for user=" + username); throw new BadCredentialsException("Wrong password."); } Collection<? extends GrantedAuthority> authorities = user.getAuthorities(); authentication = new UsernamePasswordAuthenticationToken(username, encPassword, 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."); } String encPassword = CommonUtil.encryptPassword(username, password); if (!encPassword.equals(user.getPassword())) { logger.error("Wrong password for user=" + username); throw new BadCredentialsException("Wrong password."); } Collection<? extends GrantedAuthority> authorities = user.getAuthorities(); authentication = new UsernamePasswordAuthenticationToken(username, encPassword, 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."); } String encPassword = CommonUtil.encryptPassword(username, password); if (!encPassword.equals(user.getPassword())) { logger.error("Wrong password for user=" + username); throw new BadCredentialsException("Wrong password."); } Collection<? extends GrantedAuthority> authorities = user.getAuthorities(); authentication = new UsernamePasswordAuthenticationToken(username, encPassword, 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."); } String encPassword = CommonUtil.encryptPassword(username, password); if (!encPassword.equals(user.getPassword())) { logger.error("Wrong password for user=" + username); throw new BadCredentialsException("Wrong password."); } Collection<? extends GrantedAuthority> authorities = user.getAuthorities(); authentication = new UsernamePasswordAuthenticationToken(username, encPassword, authorities); return authentication; }  @Override Authentication authenticate(Authentication 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."); } String encPassword = CommonUtil.encryptPassword(username, password); if (!encPassword.equals(user.getPassword())) { logger.error("Wrong password for user=" + username); throw new BadCredentialsException("Wrong password."); } Collection<? extends GrantedAuthority> authorities = user.getAuthorities(); authentication = new UsernamePasswordAuthenticationToken(username, encPassword, authorities); return authentication; }  @Override Authentication authenticate(Authentication authentication);  }