Search in sources :

Example 1 with OrderComparator

use of org.springframework.core.OrderComparator in project spring-framework by spring-projects.

the class PerThisAspect method testMultiplePerTargetAspectsWithOrderAnnotation.

@Test
public void testMultiplePerTargetAspectsWithOrderAnnotation() throws SecurityException, NoSuchMethodException {
    TestBean target = new TestBean();
    int realAge = 65;
    target.setAge(realAge);
    List<Advisor> advisors = new LinkedList<>();
    PerTargetAspectWithOrderAnnotation10 aspect1 = new PerTargetAspectWithOrderAnnotation10();
    aspect1.count = 100;
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect1, "someBean1")));
    PerTargetAspectWithOrderAnnotation5 aspect2 = new PerTargetAspectWithOrderAnnotation5();
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect2, "someBean2")));
    Collections.sort(advisors, new OrderComparator());
    TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class);
    assertEquals("Around advice must NOT apply", realAge, itb.getAge());
    // Hit the method in the per clause to instantiate the aspect
    itb.getSpouse();
    assertEquals("Around advice must apply", 0, itb.getAge());
    assertEquals("Around advice must apply", 1, itb.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) SyntheticInstantiationAdvisor(org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) Advisor(org.springframework.aop.Advisor) OrderComparator(org.springframework.core.OrderComparator) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 2 with OrderComparator

use of org.springframework.core.OrderComparator in project spring-security by spring-projects.

the class ClearCredentialsMethodInvokingFactoryBean method createFilterChain.

/**
	 * Creates the {@code SecurityFilterChain} bean from an &lt;http&gt; element.
	 */
private BeanReference createFilterChain(Element element, ParserContext pc) {
    boolean secured = !OPT_SECURITY_NONE.equals(element.getAttribute(ATT_SECURED));
    if (!secured) {
        if (!StringUtils.hasText(element.getAttribute(ATT_PATH_PATTERN)) && !StringUtils.hasText(ATT_REQUEST_MATCHER_REF)) {
            pc.getReaderContext().error("The '" + ATT_SECURED + "' attribute must be used in combination with" + " the '" + ATT_PATH_PATTERN + "' or '" + ATT_REQUEST_MATCHER_REF + "' attributes.", pc.extractSource(element));
        }
        for (int n = 0; n < element.getChildNodes().getLength(); n++) {
            if (element.getChildNodes().item(n) instanceof Element) {
                pc.getReaderContext().error("If you are using <http> to define an unsecured pattern, " + "it cannot contain child elements.", pc.extractSource(element));
            }
        }
        return createSecurityFilterChainBean(element, pc, Collections.emptyList());
    }
    final BeanReference portMapper = createPortMapper(element, pc);
    final BeanReference portResolver = createPortResolver(portMapper, pc);
    ManagedList<BeanReference> authenticationProviders = new ManagedList<BeanReference>();
    BeanReference authenticationManager = createAuthenticationManager(element, pc, authenticationProviders);
    boolean forceAutoConfig = isDefaultHttpConfig(element);
    HttpConfigurationBuilder httpBldr = new HttpConfigurationBuilder(element, forceAutoConfig, pc, portMapper, portResolver, authenticationManager);
    AuthenticationConfigBuilder authBldr = new AuthenticationConfigBuilder(element, forceAutoConfig, pc, httpBldr.getSessionCreationPolicy(), httpBldr.getRequestCache(), authenticationManager, httpBldr.getSessionStrategy(), portMapper, portResolver, httpBldr.getCsrfLogoutHandler());
    httpBldr.setLogoutHandlers(authBldr.getLogoutHandlers());
    httpBldr.setEntryPoint(authBldr.getEntryPointBean());
    httpBldr.setAccessDeniedHandler(authBldr.getAccessDeniedHandlerBean());
    authenticationProviders.addAll(authBldr.getProviders());
    List<OrderDecorator> unorderedFilterChain = new ArrayList<OrderDecorator>();
    unorderedFilterChain.addAll(httpBldr.getFilters());
    unorderedFilterChain.addAll(authBldr.getFilters());
    unorderedFilterChain.addAll(buildCustomFilterList(element, pc));
    Collections.sort(unorderedFilterChain, new OrderComparator());
    checkFilterChainOrder(unorderedFilterChain, pc, pc.extractSource(element));
    // The list of filter beans
    List<BeanMetadataElement> filterChain = new ManagedList<BeanMetadataElement>();
    for (OrderDecorator od : unorderedFilterChain) {
        filterChain.add(od.bean);
    }
    return createSecurityFilterChainBean(element, pc, filterChain);
}
Also used : BeanMetadataElement(org.springframework.beans.BeanMetadataElement) Element(org.w3c.dom.Element) ManagedList(org.springframework.beans.factory.support.ManagedList) OrderComparator(org.springframework.core.OrderComparator) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) BeanReference(org.springframework.beans.factory.config.BeanReference) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 3 with OrderComparator

use of org.springframework.core.OrderComparator in project spring-framework by spring-projects.

the class PerThisAspect method testMultiplePerTargetAspects.

@Test
public void testMultiplePerTargetAspects() throws SecurityException, NoSuchMethodException {
    TestBean target = new TestBean();
    int realAge = 65;
    target.setAge(realAge);
    List<Advisor> advisors = new LinkedList<>();
    PerTargetAspect aspect1 = new PerTargetAspect();
    aspect1.count = 100;
    aspect1.setOrder(10);
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect1, "someBean1")));
    PerTargetAspect aspect2 = new PerTargetAspect();
    aspect2.setOrder(5);
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect2, "someBean2")));
    Collections.sort(advisors, new OrderComparator());
    TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class);
    assertEquals("Around advice must NOT apply", realAge, itb.getAge());
    // Hit the method in the per clause to instantiate the aspect
    itb.getSpouse();
    assertEquals("Around advice must apply", 0, itb.getAge());
    assertEquals("Around advice must apply", 1, itb.getAge());
}
Also used : PerTargetAspect(test.aop.PerTargetAspect) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) SyntheticInstantiationAdvisor(org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) Advisor(org.springframework.aop.Advisor) OrderComparator(org.springframework.core.OrderComparator) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

OrderComparator (org.springframework.core.OrderComparator)3 LinkedList (java.util.LinkedList)2 JoinPoint (org.aspectj.lang.JoinPoint)2 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)2 Test (org.junit.Test)2 Advisor (org.springframework.aop.Advisor)2 SyntheticInstantiationAdvisor (org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor)2 ITestBean (org.springframework.tests.sample.beans.ITestBean)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)1 BeanReference (org.springframework.beans.factory.config.BeanReference)1 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)1 ManagedList (org.springframework.beans.factory.support.ManagedList)1 Element (org.w3c.dom.Element)1 PerTargetAspect (test.aop.PerTargetAspect)1