Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring using @Value annotation in Filter

I am currently working on a Spring project and I am making a new filter that checks if a valid JWT has been sent in the request.

I am running into an issue where I can't get a value from my application.yml file using the @Value annotation like so.

@Component
@Order(2)
public class JwtConfiguration implements Filter {

    @Value("${jwt.secret}")
    private String jwtSecret;

I know this works fine because I have the same thing in my unit test.

I have read somewhere that the filter is not in the application context so it will not have access to configuration and I will not be able to autowire dependencies.

Does anyone know a good technique for getting values from my application.yml to my filter?

I am also not using any XML configuration and would prefer a solution that doesn't use them.

I am using Spring Boot version 1.3.3.

like image 892
David Jones Avatar asked Jun 08 '16 08:06

David Jones


People also ask

What is the use of @value annotation in spring?

One of the most important annotations in spring is @Value annotation which is used to assign default values to variables and method arguments. We can read spring environment variables as well as system variables using @Value annotation. It also supports Spring Expression Language (SpEL).

How does @value work in Spring Boot?

The main focus of this article is to help you understand how Spring's @Value annotation works. @Value is a Java annotation that is used at the field or method/constructor parameter level and it indicates a default value for the affected argument.

How do I inject a string value from an annotation?

Usage Examples As a basic and mostly useless example, we can only inject “string value” from the annotation to the field: Using the @PropertySource annotation allows us to work with values from properties files with the @Value annotation. In the following example, we get Value got from the file assigned to the field:

How to inject literal values into configuration variables in spring?

It is generally used for injecting values into configuration variables, which we will show and explain in the following example. Step 1: First, let’s create a simple Spring Application and inject the literal values by setter injection. So, create a simple class Student having three attributes rollNo, name, and age.


1 Answers

This can be achieved by implementing ServletContextInitializer. See below sample code.

@Configuration
public class WebConfigurer implements ServletContextInitializer {

    @Value("${jwt.secret}")
    private String jwtSecret;

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        EnumSet<DispatcherType> disps = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.ASYNC);
        initFilter(servletContext, disps);
    }


    private void initFilter(ServletContext servletContext,
                                              EnumSet<DispatcherType> disps) {
        FilterRegistration.Dynamic myFilter =
            servletContext.addFilter("myFilter",
                new MyFilterClass(jwtSecret));

        // You can pass null as first parameter to below API calls
        myFilter.addMappingForUrlPatterns(disps, true, "/content/*");
        myFilter.addMappingForUrlPatterns(disps, true, "/app/*");
        myFilter.setAsyncSupported(true);
    }

}

Edit/Update:

I suppose there is another way to add filters using Java Config

You can use FilterRegistrationBean to register the filters. Here you can set the order using setOrder method. But think it will create as many ServletContextInitializer as there are filters because FilterRegistrationBean is a ServletContextInitializer See

like image 78
Sangram Jadhav Avatar answered Sep 29 '22 22:09

Sangram Jadhav