Search in sources :

Example 91 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project atlas by apache.

the class FileAuthenticationTest method testInValidUsernameLogin.

@Test
public void testInValidUsernameLogin() {
    when(authentication.getName()).thenReturn("wrongUserName");
    when(authentication.getCredentials()).thenReturn("wrongpassword");
    try {
        Authentication auth = authProvider.authenticate(authentication);
        LOG.debug(" {}", auth);
    } catch (UsernameNotFoundException uExp) {
        assertTrue(uExp.getMessage().contains("Username not found."));
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) Authentication(org.springframework.security.core.Authentication) Test(org.testng.annotations.Test)

Example 92 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project atlas by apache.

the class FileAuthenticationTest method testLoginWhenRolePasswordNotSet.

@Test
public void testLoginWhenRolePasswordNotSet() {
    // for this user password details are set blank
    when(authentication.getName()).thenReturn("user");
    when(authentication.getCredentials()).thenReturn("P@ssword");
    try {
        Authentication auth = authProvider.authenticate(authentication);
        LOG.debug(" {}", auth);
    } catch (UsernameNotFoundException uExp) {
        assertTrue(uExp.getMessage().startsWith("Username not found"));
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) Authentication(org.springframework.security.core.Authentication) Test(org.testng.annotations.Test)

Example 93 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project atlas by apache.

the class UserDaoTest method testUserDaowithInValidLogin.

@Test
public void testUserDaowithInValidLogin() {
    boolean hadException = false;
    Properties userLogins = new Properties();
    userLogins.put("admin", "ADMIN::admin123");
    userLogins.put("test", "DATA_STEWARD::test123");
    UserDao user = new UserDao();
    user.setUserLogins(userLogins);
    try {
        User userBean = user.loadUserByUsername("xyz");
    } catch (UsernameNotFoundException uex) {
        hadException = true;
    }
    assertTrue(hadException);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(org.apache.atlas.web.model.User) UserDao(org.apache.atlas.web.dao.UserDao) Properties(java.util.Properties) Test(org.testng.annotations.Test)

Example 94 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project danyuan-application by 514840279.

the class CustomUserDetailsService method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
    SysUserBaseInfo user;
    try {
        user = sysUserBaseService.findByName(userName);
    } catch (Exception e) {
        throw new UsernameNotFoundException("user select fail");
    }
    if (user == null) {
        throw new UsernameNotFoundException("no user found");
    } else {
        try {
            List<SysMenuInfo> menu = sysUserBaseService.getRoleByUser(user.getUuid());
            List<GrantedAuthority> gas = new ArrayList<>();
            if (menu != null) {
                for (SysMenuInfo sysMenuInfo : menu) {
                    gas.add(new SimpleGrantedAuthority(sysMenuInfo.getName()));
                }
            }
            // gas.add(new SwitchUserGrantedAuthority("ROLE_USER", new Authentication()));
            UserDetails users = new User(user.getUserName(), user.getPassword(), true, true, true, true, gas);
            return users;
        } catch (Exception e) {
            throw new UsernameNotFoundException("user role select fail");
        }
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) UserDetails(org.springframework.security.core.userdetails.UserDetails) User(org.springframework.security.core.userdetails.User) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SysMenuInfo(tk.ainiyue.danyuan.application.softm.sysmenu.po.SysMenuInfo) ArrayList(java.util.ArrayList) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SysUserBaseInfo(tk.ainiyue.danyuan.application.user.userbase.po.SysUserBaseInfo)

Example 95 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project atlas by apache.

the class UserDao method loadUserByUsername.

public User loadUserByUsername(final String username) throws AuthenticationException {
    String userdetailsStr = userLogins.getProperty(username);
    if (userdetailsStr == null || userdetailsStr.isEmpty()) {
        throw new UsernameNotFoundException("Username not found." + username);
    }
    String password = "";
    String role = "";
    String[] dataArr = userdetailsStr.split("::");
    if (dataArr != null && dataArr.length == 2) {
        role = dataArr[0];
        password = dataArr[1];
    } else {
        LOG.error("User role credentials is not set properly for {}", username);
        throw new AtlasAuthenticationException("User role credentials is not set properly for " + username);
    }
    List<GrantedAuthority> grantedAuths = new ArrayList<>();
    if (StringUtils.hasText(role)) {
        grantedAuths.add(new SimpleGrantedAuthority(role));
    } else {
        LOG.error("User role credentials is not set properly for {}", username);
        throw new AtlasAuthenticationException("User role credentials is not set properly for " + username);
    }
    User userDetails = new User(username, password, grantedAuths);
    return userDetails;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.apache.atlas.web.model.User) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) AtlasAuthenticationException(org.apache.atlas.web.security.AtlasAuthenticationException)

Aggregations

UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)132 GrantedAuthority (org.springframework.security.core.GrantedAuthority)40 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)39 UserDetails (org.springframework.security.core.userdetails.UserDetails)36 Authentication (org.springframework.security.core.Authentication)24 Transactional (org.springframework.transaction.annotation.Transactional)20 Logger (org.slf4j.Logger)18 LoggerFactory (org.slf4j.LoggerFactory)18 java.util (java.util)16 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)15 Collectors (java.util.stream.Collectors)14 UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)14 Component (org.springframework.stereotype.Component)14 User (org.springframework.security.core.userdetails.User)13 ArrayList (java.util.ArrayList)12 HashSet (java.util.HashSet)11 UserRepository (io.github.jhipster.sample.repository.UserRepository)9 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)9 User (io.github.jhipster.sample.domain.User)6 Date (java.util.Date)6