Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring disable @EnableResourceServer

I have resource server, when it's starts - it's sending request to Authentication server ("http://localhost:xxxx/auth/oauth/token_key"), and it's okay when all up and running.

But when I testing my services I do not need this at all. How can I disable resource server or maybe I should mock something so it won't be dependent on auth server(for future security tests for controllers)?

My spring boot main:

@SpringBootApplication
@EnableEurekaClient
@EnableResourceServer
public class CalendarApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(CalendarApplication.class, args);
    }
}

application.yml

security:
  basic:
    enabled: false
  oauth2:
    resource:
      jwt:
        keyUri: http://localhost:xxxx/auth/oauth/token_key

Test class annotations:

@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(value = TypeController.class, secure = false)
public class TypeControllerTest {}
like image 661
Evgenii Avatar asked Aug 02 '17 15:08

Evgenii


2 Answers

Why don't you create a separate @Configuration for your @AuthenticationServer with a separate profile (@Profile("test"))? That way, you don't need to disable security and can have an in-memory Token. That's how I dealt with it. You can also disable Spring Security for your tests completely. Have a look at this question.

like image 100
thomi Avatar answered Sep 20 '22 21:09

thomi


You can use @WithMockUser for tests

Testing Method Security

like image 21
Vazgen Torosyan Avatar answered Sep 24 '22 21:09

Vazgen Torosyan