Spring Security Java Config Preview: Readability
Join the DZone community and get the full member experience.
Join For Freeoriginal article written by rob winch

in this post, i will discuss how to make your spring security java configuration more readable. the post is intended to elaborate on a point from spring security java config preview: web security where i stated:
by formatting our java configuration code it is much easier to read. it can be read similar to the xml namespace equivalent where "and()" represents optionally closing an xml element.
indentation
the indentation of spring security's java configuration really impacts its readability. in general, indentation like a bullet list should be preferred. at a high level it looks like this:
builder #1 i modify builder i modify #1 i also modify #1 i also modify #1 and #2 i also modify builder i modify #2 i also modify #2 and #3 i also modify builder i modify #3 ...
for a more concrete example, take a look at the following code:
http // #1 .formlogin() // #2 .loginurl("/login") .failureurl("/login?error") // #3 .and() // #4 .authorizeurls() // #5 .antmatchers("/signup","/about").permitall() .antmatchers("/admin/**").hasrole("admin") .anyrequest().authenticated();
- #1 formlogin updates the http object itself. the indentation of formlogin is incremented from that of http (much like they way the <form-login> is indented from <http> )
- #2 loginurl and failureurl update the formlogin configuration. for example, loginurl determines where spring security will redirect if log in is required. for this reason, each is a child of formlogin .
- #3 and means we are done configuring the parent (in this case formlogin ). this also implies that the next line will decrease indentation by one. when looking at the configuration you can read it as http is configured with formlogin and authorizeurls . if we had nothing else to configure, the and is not necessary.
- #4 we decrease the indentation with authorizeurls since it is not related to form based log in. instead, its intent is to restrict access to various urls.
- #5 each antmatchers and anyrequest modifies the authorization requirements for authorizeurls . this is why each is a child of authorizeurls
ide formatters
the indentation may cause problems with code formatters. many ide's will allow you to disable formatting for select blocks of code with comments. for example, in sts/eclipse you can use the comments of @formatter:off and @formatter:on to turn off and on code formatting. an example is shown below:
// @formatter:off http .formlogin() .loginurl("/login") .failureurl("/login?error") .and() .authorizeurls() .antmatchers("/signup","/about").permitall() .antmatchers("/admin/**").hasrole("admin") .anyrequest().authenticated(); // @formatter:on
for this feature to work, make sure you have it enabled:
- navigate to preferences -> java -> code style -> formatter
- click the edit button
- select the off/on tags tab
- ensure enable off/on tags is selected
- you can optionally change the strings used for disabling and enabling formatting here too.
- click ok
comparison to xml namespace
our indentation also helps us relate the java configuration to the xml namespace configuration. this is not always true, but it does help. let's compare our configuration to the relevant xml configuration below.
http .formlogin() .loginpage("/login") .failureurl("/login?error") .and() .authorizeurls() .antmatchers("/signup","/about").permitall() .antmatchers("/admin/**").hasrole("admin") .anyrequest().authenticated();
the relevant, but not equivalent, xml configuration can be seen below. note that the differences between how spring security will behave between these configurations is due to the different default values between java configuration and xml configuration.
<http use-expressions="true"> <form-login login-page="/login" authentication-failure-url="/login?error" /> <!-- similar to and() --> <intercept-url pattern="/signup" access="permitall"/> <intercept-url pattern="/about" access="permitall"/> <intercept-url pattern="/**" access="hasrole('role_user')"/> </http>
- the first thing to notice is that the http and <http> are quite similar. one difference is that java configuration uses authorizeurls to specify use-expressions="true"
- formlogin and <form-login> are quite similar. each child of formlogin is an xml attribute of <form-login> . based upon our explanation of indentation, the similarities are logical since xml attributes modify xml elements.
- the and under formlogin is very similar to ending an xml element.
- each child of authorizeurls is similar to each <intercept-urls> , except that java configuration specifies requires-channel differently which helps reduce configuration in many circumstances.
you should now know how to consistently indent your spring security java configuration. by doing so your code will be more readable and be easier to translate to and from the xml configuration equivalents.
Published at DZone with permission of Pieter Humphrey, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Managing Data Residency, the Demo
-
Does the OCP Exam Still Make Sense?
-
Building a Java Payment App With Marqeta
-
[DZone Survey] Share Your Expertise for Our Database Research, 2023 Edition
Comments