./gradlew bootRun only dependencies

tl;dr build.gradle: ... configurations { bootRunOnly } depencencies { ... bootRunOnly group: 'org.bouncycastle', name: 'bc-fips', version: '2.0.0' bootRunOnly group: 'org.bouncycastle', name: 'bcpkix-fips', version: '2.0.7' } bootRun { classpath = sourceSets.main.runtimeClasspath + configurations.bootRunOnly } Background We’ve got a project requiring FIPS compliant crypto, so, instead of having to wrangle java.secrity files and the related JARs on each project, we created a base image that contains these depencencies. However, we still need to be able to run the projects locally (not necessarially in FIPS mode), but we don’t want any conflicting JARs to end up in the final Immage. ...

November 8, 2024 · Nigel Sim

Kickstart GraphQL Spring Boot Resolution Error Misdirection

Using com.graphql-java-kickstart:graphql-spring-boot-starter:14.1.0 I was refactoring some code to switch Strings for enums and I got the following error. Caused by: graphql.kickstart.tools.SchemaError: Type 'Integration' is declared as an enum in the GraphQL schema but is not a Java enum! I double checked, and Integration was in the GraphQL correctly, and was also a proper Java enum. GraphQL: enum Integration { STRIPE } input IntegrationCriteria { integration: Integration identifier: String } Java enum: ...

September 18, 2023 · Nigel Sim

Initialise H2GIS with Spring Boot JPA

TL;DR spring: jpa: generate-ddl: true properties: javax: persistence: schema-generation: database: action: drop-and-create create-source: script-then-metadata create-script-source: init.sql init.sql CREATE ALIAS IF NOT EXISTS H2GIS_SPATIAL FOR "org.h2gis.functions.factory.H2GISFunctions.load"; CALL H2GIS_SPATIAL(); CREATE DOMAIN POINT AS GEOMETRY(POINT); CREATE DOMAIN POLYGON AS GEOMETRY(POLYGON);

August 11, 2023 · Nigel Sim

Hibernate Survival Diaries

I’ve been developing with Hibernate since approximately 2005. In that time I’ve spoken with many other experienced developers and compared notes on how they use and don’t use Hibernate. This is a brief summary of some key learnings. Many of the topics discussed here could be considered defects, and in a lot of cases there are tickets, but I haven’t compiled a list here. Some may have actually been fixed (please let me know), but as far as I know this is accurate as of Hibernate 5.4.33. ...

February 28, 2023 · Nigel Sim

CSRF for Stateless SSO APIs

TL;DR In Spring CSRF generates a new token for each new session. If you have http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) then there is a new session triggered for each request. This means we need a different mechanism to calculate the CSRF token, otherwise it’ll be rotated with every request, and you’ll end up with a race condition where your UI will have its token changed mid-request (at least in Angular 12). The solution here is to derive a hash from the SSO session. ...

June 4, 2022 · Nigel Sim

Spring Security audit events not firing

TL;DR The classes that publish the AuditEvent object are instantiated by AuditAutoConfiguration which is conditional on an AuditEventRepository. If you don’t want to store the events, e.g., in an InMemoryAuditEventRepository, then you need to either: 1) manually instantiate AuthenticationAuditListener and AuthorizationAuditListener, or 2) listen for AbstractAuthorizationEvent and build up from there. The Slightly Longer Version There are a lot of articles on the web about how you can get Spring Security audit events simply by including the spring-boot-starter-actuator artifact, and then creating a listener. ...

February 17, 2021 · Nigel Sim

Migrating from ModelMapper to Map Struct

TL;DR I’ve built many applications that have used ModelMapper for projecting from one object structure to another, and it has been very good for getting up and running quickly. However, it costs in terms of type safety, and startup performance. So, I’ve switched to a type safe, compile time alternative MapStruct. This article mainly covers the pros and cons of each framework, not how to use them in real projects, or best practises. There are plenty of other articles on the web covering these topics. ...

December 1, 2020 · Nigel Sim

Spring Boot Web Services Client with Jib

When it comes to building a SOAP web services client using Spring, the guide suggests using an Ant based gradle build task to generate the required classes. However, if you are using Jib to produce a Docker container, you may find that the generated classes are not in the container, and there are few guides as to how to get them in there. It looks something like this: ... task genJaxb { ext.sourcesDir = "${buildDir}/generated-sources/jaxb" ext.classesDir = "${buildDir}/classes/jaxb" ext.schema = "http://localhost:8080/ws/countries.wsdl" outputs.dir classesDir doLast() { project.ant { taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask", classpath: configurations.jaxb.asPath mkdir(dir: sourcesDir) mkdir(dir: classesDir) xjc(destdir: sourcesDir, schema: schema, package: "com.example.consumingwebservice.wsdl") { arg(value: "-wsdl") produces(dir: sourcesDir, includes: "**/*.java") } javac(destdir: classesDir, source: 1.8, target: 1.8, debug: true, debugLevel: "lines,vars,source", classpath: configurations.jaxb.asPath) { src(path: sourcesDir) include(name: "**/*.java") include(name: "*.java") } copy(todir: classesDir) { fileset(dir: sourcesDir, erroronmissingdir: false) { exclude(name: "**/*.java") } } } } } ... The crux of the issue is getting the generated classes onto the main source set so that Jib will package them up. I’m not a Gradle expert, so while there may be a way to make this happen with this setup, the neater solution appeared to be to us the XJC Gradle Plugin which takes care of a lot of this automatically. ...

October 18, 2020 · Nigel Sim

Supplying resources directly to Flying Saucer

In the Java world, one of the options you have for generating PDFs is Flying Saucer. It is quite a nice solution that renders HTML to, amongst other things, PDF. In its simplest form a report, receipt, etc, could just be a chunk of text. But, more realistic situations would require images to be embedded in the output, be these logos, charts, images, etc. A commonly found suggestion for achieving this is to link to the assets via the file-system. This actually occurs by default if you set the base URL of the document to null: ...

July 28, 2019 · Nigel Sim

Liquibase + Hibernate 5 + Envers + Spring Boot Naming Conventions

Starting with a project using Spring Boot 1.5.8 and Hibernate 5.2.12 I implemented Liquibase to handle database schema changes. This is all straight forward enough, with plenty of other tutorials of how to setup the runtime side. The schema diff generation side was another matter. The issues hit was that the identifier names were not correct - Spring Boot would produce underscore versions of names, e.g., my_table, while Liquibase would produce names as per the class or field name, e.g., MyTable. To get around this I could specify the actual table or column names in the @Table or @Column annotations. However, this was not going to work when we put Envers into the mix. ...

March 15, 2018 · Nigel Sim