Spring MVC Annotation Interceptor

A annotation generic based java web interceptor for Spring MVC.

License

License

Categories

Categories

Spring MVC User Interface Web Frameworks
GroupId

GroupId

me.mervinz
ArtifactId

ArtifactId

spring-mvc-annotation-interceptor
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Spring MVC Annotation Interceptor
A annotation generic based java web interceptor for Spring MVC.
Project URL

Project URL

https://github.com/mofei2816/spring-mvc-annotation-interceptor
Source Code Management

Source Code Management

https://github.com/mofei2816/spring-mvc-annotation-interceptor.git

Download spring-mvc-annotation-interceptor

How to add to project

<!-- https://jarcasting.com/artifacts/me.mervinz/spring-mvc-annotation-interceptor/ -->
<dependency>
    <groupId>me.mervinz</groupId>
    <artifactId>spring-mvc-annotation-interceptor</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/me.mervinz/spring-mvc-annotation-interceptor/
implementation 'me.mervinz:spring-mvc-annotation-interceptor:1.0.0'
// https://jarcasting.com/artifacts/me.mervinz/spring-mvc-annotation-interceptor/
implementation ("me.mervinz:spring-mvc-annotation-interceptor:1.0.0")
'me.mervinz:spring-mvc-annotation-interceptor:jar:1.0.0'
<dependency org="me.mervinz" name="spring-mvc-annotation-interceptor" rev="1.0.0">
  <artifact name="spring-mvc-annotation-interceptor" type="jar" />
</dependency>
@Grapes(
@Grab(group='me.mervinz', module='spring-mvc-annotation-interceptor', version='1.0.0')
)
libraryDependencies += "me.mervinz" % "spring-mvc-annotation-interceptor" % "1.0.0"
[me.mervinz/spring-mvc-annotation-interceptor "1.0.0"]

Dependencies

provided (10)

Group / Artifact Type Version
javax.servlet : javax.servlet-api jar 3.1.0
aopalliance : aopalliance jar 1.0
commons-logging : commons-logging jar 1.2
org.springframework : spring-core jar 4.1.6.RELEASE
org.springframework : spring-aop jar 4.1.6.RELEASE
org.springframework : spring-beans jar 4.1.6.RELEASE
org.springframework : spring-context jar 4.1.6.RELEASE
org.springframework : spring-expression jar 4.1.6.RELEASE
org.springframework : spring-web jar 4.1.6.RELEASE
org.springframework : spring-webmvc jar 4.1.6.RELEASE

Project Modules

There are no modules declared in this project.

annotation-interceptor

Java Maven Build Release License MIT

Annotation generic based java web interceptor for Spring MVC.

Installation

Download the latest JAR or using Maven:

<dependency>
    <groupId>me.mervinz</groupId>
    <artifactId>spring-mvc-annotation-interceptor</artifactId>
    <version>1.0.0</version>
</dependency>

or Gradle:

compile group: 'me.mervinz', name: 'spring-mvc-annotation-interceptor', version: '1.0.0'

Usage

  1. Create a java annotation which you want the interceptor to handle. For example RequireToken.
package me.mervinz.demo.annotation;

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Inherited
public @interface RequireToken {
}
  1. Create your own interceptor class like TokenInterceptorAdapter extend by abstract class me.mervinz.springmvc.interceptor.AnnotationInterceptorAdapter to intercept the annotation RequireToken.
  2. Override the method preAnnotationHandler and postAnnotationHandler.

Sample:

package me.mervinz.demo.interceptor;

import me.mervinz.demo.annotation.RequireLogin;
import me.mervinz.springmvc.interceptor.AnnotationInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public final class TokenInterceptorAdapter extends AnnotationInterceptorAdapter<RequireToken> {

    // This is the replacement for original method 'preHandler'
    @Override
    public boolean preAnnotationHandler(HttpServletRequest request, HttpServletResponse response, Object handler,
                                        RequireLogin annotation) throws Exception {
        // put your code here ...
        return false;
    }
    
    // This is the replacement for original method 'postHandler'
    @Override
    public void postAnnotationHandler(HttpServletRequest request, HttpServletResponse response,
                                        Object handler, ModelAndView modelAndView, T annotation) throws Exception {
                                        
        // put your code here ...
    }
}
  1. Register the interceptor to you spring mvc configuration.

For JavaConfig:

package me.mervinz.demo.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import me.mervinz.demo.interceptor.TokenInterceptorAdapter;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "me.mervinz.demo")
public class AppConfig extends WebMvcConfigurerAdapter {

    /**
        * Add interceptors
        */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    
        // Register your interceptor
        registry.addInterceptor(new TokenInterceptorAdapter());
        
        // Or
        registry.addInterceptor(new AnnotationInterceptorAdapter<RequireToken>() {
        
            @Override
            public boolean preAnnotationHandler(HttpServletRequest request, HttpServletResponse response, Object handler, RequireLogin annotation) throws Exception {
            
                // Put your code here ...
                return false;
            }
            
            @Override
            public void postAnnotationHandler(HttpServletRequest request, HttpServletResponse response,
                                                Object handler, ModelAndView modelAndView, T annotation) throws Exception {
                                                
                // Put your code here ...
            }
        });
        
        // Registry other interceptors
    }
    
}

For XML:

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <context:component-scan base-package="me.mervinz.demo"/>
    
    <mvc:interceptors>
        <mvc:interceptor>
            <bean class="me.mervinz.demo.interceptor.TokenInterceptorAdapter"/>
        </mvc:interceptor>
    </mvc:interceptors>
    
</beans>

Dependencies

For detail see pom.xml.

Contributing

  1. Fork it.
  2. Create your feature branch. ($ git checkout feature/my-feature-branch)
  3. Commit your changes. ($ git commit -am 'What feature I just added.')
  4. Push to the branch. ($ git push origin feature/my-feature-branch)
  5. Create a new Pull Request

Authors

@Mervin

License

The MIT License (MIT). For detail see LICENSE.

Versions

Version
1.0.0