Search in sources :

Example 1 with UserRoleAuthorizationInterceptor

use of org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor in project spring-framework by spring-projects.

the class MvcNamespaceTests method testInterceptors.

@Test
public void testInterceptors() throws Exception {
    loadBeanDefinitions("mvc-config-interceptors.xml");
    RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
    assertThat(mapping).isNotNull();
    mapping.setDefaultHandler(handlerMethod);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
    request.setRequestURI("/accounts/12345");
    request.addParameter("locale", "en");
    request.addParameter("theme", "green");
    HandlerExecutionChain chain = mapping.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(4);
    assertThat(chain.getInterceptorList().get(0) instanceof ConversionServiceExposingInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(1) instanceof LocaleChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(2) instanceof ThemeChangeInterceptor).isTrue();
    assertThat(chain.getInterceptorList().get(3) instanceof UserRoleAuthorizationInterceptor).isTrue();
    request.setRequestURI("/admin/users");
    chain = mapping.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(2);
    request.setRequestURI("/logged/accounts/12345");
    chain = mapping.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(3);
    request.setRequestURI("/foo/logged");
    chain = mapping.getHandler(request);
    assertThat(chain.getInterceptorList().size()).isEqualTo(3);
}
Also used : LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) UserRoleAuthorizationInterceptor(org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) Test(org.junit.jupiter.api.Test)

Example 2 with UserRoleAuthorizationInterceptor

use of org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor in project spring-framework by spring-projects.

the class ComplexWebApplicationContext method refresh.

@Override
@SuppressWarnings("deprecation")
public void refresh() throws BeansException {
    registerSingleton(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, SessionLocaleResolver.class);
    registerSingleton(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, SessionThemeResolver.class);
    LocaleChangeInterceptor interceptor1 = new LocaleChangeInterceptor();
    LocaleChangeInterceptor interceptor2 = new LocaleChangeInterceptor();
    interceptor2.setParamName("locale2");
    ThemeChangeInterceptor interceptor3 = new ThemeChangeInterceptor();
    ThemeChangeInterceptor interceptor4 = new ThemeChangeInterceptor();
    interceptor4.setParamName("theme2");
    UserRoleAuthorizationInterceptor interceptor5 = new UserRoleAuthorizationInterceptor();
    interceptor5.setAuthorizedRoles("role1", "role2");
    List<Object> interceptors = new ArrayList<>();
    interceptors.add(interceptor5);
    interceptors.add(interceptor1);
    interceptors.add(interceptor2);
    interceptors.add(interceptor3);
    interceptors.add(interceptor4);
    interceptors.add(new MyHandlerInterceptor1());
    interceptors.add(new MyHandlerInterceptor2());
    interceptors.add(new MyWebRequestInterceptor());
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("mappings", "/view.do=viewHandler\n/locale.do=localeHandler\nloc.do=anotherLocaleHandler");
    pvs.add("interceptors", interceptors);
    registerSingleton("myUrlMapping1", SimpleUrlHandlerMapping.class, pvs);
    pvs = new MutablePropertyValues();
    pvs.add("mappings", "/form.do=localeHandler\n/unknown.do=unknownHandler\nservlet.do=myServlet");
    pvs.add("order", "2");
    registerSingleton("myUrlMapping2", SimpleUrlHandlerMapping.class, pvs);
    pvs = new MutablePropertyValues();
    pvs.add("mappings", "/head.do=headController\n" + "body.do=bodyController\n/noview*=noviewController\n/noview/simple*=noviewController");
    pvs.add("order", "1");
    registerSingleton("handlerMapping", SimpleUrlHandlerMapping.class, pvs);
    registerSingleton("myDummyAdapter", MyDummyAdapter.class);
    registerSingleton("myHandlerAdapter", MyHandlerAdapter.class);
    registerSingleton("standardHandlerAdapter", SimpleControllerHandlerAdapter.class);
    registerSingleton("noviewController", NoViewController.class);
    pvs = new MutablePropertyValues();
    pvs.add("order", 0);
    pvs.add("basename", "org.springframework.web.servlet.complexviews");
    registerSingleton("viewResolver", org.springframework.web.servlet.view.ResourceBundleViewResolver.class, pvs);
    pvs = new MutablePropertyValues();
    pvs.add("suffix", ".jsp");
    registerSingleton("viewResolver2", InternalResourceViewResolver.class, pvs);
    pvs = new MutablePropertyValues();
    pvs.add("viewName", "form");
    registerSingleton("viewHandler", ParameterizableViewController.class, pvs);
    registerSingleton("localeHandler", ComplexLocaleChecker.class);
    registerSingleton("anotherLocaleHandler", ComplexLocaleChecker.class);
    registerSingleton("unknownHandler", Object.class);
    registerSingleton("headController", HeadController.class);
    registerSingleton("bodyController", BodyController.class);
    registerSingleton("servletPostProcessor", SimpleServletPostProcessor.class);
    registerSingleton("handlerAdapter", SimpleServletHandlerAdapter.class);
    registerSingleton("myServlet", MyServlet.class);
    pvs = new MutablePropertyValues();
    pvs.add("order", "1");
    pvs.add("exceptionMappings", "java.lang.IllegalAccessException=failed2\n" + "ServletRequestBindingException=failed3");
    pvs.add("defaultErrorView", "failed0");
    registerSingleton("exceptionResolver1", SimpleMappingExceptionResolver.class, pvs);
    pvs = new MutablePropertyValues();
    pvs.add("order", "0");
    pvs.add("exceptionMappings", "java.lang.Exception=failed1");
    pvs.add("mappedHandlers", ManagedList.of(new RuntimeBeanReference("anotherLocaleHandler")));
    pvs.add("defaultStatusCode", "500");
    pvs.add("defaultErrorView", "failed2");
    registerSingleton("handlerExceptionResolver", SimpleMappingExceptionResolver.class, pvs);
    registerSingleton("multipartResolver", MockMultipartResolver.class);
    registerSingleton("testListener", TestApplicationListener.class);
    addMessage("test", Locale.ENGLISH, "test message");
    addMessage("test", Locale.CANADA, "Canadian & test message");
    super.refresh();
}
Also used : UserRoleAuthorizationInterceptor(org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor) ArrayList(java.util.ArrayList) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Aggregations

UserRoleAuthorizationInterceptor (org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor)2 LocaleChangeInterceptor (org.springframework.web.servlet.i18n.LocaleChangeInterceptor)2 ThemeChangeInterceptor (org.springframework.web.servlet.theme.ThemeChangeInterceptor)2 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)1 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)1 ConversionServiceExposingInterceptor (org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor)1 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)1 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)1