<commit_before>package org.springframework.security.config;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.security.ui.webapp.AuthenticationProcessingFilter;
import org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint;
import org.springframework.security.ui.webapp.DefaultLoginPageGeneratingFilter;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author <NAME>
 * @version $Id$
 */
public class FormLoginBeanDefinitionParser implements BeanDefinitionParser {
    protected final Log logger = LogFactory.getLog(getClass());

    public static final String DEFAULT_FORM_LOGIN_FILTER_ID = "_formLoginFilter";
    public static final String DEFAULT_FORM_LOGIN_ENTRY_POINT_ID = "_formLoginEntryPoint";
    
    private static final String LOGIN_URL_ATTRIBUTE = "loginUrl";
    private static final String LOGIN_PAGE_ATTRIBUTE = "loginPage";

    private static final String FORM_LOGIN_TARGET_URL_ATTRIBUTE = "defaultTargetUrl";
    private static final String DEFAULT_FORM_LOGIN_TARGET_URL = "/index";

    private static final String FORM_LOGIN_AUTH_FAILURE_URL_ATTRIBUTE = "defaultTargetUrl";
    // TODO: Change AbstractProcessingFilter to not need a failure URL and just write a failure message
    // to the response if one isn't set.
    private static final String DEFAULT_FORM_LOGIN_AUTH_FAILURE_URL = "/loginError";


    public BeanDefinition parse(Element elt, ParserContext parserContext) {
        BeanDefinition filterBean = createFilterBean(elt);

        BeanDefinitionBuilder entryPointBuilder =
                BeanDefinitionBuilder.rootBeanDefinition(AuthenticationProcessingFilterEntryPoint.class);


        String loginPage = elt.getAttribute(LOGIN_PAGE_ATTRIBUTE);

        // If no login page has been defined, add in the default page generator.
        if (!StringUtils.hasText(loginPage)) {
            logger.info("No login page configured in form-login element. The default internal one will be used. Use" +
                    "the 'loginPage' attribute to specify the URL of the login page.");
            loginPage = DefaultLoginPageGeneratingFilter.DEFAULT_LOGIN_PAGE_URL;
            RootBeanDefinition loginPageFilter = new RootBeanDefinition(DefaultLoginPageGeneratingFilter.class);
            loginPageFilter.getConstructorArgumentValues().addGenericArgumentValue(filterBean);
            parserContext.getRegistry().registerBeanDefinition("_springSecurityLoginPageFilter", loginPageFilter);<commit_msg>Changed to use a BeanReference when creating default login page to prevent duplication of filter bean.<commit_after>package org.springframework.security.config;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.security.ui.webapp.AuthenticationProcessingFilter;
import org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint;
import org.springframework.security.ui.webapp.DefaultLoginPageGeneratingFilter;
import org.springframework.util.StringUtils;

import org.w3c.dom.Element;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author <NAME>
 * @version $Id$
 */
public class FormLoginBeanDefinitionParser implements BeanDefinitionParser {
    protected final Log logger = LogFactory.getLog(getClass());

    public static final String DEFAULT_FORM_LOGIN_FILTER_ID = "_formLoginFilter";
    public static final String DEFAULT_FORM_LOGIN_ENTRY_POINT_ID = "_formLoginEntryPoint";

    private static final String LOGIN_URL_ATTRIBUTE = "loginUrl";
    private static final String LOGIN_PAGE_ATTRIBUTE = "loginPage";

    private static final String FORM_LOGIN_TARGET_URL_ATTRIBUTE = "defaultTargetUrl";
    private static final String DEFAULT_FORM_LOGIN_TARGET_URL = "/index";

    private static final String FORM_LOGIN_AUTH_FAILURE_URL_ATTRIBUTE = "defaultTargetUrl";
    // TODO: Change AbstractProcessingFilter to not need a failure URL and just write a failure message
    // to the response if one isn't set.
    private static final String DEFAULT_FORM_LOGIN_AUTH_FAILURE_URL = "/loginError";


    public BeanDefinition parse(Element elt, ParserContext parserContext) {
        BeanDefinition filterBean = createFilterBean(elt);

        BeanDefinitionBuilder entryPointBuilder =
                BeanDefinitionBuilder.rootBeanDefinition(AuthenticationProcessingFilterEntryPoint.class);

        String loginPage = elt.getAttribute(LOGIN_PAGE_ATTRIBUTE);

        // If no login page has been defined, add in the default page generator.
        if (!StringUtils.hasText(loginPage)) {
            logger.info("No login page configured in form-login element. The default internal one will be used. Use" +
                    "the 'loginPage' attribute to specify the URL of the login page.");
            loginPage = DefaultLoginPageGeneratingFilter.DEFAULT_LOGIN_PAGE_URL;
            RootBeanDefinition loginPageFilter = new RootBeanDefinition(DefaultLoginPageGeneratingFilter.class);
            loginPageFilter.getConstructorArgumentValues().addGenericArgumentValue(
                    new RuntimeBeanReference(DEFAULT_FORM_LOGIN_FILTER_ID));
            parserContext.getRegistry().registerBeanDefinition("_springSecurityLoginPageFilter", loginPageFilter);
{'fraction_non_alphanumeric': 0.06974034620505992, 'fraction_numerical': 0.00033288948069241014, 'mean_word_length': 4.914370078740157, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7254331', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1460, 'n_words': 368}