类org.springframework.security.authentication.encoding.LdapShaPasswordEncoder源码实例Demo

下面列出了怎么用org.springframework.security.authentication.encoding.LdapShaPasswordEncoder的API类实例代码及写法,或者点击链接到github查看源代码。


/**
     * Configure AuthenticationManager with inMemory credentials.
     *
     * NOTE:
     * Due to a known limitation with JavaConfig:
     * <a href="https://jira.spring.io/browse/SPR-13779">
     *     https://jira.spring.io/browse/SPR-13779</a>
     *
     * We cannot use the following to expose a {@link UserDetailsManager}
     * <pre>
     *     http.authorizeRequests()
     * </pre>
     *
     * In order to expose {@link UserDetailsManager} as a bean, we must create  @Bean
     *
     * @see {@link super.userDetailsService()}
     * @see {@link com.packtpub.springsecurity.service.DefaultCalendarService}
     *
     * @param auth       AuthenticationManagerBuilder
     * @throws Exception Authentication exception
     */
    @Override
    public void configure(final AuthenticationManagerBuilder auth) throws Exception {
        auth
                .ldapAuthentication()
                .userSearchBase("")
                .userSearchFilter("(uid={0})")
                .groupSearchBase("ou=Groups")
                .groupSearchFilter("(uniqueMember={0})")
                .userDetailsContextMapper(new InetOrgPersonContextMapper())
                .contextSource(contextSource())
//                .contextSource()
//                    .managerDn("uid=admin,ou=system")
//                    .managerPassword("secret")
//                    .url("ldap://localhost:33389/dc=jbcpcalendar,dc=com")
//                    .root("dc=jbcpcalendar,dc=com")
//                    .ldif("classpath:/ldif/calendar.ldif")
//                .and()
                    .passwordCompare()
                    // Supports {SHA} and {SSHA}
                    .passwordEncoder(new LdapShaPasswordEncoder())
                    .passwordAttribute("userPassword")
        ;
    }
 
源代码2 项目: onetwo   文件: PasswordEncoderTest.java

@Test
public void testSsha(){
	LdapShaPasswordEncoder encoder = new LdapShaPasswordEncoder();
	
	String rawPass = "test";
	String salt = "@#%AS%&DF_=PJ}{EB23+42342*()*^%$)_(*%^)";
	String res = encoder.encodePassword(rawPass, salt.getBytes());
	System.out.println("res:"+res);
	boolean valid = encoder.isPasswordValid(res, rawPass, "[email protected]#[email protected]");
	Assert.assertTrue(valid);
}
 

/**
     * Configure AuthenticationManager with inMemory credentials.
     *
     * NOTE:
     * Due to a known limitation with JavaConfig:
     * <a href="https://jira.spring.io/browse/SPR-13779">
     *     https://jira.spring.io/browse/SPR-13779</a>
     *
     * We cannot use the following to expose a {@link UserDetailsManager}
     * <pre>
     *     http.authorizeRequests()
     * </pre>
     *
     * In order to expose {@link UserDetailsManager} as a bean, we must create  @Bean
     *
     * @see {@link super.userDetailsService()}
     * @see {@link com.packtpub.springsecurity.service.DefaultCalendarService}
     *
     * @param auth       AuthenticationManagerBuilder
     * @throws Exception Authentication exception
     */
    @Override
    public void configure(final AuthenticationManagerBuilder auth) throws Exception {
        auth
                .ldapAuthentication()
//                .ldapAuthoritiesPopulator(ldapAuthoritiesPopulator())
                .userSearchBase("")
                .userSearchFilter("(uid={0})")
                .groupSearchBase("ou=Groups")
                .groupSearchFilter("(uniqueMember={0})")
//                .userDetailsContextMapper(new InetOrgPersonContextMapper())
                .contextSource(contextSource())
                .passwordCompare()
                    // Supports {SHA} and {SSHA}
                    .passwordEncoder(new LdapShaPasswordEncoder())
                    .passwordAttribute("userPassword")
        ;
        /*
        <ldap-authentication-provider server-ref="ldapServer"
        user-search-filter="(uid={0})"
        group-search-base="ou=Groups"
        user-details-class="inetOrgPerson">


    <bean id="ldapAuthenticationProvider"
            class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
        <constructor-arg ref="ldapBindAuthenticator"/>
        <constructor-arg ref="ldapAuthoritiesPopulator"/>
        <property name="userDetailsContextMapper" ref="ldapUserDetailsContextMapper"/>
    </bean>

    <bean id="ldapBindAuthenticator"
            class="org.springframework.security.ldap.authentication.BindAuthenticator">
        <constructor-arg ref="ldapServer"/>
        <property name="userSearch" ref="ldapSearch"/>
    </bean>

//    <bean id="ldapSearch"
//            class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
//        <constructor-arg value=""/> <!-- use-search-base -->
//        <constructor-arg value="(uid={0})"/> <!-- user-search-filter -->
//        <constructor-arg ref="ldapServer"/>
//    </bean>

//    <bean id="ldapAuthoritiesPopulator"
//            class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator">
//        <constructor-arg ref="ldapServer"/>
//        <constructor-arg value="ou=Groups"/>
//        <property name="groupSearchFilter" value="(uniqueMember={0})"/>
//    </bean>

         */
    }
 

/**
 * Configure AuthenticationManager with inMemory credentials.
 *
 * NOTE:
 * Due to a known limitation with JavaConfig:
 * <a href="https://jira.spring.io/browse/SPR-13779">
 *     https://jira.spring.io/browse/SPR-13779</a>
 *
 * We cannot use the following to expose a {@link UserDetailsManager}
 * <pre>
 *     http.authorizeRequests()
 * </pre>
 *
 * In order to expose {@link UserDetailsManager} as a bean, we must create  @Bean
 *
 * @see {@link super.userDetailsService()}
 * @see {@link com.packtpub.springsecurity.service.DefaultCalendarService}
 *
 * @param auth       AuthenticationManagerBuilder
 * @throws Exception Authentication exception
 */
@Override
public void configure(final AuthenticationManagerBuilder auth) throws Exception {
    auth
            .ldapAuthentication()
            .userSearchBase("")
            .userSearchFilter("(uid={0})")
            .groupSearchBase("ou=Groups")
            .groupSearchFilter("(uniqueMember={0})")
            .userDetailsContextMapper(new InetOrgPersonContextMapper())
            .contextSource(contextSource())
            .passwordCompare()
                // Supports {SHA} and {SSHA}
                .passwordEncoder(new LdapShaPasswordEncoder())
                .passwordAttribute("userPassword")
    ;
}
 
 类方法
 同包方法