Michael Schwartz
Michael Schwartz

Reputation: 1

Spring boot logging and security seem to not work when configuring a Web Service boot project dependent on a DAL project

I am new to Spring Boot and somewhat new to Java / Maven as well, so I am pretty sure I have some basic misconfiguration here in terms of using a DAL project and a Web Service project, but it is difficult for me to figure out.

I can attach .POM files or add more code if my explanation here is insufficient - I am hoping perhaps there is something obviously wrong that a Spring Boot advanced user could help out with. Even suggestions for turning on more logging so I could see why Spring Boot thinks it has to log to stderr in this case would be great.

My basic set up is - I have 2 Spring Boot projects:

  1. DAL project. This uses Postgres / JPA to set up my database and allows a routing component of our project to internally use the DAL. This sets up all of the repositories for the DAL as not being externally exposed - for instance:

    @RepositoryRestResource(collectionResourceRel = "user", path = "user", exported=false) public interface UserRepository extends PagingAndSortingRepository<User, Long> { }

  2. A Web Service project that will take care of any business logic and expose end points to our end users. This logic is not needed by our routing component. This also exposes REST endpoints for instance like:

    @RestController @RequestMapping(value = "/auditentries") @Api(value = "Audit Entries", description = "Audit Entries API") public class AuditEntryController extends BaseController<AuditEntry, AuditEntryService> { }

When I run the Web Service as a Spring Boot app, the endpoints are exposed correctly and I can use a REST client to get to /auditentries or other external endpoints that I have configured. The DAL is also basically working as it sets up tables using Hibernate and the internal PagingAndSortingRepository classes used by the Web Service. So - this is great - the main use case for the project setup seems to work without issue.

However, I have noticed 2 problems so far - and perhaps I am missing something but I can't even figure out how to debug them or to tell why Spring Boot is getting confused in these cases:

  1. If I configure logging for the Web Service project, the spring boot logging from the Web Service project is immediately directed to stderr in red text, while the logging from the DAL project is appropriately directed to the log file. Manual logging statements I have inserted using logger.info in the Web Service also correctly go to the log file.

By default Spring Boot logs to the console, so logging such as that at the end of this post always goes to the console and stderr even though a log file is configured.

  1. If I attempt to enable security in the Web Service project, Spring Boot is enabling security on all the endpoints, but it does not recognize my custom class which is supposed to define my security:

    @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ }

I believe both of these issues are related to the inclusion of the DAL project in the POM file from the Web Service project, because if I simplify the project back down to essentially a spring boot project that no longer relies on DAL (or does anything else interesting anymore), my security configuration class is called, and the logging appears to work as expected.

One potential problem currently is both POM files for DAL and Web Service include most of the dependencies explicitly for things like spring-boot-starter-data-rest, postgres, jpa, etc. Perhaps the Web Service should just be relying on the DAL's POM dependency inclusions entirely, but I don't see offhand why this would be causing errors even if it is not best practice to include dependencies in both projects explicitly.

Is there an invalid assumption I am making or some spring configuration I should be turning off in the DAL in order to have both projects work together? Does Spring Boot potentially not like that both projects have REST endpoints but only 1 set of endpoints is exposed? How do I even debug this issue without resorting to trial and error / brute force?

Here is some sample logging from the Web Service project that is always going to the console even though logging is configured:

[main] INFO CNER.WS.App - Starting App on mschwartz-w550s with PID 5072 (C:\dev\event_router\server\WebServices\WS\target\classes started by mschwartz in C:\dev\event_router\server\WebServices\WS) [main] INFO CNER.WS.App - The following profiles are active: default [main] INFO org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext - Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@10feca44: startup date [Fri Mar 25 05:24:02 UTC 2016]; root of context hierarchy [main] INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]] [main] INFO org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type [class org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration$$EnhancerBySpringCGLIB$$8c5fa87b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) [main] INFO org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$82f8180d] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) [main] INFO org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer - Tomcat initialized with port(s): 8187 (http) [main] INFO org.apache.catalina.core.StandardService - Starting service Tomcat

Here is some example logging from the DAL project that is correctly going to the log file - you can see it is related to DAL-specific implementation and Hibernate.

INFO 2016-03-25 00:24:00,985 [main] CNER.WS.App:main(54): Starting Server ... INFO 2016-03-25 05:24:01,955 [background-preinit] org.hibernate.validator.internal.util.Version:(17): HV000001: Hibernate Validator 5.2.2.Final INFO 2016-03-25 05:24:06,669 [localhost-startStop-1] org.hibernate.jpa.internal.util.LogHelper:logPersistenceUnitInformation(46): HHH000204: Processing PersistenceUnitInfo [ name: default ...]

Upvotes: 0

Views: 629

Answers (2)

Michael Schwartz
Michael Schwartz

Reputation: 1

Resolved with .pom file changes for logging, and adding following to ComponentScan to pick up the implementation of WebSecurityConfigurerAdapter

@ComponentScan(basePackages= {"repo", "wstest.controllers", "wstest.services", "wstest"})

Upvotes: 0

duffymo
duffymo

Reputation: 308928

Your data access layer sounds like a JAR you'd like to publish to Maven and subsequently let your web layer pull down using its pom.

It should be obvious that the dependencies between the two must match.

I'd wonder why you don't write Spring Boot REST data services and combine the two. Is that what you're doing?

Upvotes: 0

Related Questions