本文整理汇总了Java中org.springframework.web.servlet.HandlerInterceptor类的典型用法代码示例。如果您正苦于以下问题:Java HandlerInterceptor类的具体用法?Java HandlerInterceptor怎么用?Java HandlerInterceptor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HandlerInterceptor类属于org.springframework.web.servlet包,在下文中一共展示了HandlerInterceptor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: restAuthenticationThrottle
import org.springframework.web.servlet.HandlerInterceptor; //导入依赖的package包/类
@ConditionalOnMissingBean(name = "restAuthenticationThrottle")
@Bean
public HandlerInterceptor restAuthenticationThrottle() {
final String throttler = casProperties.getRest().getThrottler();
if (StringUtils.isNotBlank(throttler) && this.applicationContext.containsBean(throttler)) {
return this.applicationContext.getBean(throttler, HandlerInterceptor.class);
}
return new HandlerInterceptorAdapter() {
@Override
public boolean preHandle(final HttpServletRequest request,
final HttpServletResponse response,
final Object handler) {
return true;
}
};
}
示例2: registerTenantInterceptorWithIgnorePathPattern
import org.springframework.web.servlet.HandlerInterceptor; //导入依赖的package包/类
/**
* Registered interceptor to all request except passed urls.
* @param registry helps with configuring a list of mapped interceptors.
* @param interceptor the interceptor
*/
protected void registerTenantInterceptorWithIgnorePathPattern(
InterceptorRegistry registry, HandlerInterceptor interceptor) {
InterceptorRegistration tenantInterceptorRegistration = registry.addInterceptor(interceptor);
tenantInterceptorRegistration.addPathPatterns("/**");
List<String> tenantIgnorePathPatterns = getTenantIgnorePathPatterns();
Objects.requireNonNull(tenantIgnorePathPatterns, "tenantIgnorePathPatterns can't be null");
for (String pattern : tenantIgnorePathPatterns) {
tenantInterceptorRegistration.excludePathPatterns(pattern);
}
LOGGER.info("Added handler interceptor '{}' to all urls, exclude {}", interceptor.getClass()
.getSimpleName(), tenantIgnorePathPatterns);
}
示例3: resourceHandlerMapping
import org.springframework.web.servlet.HandlerInterceptor; //导入依赖的package包/类
/**
* Return a handler mapping ordered at Integer.MAX_VALUE-1 with mapped
* resource handlers. To configure resource handling, override
* {@link #addResourceHandlers}.
*/
@Bean
public HandlerMapping resourceHandlerMapping() {
ResourceHandlerRegistry registry = new ResourceHandlerRegistry(this.applicationContext, this.servletContext);
addResourceHandlers(registry);
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
if (handlerMapping != null) {
handlerMapping.setPathMatcher(mvcPathMatcher());
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
handlerMapping.setInterceptors(new HandlerInterceptor[] {
new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider())});
handlerMapping.setCorsConfigurations(getCorsConfigurations());
}
else {
handlerMapping = new EmptyHandlerMapping();
}
return handlerMapping;
}
示例4: mappedInterceptors
import org.springframework.web.servlet.HandlerInterceptor; //导入依赖的package包/类
@Test
public void mappedInterceptors() throws Exception {
String path = "/foo";
HandlerInterceptor interceptor = new HandlerInterceptorAdapter() {};
MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] {path}, interceptor);
TestRequestMappingInfoHandlerMapping hm = new TestRequestMappingInfoHandlerMapping();
hm.registerHandler(new TestController());
hm.setInterceptors(new Object[] { mappedInterceptor });
hm.setApplicationContext(new StaticWebApplicationContext());
HandlerExecutionChain chain = hm.getHandler(new MockHttpServletRequest("GET", path));
assertNotNull(chain);
assertNotNull(chain.getInterceptors());
assertSame(interceptor, chain.getInterceptors()[0]);
chain = hm.getHandler(new MockHttpServletRequest("GET", "/invalid"));
assertNull(chain);
}
示例5: getInterceptorsForPath
import org.springframework.web.servlet.HandlerInterceptor; //导入依赖的package包/类
private List<HandlerInterceptor> getInterceptorsForPath(String lookupPath) {
PathMatcher pathMatcher = new AntPathMatcher();
List<HandlerInterceptor> result = new ArrayList<HandlerInterceptor>();
for (Object interceptor : this.registry.getInterceptors()) {
if (interceptor instanceof MappedInterceptor) {
MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor;
if (mappedInterceptor.matches(lookupPath, pathMatcher)) {
result.add(mappedInterceptor.getInterceptor());
}
}
else if (interceptor instanceof HandlerInterceptor) {
result.add((HandlerInterceptor) interceptor);
}
else {
fail("Unexpected interceptor type: " + interceptor.getClass().getName());
}
}
return result;
}
示例6: printHandler
import org.springframework.web.servlet.HandlerInterceptor; //导入依赖的package包/类
/**
* Print the handler.
*/
protected void printHandler(Object handler, HandlerInterceptor[] interceptors) throws Exception {
if (handler == null) {
this.printer.printValue("Type", null);
}
else {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
this.printer.printValue("Type", handlerMethod.getBeanType().getName());
this.printer.printValue("Method", handlerMethod);
}
else {
this.printer.printValue("Type", handler.getClass().getName());
}
}
}
示例7: sitePreferenceHandlerInterceptorRegistered
import org.springframework.web.servlet.HandlerInterceptor; //导入依赖的package包/类
@Test
public void sitePreferenceHandlerInterceptorRegistered() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
SitePreferenceAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
RequestMappingHandlerMapping mapping = this.context
.getBean(RequestMappingHandlerMapping.class);
HandlerInterceptor[] interceptors = mapping
.getHandler(new MockHttpServletRequest()).getInterceptors();
assertThat(interceptors)
.hasAtLeastOneElementOfType(SitePreferenceHandlerInterceptor.class);
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:17,代码来源:SitePreferenceAutoConfigurationTests.java
示例8: sitePreferenceHandlerInterceptorRegistered
import org.springframework.web.servlet.HandlerInterceptor; //导入依赖的package包/类
@Test
public void sitePreferenceHandlerInterceptorRegistered() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
SitePreferenceAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
RequestMappingHandlerMapping mapping = this.context
.getBean(RequestMappingHandlerMapping.class);
HandlerInterceptor[] interceptors = mapping
.getHandler(new MockHttpServletRequest()).getInterceptors();
assertThat(interceptors,
hasItemInArray(instanceOf(SitePreferenceHandlerInterceptor.class)));
}
示例9: deviceResolverHandlerInterceptorRegistered
import org.springframework.web.servlet.HandlerInterceptor; //导入依赖的package包/类
@Test
public void deviceResolverHandlerInterceptorRegistered() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
DeviceResolverAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
RequestMappingHandlerMapping mapping = this.context
.getBean(RequestMappingHandlerMapping.class);
HandlerInterceptor[] interceptors = mapping
.getHandler(new MockHttpServletRequest()).getInterceptors();
assertThat(interceptors,
hasItemInArray(instanceOf(DeviceResolverHandlerInterceptor.class)));
}
示例10: addInterceptors
import org.springframework.web.servlet.HandlerInterceptor; //导入依赖的package包/类
@Override
public void addInterceptors(InterceptorRegistry registry) {
/*Optional.ofNullable(interceptorList).ifPresent(list->{
list.stream().forEach(inter->registry.addInterceptor(inter));
});*/
if(LangUtils.isEmpty(interceptorList)){
return ;
}
for(HandlerInterceptor inter : interceptorList){
InterceptorRegistration reg = registry.addInterceptor(inter);
if(inter instanceof WebInterceptorAdapter){
WebInterceptorAdapter webinter = (WebInterceptorAdapter) inter;
if(LangUtils.isEmpty(webinter.getPathPatterns())){
continue;
}
reg.addPathPatterns(webinter.getPathPatterns());
}
}
// registry.addInterceptor(new BootFirstInterceptor());
}
示例11: getHandlerInterceptors
import org.springframework.web.servlet.HandlerInterceptor; //导入依赖的package包/类
@SuppressWarnings({ "unchecked", "rawtypes" })
private List<HandlerInterceptor> getHandlerInterceptors(Class<? extends Object> clazz,
Interceptors interceptorAnnotation) {
List<HandlerInterceptor> interceptors = new ArrayList<HandlerInterceptor>();
if (interceptorAnnotation != null) {
Class[] interceptorClasses = interceptorAnnotation.value();
if (interceptorClasses != null) {
for (Class interceptorClass : interceptorClasses) {
if (!HandlerInterceptor.class.isAssignableFrom(interceptorClass)) {
raiseIllegalInterceptorValue(clazz, interceptorClass);
}
interceptors.add((HandlerInterceptor) getApplicationContext().getBean(interceptorClass));
}
}
}
return interceptors;
}
示例12: printHandler
import org.springframework.web.servlet.HandlerInterceptor; //导入依赖的package包/类
/** Print the handler */
protected void printHandler(Object handler, HandlerInterceptor[] interceptors) throws Exception {
if (handler == null) {
this.printer.printValue("Type", null);
}
else {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
this.printer.printValue("Type", handlerMethod.getBeanType().getName());
this.printer.printValue("Method", handlerMethod);
}
else {
this.printer.printValue("Type", handler.getClass().getName());
}
}
}
示例13: getInterceptorsForPath
import org.springframework.web.servlet.HandlerInterceptor; //导入依赖的package包/类
private List<HandlerInterceptor> getInterceptorsForPath(String lookupPath) {
PathMatcher pathMatcher = new AntPathMatcher();
List<HandlerInterceptor> result = new ArrayList<HandlerInterceptor>();
for (Object i : registry.getInterceptors()) {
if (i instanceof MappedInterceptor) {
MappedInterceptor mappedInterceptor = (MappedInterceptor) i;
if (mappedInterceptor.matches(lookupPath, pathMatcher)) {
result.add(mappedInterceptor.getInterceptor());
}
}
else if (i instanceof HandlerInterceptor){
result.add((HandlerInterceptor) i);
}
else {
fail("Unexpected interceptor type: " + i.getClass().getName());
}
}
return result;
}
示例14: testShouldMergeInterceptors
import org.springframework.web.servlet.HandlerInterceptor; //导入依赖的package包/类
public void testShouldMergeInterceptors() throws Throwable {
HandlerInterceptor interceptorOfFramework = new HandlerInterceptorSub();
HandlerInterceptor interceptorOfTab = new HandlerInterceptorSub();
HandlerInterceptor[] interceptorsOfFramework = new HandlerInterceptor[] {interceptorOfFramework};
HandlerInterceptor[] interceptorsOfTab = new HandlerInterceptor[] {interceptorOfTab};
Mock proceedingJoinPoint = mock(ProceedingJoinPoint.class);
proceedingJoinPoint.expects(once()).method("proceed").will(
returnValue(new HandlerExecutionChain(null, interceptorsOfTab)));
InterceptorInjector injector = new InterceptorInjector();
injector.setInterceptors(interceptorsOfFramework);
HandlerExecutionChain handlers =
injector.mergeInterceptorsToTabs((ProceedingJoinPoint) proceedingJoinPoint.proxy());
assertEquals(2, handlers.getInterceptors().length);
assertSame(interceptorOfFramework, handlers.getInterceptors()[0]);
assertSame(interceptorOfTab, handlers.getInterceptors()[1]);
}
示例15: testShouldJustReturnInterceptorsOfFrameworkIfNoTabInterceptors
import org.springframework.web.servlet.HandlerInterceptor; //导入依赖的package包/类
public void testShouldJustReturnInterceptorsOfFrameworkIfNoTabInterceptors() throws Throwable {
HandlerInterceptor interceptorOfFramework = new HandlerInterceptorSub();
HandlerInterceptor[] interceptorsOfFramework = new HandlerInterceptor[] {interceptorOfFramework};
Mock proceedingJoinPoint = mock(ProceedingJoinPoint.class);
proceedingJoinPoint.expects(once()).method("proceed").will(
returnValue(new HandlerExecutionChain(null, null)));
InterceptorInjector injector = new InterceptorInjector();
injector.setInterceptors(interceptorsOfFramework);
HandlerExecutionChain handlers =
injector.mergeInterceptorsToTabs((ProceedingJoinPoint) proceedingJoinPoint.proxy());
assertEquals(1, handlers.getInterceptors().length);
assertSame(interceptorOfFramework, handlers.getInterceptors()[0]);
}