どうでしょ認証に対しアクティブディレクトリをサーバーを用いた安全保障?

StackOverflow https://stackoverflow.com/questions/84680

質問

私は書泉るwebアプリケーションが必要でユーザーの"login"ボタンをクリックします。会社アクティブディレクトリをサーバーということです。しかし、うまく利用春安全保障への接続、サーバーにコピーします。

を使用してい春2.5.5春セ2.0.3、Java1.6.

を変更した場合、購入したのLDAP URLに違うIPアドレスされているとは思いませんが、例外をスローも何もなかった場合でも しようと サーバーに接続できます。

このウェブアプリケーションの起動は、イブレア城、イブレアは、個人情報の取り扱いに関に入ると、ログインページは拒否されます。また利用したInMemoryDaoImpl、現代美術館などで、私の願いよう正確に設定されていること。

ここで私のセキュリティ関連の豆:

  <beans:bean id="ldapAuthProvider" class="org.springframework.security.providers.ldap.LdapAuthenticationProvider">
    <beans:constructor-arg>
      <beans:bean class="org.springframework.security.providers.ldap.authenticator.BindAuthenticator">
        <beans:constructor-arg ref="initialDirContextFactory" />
        <beans:property name="userDnPatterns">
          <beans:list>
            <beans:value>CN={0},OU=SBSUsers,OU=Users,OU=MyBusiness,DC=Acme,DC=com</beans:value>
          </beans:list>
        </beans:property>
      </beans:bean>
    </beans:constructor-arg>
  </beans:bean>

  <beans:bean id="userDetailsService" class="org.springframework.security.userdetails.ldap.LdapUserDetailsManager">
    <beans:constructor-arg ref="initialDirContextFactory" />
  </beans:bean>

  <beans:bean id="initialDirContextFactory" class="org.springframework.security.ldap.DefaultInitialDirContextFactory">
    <beans:constructor-arg value="ldap://192.168.123.456:389/DC=Acme,DC=com" />
  </beans:bean>
役に立ちましたか?

解決

私は同じで叩-my-ヘッドに対し、経験したアクティビティの終文書のカスタム認証プロバイダは、LDAPクエリに対してアクティブディレクトリサーバーです。

私のセキュリティ関連の豆には:

<beans:bean id="contextSource"
    class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
    <beans:constructor-arg value="ldap://hostname.queso.com:389/" />
</beans:bean>

<beans:bean id="ldapAuthenticationProvider"
    class="org.queso.ad.service.authentication.LdapAuthenticationProvider">
    <beans:property name="authenticator" ref="ldapAuthenticator" />
    <custom-authentication-provider />
</beans:bean>

<beans:bean id="ldapAuthenticator"
    class="org.queso.ad.service.authentication.LdapAuthenticatorImpl">
    <beans:property name="contextFactory" ref="contextSource" />
    <beans:property name="principalPrefix" value="QUESO\" />
</beans:bean>

その後、LdapAuthenticationProviderクラス:

/**
 * Custom Spring Security authentication provider which tries to bind to an LDAP server with
 * the passed-in credentials; of note, when used with the custom {@link LdapAuthenticatorImpl},
 * does <strong>not</strong> require an LDAP username and password for initial binding.
 * 
 * @author Jason
 */
public class LdapAuthenticationProvider implements AuthenticationProvider {

    private LdapAuthenticator authenticator;

    public Authentication authenticate(Authentication auth) throws AuthenticationException {

        // Authenticate, using the passed-in credentials.
        DirContextOperations authAdapter = authenticator.authenticate(auth);

        // Creating an LdapAuthenticationToken (rather than using the existing Authentication
        // object) allows us to add the already-created LDAP context for our app to use later.
        LdapAuthenticationToken ldapAuth = new LdapAuthenticationToken(auth, "ROLE_USER");
        InitialLdapContext ldapContext = (InitialLdapContext) authAdapter
                .getObjectAttribute("ldapContext");
        if (ldapContext != null) {
            ldapAuth.setContext(ldapContext);
        }

        return ldapAuth;
    }

    public boolean supports(Class clazz) {
        return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(clazz));
    }

    public LdapAuthenticator getAuthenticator() {
        return authenticator;
    }

    public void setAuthenticator(LdapAuthenticator authenticator) {
        this.authenticator = authenticator;
    }

}

その後、LdapAuthenticatorImplクラス:

/**
 * Custom Spring Security LDAP authenticator which tries to bind to an LDAP server using the
 * passed-in credentials; does <strong>not</strong> require "master" credentials for an
 * initial bind prior to searching for the passed-in username.
 * 
 * @author Jason
 */
public class LdapAuthenticatorImpl implements LdapAuthenticator {

    private DefaultSpringSecurityContextSource contextFactory;
    private String principalPrefix = "";

