How to add scanBasePackages in SpringBootServletInitializer & SpringApplicationBuilder? - spring

Following is used for my spring project
#SpringBootApplication(scanBasePackages = "com.tv")
public class WWWAbacusApplication {
public static void main(String[] args) {
SpringApplication.run(WWWAbacusApplication.class, args);
}
}
I need to deploy it in widlfly so i do it like below
public class ServletInitializer extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WWWAbacusApplication.class);
}
}
but i never required scanbasepackages but in this application i required it.
Can anyone suggest how to do it in SpringBootServletInitializer

Related

How many ways we can pass the parameters in spring boot main method?

#SpringBootApplication
public class NewUserDetailesApplication {
public static void main(String[] args) {
SpringApplication.run(NewUserDetailesApplication.class, args);
}
}

How does springboot set the timeout exit?

Such as setting 1 minute no operation, the system will automatically exit.
This is my setup, but it doesn't seem to work.
server.session.timeout=60
public class BigYouApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(BigYouApplication.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(BigYouApplication.class);
}
}

How to exclude a URL when usinf PCF SSO service with EnableOAuth2Sso annotation?

I am using Angular and Spring Boot to build a Single Page app with Rest API. Here is my configuration:
#SpringBootApplication
#EnableOAuth2Sso
public class AppConfig extends SpringBootServletInitializer implements ApplicationContextAware {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(AppConfig.class);
}
public static void main(String[] args) {
ApplicationContext appContext = SpringApplication.run(AppConfig.class);
context = appContext;
}
#Configuration
protected static class SecurityConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/healthcheck", "/").permitAll()
.antMatchers("/api/**").authenticated()
.anyRequest().authenticated();
}
}
}
The SSO service I am using is provided by Pivotal Cloud Foundry[PCF]. Everything was fine before I included
SecurityConfig
class. As soon as the app is loaded, user is redirected to the SSO login page and then redirected back to the app. But I need to exclude the "healthcheck" URL from authentication. That is why I included the SecurityConfig class. But now the SSO Authentication is not working at all. I could only reach /healthcheck.
I followed this example https://spring.io/guides/tutorials/spring-boot-oauth2/
Can someone please let me know what is wrong with my code?
Thanks.
I figured it out. I had to move my EnableOAuth2Sso to the WebSecurityConfigurerAdapter. Like this:
#SpringBootApplication
public class AppConfig extends SpringBootServletInitializer implements ApplicationContextAware {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(AppConfig.class);
}
public static void main(String[] args) {
ApplicationContext appContext = SpringApplication.run(AppConfig.class);
context = appContext;
}
#Configuration
#EnableOAuth2Sso
protected static class SecurityConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/healthcheck", "/").permitAll()
.antMatchers("/api/**").authenticated()
.anyRequest().authenticated();
}
}
}

Where is SpringBootServletInitializer's DispatcherServlet?

How SpringBootServletInitializer determines RootConfig.class, WebConfig.class, and maps DispatcherSevlet?
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
return application.sources(Application.class); - loads the Application.class. That's your main configuration, where you can declare #Beans. You can add more #Configuration classes by putting them in the same folder, for example, and they will be "component-scanned".
If you declare a #Configuration class that extends WebMvcConfigurerAdapter, you have an access to the web configuration like resource handlers, argument resolvers, etc.
#Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/public-resources/")
.setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
}
#Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new FooBarHandlerMethodArgumentResolver());
}
}
By default the dispatcher servlet is configured to the root path "/"
If you need more details, see the auto configuration.

spring boot is ignoring web.xml

I try to make spring boot to load stuff from web.xml and so far it is ignoring definitions from there...
I have bellow starter:
#Configuration
#EnableAutoConfiguration
#ComponentScan
#SpringBootApplication
#ImportResource(value = {"classpath:/*-beans.xml"})
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
I see in logs that spring beans are initialized but no definitions are loaded from web.xml...

Resources