1

私は春のセキュリティのサンプル使用例を持っています:

<sec:http auto-config="true" xmlns="http://www.springframework.org/schema/security">
    <intercept-url pattern="/secure/**" access="ROLE_USER" />
    <intercept-url pattern="/admin/**" access="ROLE_ADMIN" />
    <sec:anonymous username="lolka"/>
</sec:http>

<sec:authentication-manager xmlns="http://www.springframework.org/schema/security">
    <authentication-provider>
        <user-service>
            <user name="admin" password="adminpassword" authorities="ROLE_USER, ROLE_ADMIN" />
            <user name="user" password="userpassword" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</sec:authentication-manager>

このサンプル構成ではAnonymousAuthenticationFilter、anon ユーザーを処理し、my プリンシパル名で提供するように作成されていますlolka。このフィルタの生成は で発生しましたAuthenticationConfigBuilder.createAnonymousFilter。そのメソッドのコードを開くと、 に割り当てられた AnonymousAuthenticationProvider も作成されていることがわかります。そのanonymousProviderRefため、この authenticationProvaderは認証プロバイダーのリストに存在する必要がありますが、実行時に認証プロバイダーのリストを取得すると、タグDaoAuthenticationProviderから作成されるのみ。sec:authentication-manager

実行時に認証プロバイダーを取得するための私のコードがあります: @Autowired private ApplicationContext applicationContext;

@PostConstruct
public void init(){
    Object bean = applicationContext.getBean("authenticationManager");
    log.info("authenticationManager:{}",bean);
    ProviderManager vefified = (ProviderManager) bean;
    log.info("providers:{}",vefified.getProviders());


    try {
        Field ff = vefified.getClass().getDeclaredField("parent"); 
        ff.setAccessible(true);
        Object parentObj = ff.get(vefified);
        if(ProviderManager.class.isInstance(parentObj)){
            log.info("parent is instance of ProviderManager:{}", parentObj.getClass().getSimpleName());
            ProviderManager verifyedParent = (ProviderManager)parentObj;
            log.info("parent providers list:{}", verifyedParent.getProviders());
        } else {
            log.info("parent is NOT instance of ProviderManager:{}", parentObj);
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }

    AnonymousAuthenticationProvider anonProvider = applicationContext.getBean(AnonymousAuthenticationProvider.class);
    log.info("AnonymousAuthenticationProvider:{}",anonProvider);

}

そして出力があります:

INFO (Main.java:35) authenticationManager:org.springframework.security.authentication.ProviderManager@1566060
INFO (Main.java:37) providers:[org.springframework.security.authentication.dao.DaoAuthenticationProvider@1d1695e]
INFO (Main.java:49) parent is NOT instance of ProviderManager:null
INFO (Main.java:56) AnonymousAuthenticationProvider:org.springframework.security.authentication.AnonymousAuthenticationProvider@187d27e

だから私の質問は:

  1. AnonymousAuthenticationProvider を使用する場合、その目的は何ですか?
  2. AnonymousAuthenticationProvider が実行時に存在するのに、作業中のリストに存在しないのはなぜauthenticationManagerですか?

Spring のバージョン: 3.2.4.RELEASE Spring のセキュリティ バージョン: 3.1.4.RELEASE

4

1 に答える 1