    public DirContextOperations authenticate(Authentication authentication) {

        // Grab the username and password out of the authentication object.
        String principal = principalPrefix + authentication.getName();
        String password = "";
        if (authentication.getCredentials() != null) {
            password = authentication.getCredentials().toString();
        }

        // If we have a valid username and password, try to authenticate.
        if (!("".equals(principal.trim())) && !("".equals(password.trim()))) {
            InitialLdapContext ldapContext = (InitialLdapContext) contextFactory
                    .getReadWriteContext(principal, password);

            // We need to pass the context back out, so that the auth provider can add it to the
            // Authentication object.
            DirContextOperations authAdapter = new DirContextAdapter();
            authAdapter.addAttributeValue("ldapContext", ldapContext);

            return authAdapter;
        } else {
            throw new BadCredentialsException("Blank username and/or password!");
        }
    }

    /**
     * Since the InitialLdapContext that's stored as a property of an LdapAuthenticationToken is
     * transient (because it isn't Serializable), we need some way to recreate the
     * InitialLdapContext if it's null (e.g., if the LdapAuthenticationToken has been serialized
     * and deserialized). This is that mechanism.
     * 
     * @param authenticator
     *          the LdapAuthenticator instance from your application's context
     * @param auth
     *          the LdapAuthenticationToken in which to recreate the InitialLdapContext
     * @return
     */
    static public InitialLdapContext recreateLdapContext(LdapAuthenticator authenticator,
            LdapAuthenticationToken auth) {
        DirContextOperations authAdapter = authenticator.authenticate(auth);
        InitialLdapContext context = (InitialLdapContext) authAdapter
                .getObjectAttribute("ldapContext");
        auth.setContext(context);
        return context;
    }

    public DefaultSpringSecurityContextSource getContextFactory() {
        return contextFactory;
    }

    /**
     * Set the context factory to use for generating a new LDAP context.
     * 
     * @param contextFactory
     */
    public void setContextFactory(DefaultSpringSecurityContextSource contextFactory) {
        this.contextFactory = contextFactory;
    }

    public String getPrincipalPrefix() {
        return principalPrefix;
    }

    /**
     * Set the string to be prepended to all principal names prior to attempting authentication
     * against the LDAP server.  (For example, if the Active Directory wants the domain-name-plus
     * backslash prepended, use this.)
     * 
     * @param principalPrefix
     */
    public void setPrincipalPrefix(String principalPrefix) {
        if (principalPrefix != null) {
            this.principalPrefix = principalPrefix;
        } else {
            this.principalPrefix = "";
        }
    }

}

最後に、LdapAuthenticationTokenクラス:

/**
 * <p>
 * Authentication token to use when an app needs further access to the LDAP context used to
 * authenticate the user.
 * </p>
 * 
 * <p>
 * When this is the Authentication object stored in the Spring Security context, an application
 * can retrieve the current LDAP context thusly:
 * </p>
 * 
 * <pre>
 * LdapAuthenticationToken ldapAuth = (LdapAuthenticationToken) SecurityContextHolder
 *      .getContext().getAuthentication();
 * InitialLdapContext ldapContext = ldapAuth.getContext();
 * </pre>
 * 
 * @author Jason
 * 
 */
public class LdapAuthenticationToken extends AbstractAuthenticationToken {

    private static final long serialVersionUID = -5040340622950665401L;

    private Authentication auth;
    transient private InitialLdapContext context;
    private List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

    /**
     * Construct a new LdapAuthenticationToken, using an existing Authentication object and
     * granting all users a default authority.
     * 
     * @param auth
     * @param defaultAuthority
     */
    public LdapAuthenticationToken(Authentication auth, GrantedAuthority defaultAuthority) {
        this.auth = auth;
        if (auth.getAuthorities() != null) {
            this.authorities.addAll(Arrays.asList(auth.getAuthorities()));
        }
        if (defaultAuthority != null) {
            this.authorities.add(defaultAuthority);
        }
        super.setAuthenticated(true);
    }

    /**
     * Construct a new LdapAuthenticationToken, using an existing Authentication object and
     * granting all users a default authority.
     * 
     * @param auth
     * @param defaultAuthority
     */
    public LdapAuthenticationToken(Authentication auth, String defaultAuthority) {
        this(auth, new GrantedAuthorityImpl(defaultAuthority));
    }

    public GrantedAuthority[] getAuthorities() {
        GrantedAuthority[] authoritiesArray = this.authorities.toArray(new GrantedAuthority[0]);
        return authoritiesArray;
    }

    public void addAuthority(GrantedAuthority authority) {
        this.authorities.add(authority);
    }

    public Object getCredentials() {
        return auth.getCredentials();
    }

    public Object getPrincipal() {
        return auth.getPrincipal();
    }

    /**
     * Retrieve the LDAP context attached to this user's authentication object.
     * 
     * @return the LDAP context
     */
    public InitialLdapContext getContext() {
        return context;
    }

    /**
     * Attach an LDAP context to this user's authentication object.
     * 
     * @param context
     *          the LDAP context
     */
    public void setContext(InitialLdapContext context) {
        this.context = context;
    }

}

