Search in sources :

Example 1 with Md5PasswordEncoder

use of org.springframework.security.authentication.encoding.Md5PasswordEncoder in project ranger by apache.

the class RangerAuthenticationProvider method getJDBCAuthentication.

private Authentication getJDBCAuthentication(Authentication authentication, String encoder) throws AuthenticationException {
    try {
        ReflectionSaltSource saltSource = new ReflectionSaltSource();
        saltSource.setUserPropertyToUse("username");
        DaoAuthenticationProvider authenticator = new DaoAuthenticationProvider();
        authenticator.setUserDetailsService(userDetailsService);
        if (encoder != null && "SHA256".equalsIgnoreCase(encoder)) {
            authenticator.setPasswordEncoder(new ShaPasswordEncoder(256));
        } else if (encoder != null && "MD5".equalsIgnoreCase(encoder)) {
            authenticator.setPasswordEncoder(new Md5PasswordEncoder());
        }
        authenticator.setSaltSource(saltSource);
        String userName = "";
        String userPassword = "";
        if (authentication != null) {
            userName = authentication.getName();
            if (authentication.getCredentials() != null) {
                userPassword = authentication.getCredentials().toString();
            }
        }
        String rangerLdapDefaultRole = PropertiesUtil.getProperty("ranger.ldap.default.role", "ROLE_USER");
        if (userName != null && userPassword != null && !userName.trim().isEmpty() && !userPassword.trim().isEmpty()) {
            final List<GrantedAuthority> grantedAuths = new ArrayList<>();
            grantedAuths.add(new SimpleGrantedAuthority(rangerLdapDefaultRole));
            final UserDetails principal = new User(userName, userPassword, grantedAuths);
            final Authentication finalAuthentication = new UsernamePasswordAuthenticationToken(principal, userPassword, grantedAuths);
            authentication = authenticator.authenticate(finalAuthentication);
            return authentication;
        } else {
            if (authentication != null && !authentication.isAuthenticated()) {
                throw new BadCredentialsException("Bad credentials");
            }
        }
    } catch (BadCredentialsException e) {
        throw e;
    } catch (AuthenticationServiceException e) {
        throw e;
    } catch (AuthenticationException e) {
        throw e;
    } catch (Exception e) {
        throw e;
    }
    return authentication;
}
Also used : ShaPasswordEncoder(org.springframework.security.authentication.encoding.ShaPasswordEncoder) User(org.springframework.security.core.userdetails.User) AuthenticationException(org.springframework.security.core.AuthenticationException) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) AuthenticationServiceException(org.springframework.security.authentication.AuthenticationServiceException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) AuthenticationException(org.springframework.security.core.AuthenticationException) AuthenticationServiceException(org.springframework.security.authentication.AuthenticationServiceException) ReflectionSaltSource(org.springframework.security.authentication.dao.ReflectionSaltSource) Md5PasswordEncoder(org.springframework.security.authentication.encoding.Md5PasswordEncoder) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) UserDetails(org.springframework.security.core.userdetails.UserDetails) DaoAuthenticationProvider(org.springframework.security.authentication.dao.DaoAuthenticationProvider) Authentication(org.springframework.security.core.Authentication)

Example 2 with Md5PasswordEncoder

use of org.springframework.security.authentication.encoding.Md5PasswordEncoder in project commons-dao by reportportal.

the class HashFuctionTest method checkHashFunction.

@Test
public void checkHashFunction() {
    String testPassword = "value to encode";
    EncodePasswordTrigger trigger = new EncodePasswordTrigger();
    User user = new User();
    user.setPassword(testPassword);
    trigger.onBeforeConvert(new BeforeConvertEvent<>(user, null));
    Assert.assertEquals(user.getPassword(), new Md5PasswordEncoder().encodePassword(testPassword, null));
}
Also used : Md5PasswordEncoder(org.springframework.security.authentication.encoding.Md5PasswordEncoder) User(com.epam.ta.reportportal.database.entity.user.User) Test(org.junit.Test)

Example 3 with Md5PasswordEncoder

use of org.springframework.security.authentication.encoding.Md5PasswordEncoder in project paascloud-master by paascloud.

the class MD5Test method md5_SystemWideSaltSource.

/**
 * Md 5 system wide salt source.
 */
private static void md5_SystemWideSaltSource() {
    Md5PasswordEncoder md5 = new Md5PasswordEncoder();
    md5.setEncodeHashAsBase64(false);
    // 使用动态加密盐的只需要在注册用户的时候将第二个参数换成用户名即可
    String pwd = md5.encodePassword("123456", "acegisalt");
    log.info("MD5 SystemWideSaltSource: " + pwd + " len=" + pwd.length());
}
Also used : Md5PasswordEncoder(org.springframework.security.authentication.encoding.Md5PasswordEncoder)

Example 4 with Md5PasswordEncoder

use of org.springframework.security.authentication.encoding.Md5PasswordEncoder in project paascloud-master by paascloud.

the class MD5Test method md5.

/**
 * Md 5.
 */
private static void md5() {
    Md5PasswordEncoder md5 = new Md5PasswordEncoder();
    // false 表示:生成32位的Hex版, 这也是encodeHashAsBase64的, Acegi 默认配置; true  表示:生成24位的Base64版
    md5.setEncodeHashAsBase64(false);
    String pwd = md5.encodePassword("123456", null);
    log.info("MD5: " + pwd + " len=" + pwd.length());
}
Also used : Md5PasswordEncoder(org.springframework.security.authentication.encoding.Md5PasswordEncoder)

Aggregations

Md5PasswordEncoder (org.springframework.security.authentication.encoding.Md5PasswordEncoder)4 User (com.epam.ta.reportportal.database.entity.user.User)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 AuthenticationServiceException (org.springframework.security.authentication.AuthenticationServiceException)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 DaoAuthenticationProvider (org.springframework.security.authentication.dao.DaoAuthenticationProvider)1 ReflectionSaltSource (org.springframework.security.authentication.dao.ReflectionSaltSource)1 ShaPasswordEncoder (org.springframework.security.authentication.encoding.ShaPasswordEncoder)1 Authentication (org.springframework.security.core.Authentication)1 AuthenticationException (org.springframework.security.core.AuthenticationException)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1 User (org.springframework.security.core.userdetails.User)1 UserDetails (org.springframework.security.core.userdetails.UserDetails)1