ResourceServerTokenServicesConfiguration disappeared in Spring Boot 2.0.0.M7 - spring-boot

I have a Spring Boot application using OAuth2 and working successfully in Spring Boot 2.0.0.M2 I switched Spring Boot version to 2.0.0.M7 and the application stop working.
After some debugging, I found that the class 'org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration' is removed from the artifact 'spring-boot-autoconfigure' in 2.0.0.M7 which was doing the following initialisation...
#Bean
#ConditionalOnMissingBean(ResourceServerTokenServices.class)
public UserInfoTokenServices userInfoTokenServices() {
UserInfoTokenServices services = new UserInfoTokenServices(
this.sso.getUserInfoUri(), this.sso.getClientId());
services.setRestTemplate(this.restTemplate);
services.setTokenType(this.sso.getTokenType());
if (this.authoritiesExtractor != null) {
services.setAuthoritiesExtractor(this.authoritiesExtractor);
}
if (this.principalExtractor != null) {
services.setPrincipalExtractor(this.principalExtractor);
}
return services;
}
Now that this class removed, no initialization occurs and 'DefaultTokenServices' initialized in the class 'org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer' and my application fails.
private ResourceServerTokenServices tokenServices(HttpSecurity http) {
if (resourceTokenServices != null) {
return resourceTokenServices;
}
DefaultTokenServices tokenServices = new DefaultTokenServices();
tokenServices.setTokenStore(tokenStore());
tokenServices.setSupportRefreshToken(true);
tokenServices.setClientDetailsService(clientDetails());
this.resourceTokenServices = tokenServices;
return tokenServices;
}
Does anybody knows why this class is removed or is there any replacement for it?
Thx for answers...

It seems auto-configure delegate these configuration to Spring Security 5 until that is ready they provide a temporary jar file fulfilling the old functionality which can be reach with the following issue...
https://github.com/spring-projects/spring-security-oauth/issues/1240

Related

ServletRegistrationBean in Spring cloud

I have recently migrated the application to Spring Cloud Gateway from zuul filter for routing to multiple microservices. I have a filter which is written in
native code (HTTPServlet). In Spring application initialization, I am registering that Servlet as below
#Bean
public ServletRegistrationBean oAuthCodeReceiverServletBean() {
ServletRegistrationBean oauthCodeReceiverServletBean = new ServletRegistrationBean(
new OAuthCodeRecieverServlet(), "/login/oauth/codereceiver");
return oauthCodeReceiverServletBean;
}
#Bean
public FilterRegistrationBean<OAuthFilter> securityRegistrationBean() {
FilterRegistrationBean<OAuthFilter> registrationBean = new FilterRegistrationBean<>(oAuthFilter);
registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
registrationBean.addUrlPatterns("/*");
return registrationBean;
}
But the Filter is not at all called. The filters that implements GlobalFilter are called. Is there a solution handle ?
Please note I dont have option to update the native code OauthFilter.

Spring boot MessageDispatcherServlet overriding DispatcherServlet. how to skip overriding and register both DispatcherServlet?

in a single application, I m trying to use soap as well as rest web service. and each servlet is given different URI, below is the code of config class.
The question is: only soap service URL is working fine but for the rest on getting 405 error.
#EnableWs
#Configuration
public class WebServiceConfig {
#Bean
public ServletRegistrationBean RsRegistrationBean(ApplicationContext applicationContext) {
DispatcherServlet servlet = new DispatcherServlet();
servlet.setApplicationContext(applicationContext);
return new ServletRegistrationBean(servlet,"/rest/*");
}
#Bean
public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(
ApplicationContext context) {
MessageDispatcherServlet messageDispatcherServlet = new MessageDispatcherServlet();
messageDispatcherServlet.setApplicationContext(context);
messageDispatcherServlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean<>(messageDispatcherServlet,"/");
}
}
Remove your WebServiceConfig class, as both are auto-configured by Spring Boot already (as of Spring Boot 1.4). Add the following to your application.properties
spring.mvc.servlet.path=/rest
spring.webservices.path=/
Now you leverage the Spring Boot proivded infrastructure instead of fighting with it.

EmbeddedServletContainerCustomizer not working in Spring Boot 2 for configuring session timeout

I have a Spring Boot Application where I am configuring the container's session timeout in programmatic way using EmbeddedServletContainerCustomizer. It was working fine for Spring Boot version 1.5.4.RELEASE. Following is the code.
#Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return (container -> {
container.setSessionTimeout(24, TimeUnit.HOURS); // session timeout value
});
}
Now I am trying to upgrade the Spring Boot version to 2.0.1.RELEASE. And the above code is not working. Can anybody help on this.
After some search I found the answer. Posting the same, as this can help others: The code need to change ;
#Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> sessionManagerCustomizer() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
return server -> server.addContextCustomizers(context -> context.setSessionTimeout(24 * 60));
}

How to integrate Spring Boot and Resteay

Although there are a lot of code example on the internet of integrating Spring Boot/Spring and Resteasy, but most of them are out of data or even don't work.
I am look at the latest Resteasy document, try to create a instance of SpringBeanProcessorServletAware in my config bean.
#Bean
public ServletListenerRegistrationBean<ResteasyBootstrap> resteasyBootstrapRegistratio() {
ServletListenerRegistrationBean<ResteasyBootstrap> registration = new ServletListenerRegistrationBean<>();
registration.setListener(new ResteasyBootstrap());
return registration;
}
#Bean
public ServletRegistrationBean resteasyServletRegistratio() {
ServletRegistrationBean registration = new ServletRegistrationBean();
registration.setServlet(new HttpServletDispatcher());
registration.addUrlMappings("/*");
return registration;
}
#Bean
public SpringBeanProcessorServletAware springBeanProcessorServletAware() {
SpringBeanProcessorServletAware springBeanProcessor = new SpringBeanProcessorServletAware();
return springBeanProcessor;
}
But it will throw Nullpoint exception. It seems like the servletContext is required to make SpringBeanProcessorServletAware work.
Then I try to inject servletContext. But the bean SpringBeanProcessorServletAware is being created before ServletContextInitializer finished.
How to make some bean created after ServletContextInitializer is finished?
Am I wrong in do the integration between Spring Boot and Resteasy.

Spring-boot: Configure FreeMarkerConfigurationFactoryBean in web application

I'm trying to use Freemarker for e-mail templating in a web application.
I have declared a FreeMarkerConfigurationFactoryBean as follow:
#Bean
public FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean(EmailTemplateService templateService) {
FreeMarkerConfigurationFactoryBean configurationFactoryBean = new FreeMarkerConfigurationFactoryBean();
configurationFactoryBean.setPreTemplateLoaders(templateService);
return configurationFactoryBean;
}
When running my JUnit everything is working well, but when running in my webapp my bean is "overriden" by the spring boot FreeMarkerAutoConfiguration.
I have tried to:
remove the spring-boot-starter-freemarker from my gradle file
#EnableAutoConfiguration(exclude = {FreeMarkerAutoConfigurationk.class})
spring.freemarker.enabled=false
But without success. Any idea?
Thanks guys.
As your application is a Web application, it's Spring Boot's FreeMarkerWebConfiguration that you're interested in. It doesn't use a FreeMarkerConfigurationFactoryBean but a FreeMarkerConfigurer. You should create a FreeMarkerConfigurer bean and configure it as required. For example:
#Bean
public FreeMarkerConfigurer freeMarkerConfigurer(EmailTemplateService templateService) {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setPreTemplateLoaders(templateService);
return configurer;
}

Resources