2

In my Jersey 2 based rest application, I have added spring security filter chain to check authentication. Everything works well except during unit-test if I try to add that security chain I get 500 error (and the code pointer never goes to service resource methods). According to this ticket, adding filter chain in JerseyTest should be fixed by now.

But I am yet to find a way to do it properly. My JerseySpringTest class look like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:META-INF/applicationContext-test.xml", "classpath:META-INF/security-context.xml"})
@ActiveProfiles("unit-test")
@WebAppConfiguration
public abstract class JerseySpringTest {

private JerseyTest _jerseyTest;

public final WebTarget target(final String path) {
    return _jerseyTest.target(path);
}

@Before
public void setupJerseyTest() throws Exception {
    _jerseyTest.setUp();
}

@After
public void tearDownJerseyTest() throws Exception {
    _jerseyTest.tearDown();
}

@Autowired
public void setApplicationContext(final ApplicationContext context) {
    _jerseyTest = new JerseyTest() {
        @Override
        protected Application configure() {
            ResourceConfig application = JerseySpringTest.this.configure();
            application.register(CustormDelegatingFilterProxy.class);
            application.property("springSecurityFilterChain", "/services/*");
            application.property("contextConfig", context);
            return application;
        }

    };
}

protected abstract ResourceConfig configure();
}

Any help will be appreciated.

sowrov
  • 1,018
  • 10
  • 16
  • Those `property` calls are not the same as web.xml configuration properties, and the `register` method does not register servlet filters. If you want to configure a servlet environment, then you need to configure Jersey test as a servlet container. See example in [this post](http://stackoverflow.com/a/28620966/2587435) – Paul Samsotha May 17 '16 at 09:54
  • @peeskillet That answer only deal with the things that I have done so far (look at the update 1), couldn't find anything about adding a spring filter chain. – sowrov May 17 '16 at 10:22
  • The first example overrides the `getTestContainerFactory` and the `confgureDeployment` to set up the servlet environment. You have not done this. You can add a filter in the `configureDeplyment` method. And in case you are wondering the `ResourceRegister` is the OPs `ResourceConfig`. Everything you can configure in a web.xml, you can configure in the `configureDeployment` method. Most things can be chain called – Paul Samsotha May 17 '16 at 10:23
  • Yes, that is because I am using default container factory. If you don't use `GrizzlyWebTestContainerFactory` then you don't need to override `configureDeployment`. Basically I don't have problem with normal Jersey test - I only have problem when i am trying to add `springSecurityFilterChain` – sowrov May 17 '16 at 10:29
  • If you don't use it, then you can't set up a servlet container environment. Which you need – Paul Samsotha May 17 '16 at 10:29
  • The point is that you are trying to configure things with Jersey that need to be configured at the web application level. Jersey has no part in stuff. So your configurations _inside_ jersey have no effect. – Paul Samsotha May 17 '16 at 10:42
  • 1
    You can try something [like this](https://gist.github.com/psamsotha/d99d32b652e20685051c827e00f7b88e) – Paul Samsotha May 17 '16 at 11:19

0 Answers0