いがビットがない場合がございます。

例えば、私のアプリに必要な維持に成功-ログインLDAPコンテキストの一層の活用により、ユーザー一度ログインする--アプリの目的はユーザーがログインを通じて広告の資格として更なる広告関連の機能を呼び出すでは、どうしたらいいでしょうかいカスタム認証トークンをLdapAuthenticationToken、その過程ではなく春のデフォルトの認証トークン)を行なっていを添付してLDAPコンテキストにLdapAuthenticationProvider.認証()では,このトークンを渡し、にLdapAuthenticatorImpl.認証()まで添付してログインコンテキストに返却オブジェクトできるように追加するユーザーの春認証オブジェクトです。

また、LdapAuthenticationProvider.認証()、割り当てすべてのログインユーザーのROLE_USER役割が何を作ってもらったこともうれしかった試験のためにその役割を果た私の傍受-urlです。またこの試合を与えたいテスト、役割分担を行いながらに基づくィングなどを実行します。

最後に、逆にその実施LdapAuthenticationProvider.認証してくださいすべてのユーザーに有効な広告を占同ROLE_USERます。明らかにし、この方法をお渡ししますので、さらなる試験を、ユーザーは、ユーザーが特定の広告グループ?) と役割分担を行いながらそのように、試験の一部の状態も付与のユーザーのアクセスで すべての.

他のヒント

参考までに、春の安全3.1は、認証プロバイダ 具体的には活発なディレクトリ.

だくことを最新の状態にします。春セ3.0は 完全なパッケージ デフォルトの実装にてldap-結合してクエリと比較認証を行います。

ることができた認証に対し活躍ディレクトリを用いた安全保障2.0.4.

私は文書化され、設定

http://maniezhilan.blogspot.com/2008/10/spring-security-204-with-active.html

としてルークからの回答上:

春安全3.1は、認証プロバイダにアクティブディレクトリです。

こちらの方が簡単にでき用ActiveDirectoryLdapAuthenticationprovider.

に資源です。groovy:

ldapAuthProvider1(ActiveDirectoryLdapAuthenticationProvider,
        "mydomain.com",
        "ldap://mydomain.com/"
)

Config.groovy:

grails.plugin.springsecurity.providerNames = ['ldapAuthProvider1']

これらはすべてのコードはあります。できるというのを全て取り除くその他のgrails.引き出しおよび設定ができますspringsecurity.ldap.* 設定Config.groovyとしていることに広告設定します。

ドキュメンテーショーにて、http://docs.spring.io/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#ldap-active-directory

LDAP認証なSSLではない安全なもので、ユーザー資格のある場合は移植にLDAPサーバーです。であることをLDAPS:\プロトコルによる認証を行う。必要としないことから大きな変化が自然にできた一部の問題に関連しています。見 LDAP Active Directoryの認証春SSL 詳しくは

からルークからの回答上:

参考までに、春の安全3.1は、認証プロバイダ 【具体的には活発なディレクトリ]に[1]です。

[1]: http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#ldap-active-directory

また、上記の春キ3.1.1:ありが微妙に変化からldapのactive directoryのユーザーが会員の通ったとします。

以下のldapのグループが資本および資を先頭に付け"ROLE_"、していない商品については、見るにはテキスト検索プロジェクトが明らかな場合には問題unixグループの場合にあった2グループに分ける場合(ie座の口座).

また、書式]要求マニュアル仕様のドメインコントローラの名前とポートで同期することにより、というのが、ちょっと怖いよう冗長化.はあるのではないでしょうかを見るには、SRV DNSレコードのドメインのjava、相当(からサンバ4つのハウトゥ):

$ host -t SRV _ldap._tcp.samdom.example.com.
_ldap._tcp.samdom.example.com has SRV record 0 100 389 samba.samdom.example.com.

その後正規のルックアップ:

$ host -t A samba.samdom.example.com.
samba.samdom.example.com has address 10.0.0.1

(実際に必要なすルックアップ_kerberos SRVの記録ですが...。)

上記のたSamba4.0rc1ま進むからのアップグレードのサンバ3.x LDAP環境サンバの広告です。

ご利用の場合春 安全保障4 でも実施する同利用 指定されたクラス

  • SecurityConfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


static final Logger LOGGER = LoggerFactory.getLogger(SecurityConfig.class);

@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
              .antMatchers("/").permitAll()
              .anyRequest().authenticated();
            .and()
              .formLogin()
            .and()
              .logout();
}

@Bean
public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
    ActiveDirectoryLdapAuthenticationProvider authenticationProvider = 
        new ActiveDirectoryLdapAuthenticationProvider("<domain>", "<url>");

    authenticationProvider.setConvertSubErrorCodesToExceptions(true);
    authenticationProvider.setUseAuthenticationRequestCredentials(true);

    return authenticationProvider;
}
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top