java.adoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. [[jc]]
  2. = Java Configuration
  3. General support for https://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-java[Java Configuration] was added to Spring Framework in Spring 3.1.
  4. Since Spring Security 3.2 there has been Spring Security Java Configuration support which enables users to easily configure Spring Security without the use of any XML.
  5. If you are familiar with the xref:servlet/configuration/xml-namespace.adoc#ns-config[Security Namespace Configuration] then you should find quite a few similarities between it and the Security Java Configuration support.
  6. NOTE: Spring Security provides https://github.com/spring-projects/spring-security-samples/tree/main/servlet/java-configuration[lots of sample applications] which demonstrate the use of Spring Security Java Configuration.
  7. == Hello Web Security Java Configuration
  8. The first step is to create our Spring Security Java Configuration.
  9. The configuration creates a Servlet Filter known as the `springSecurityFilterChain` which is responsible for all the security (protecting the application URLs, validating submitted username and passwords, redirecting to the log in form, etc) within your application.
  10. You can find the most basic example of a Spring Security Java Configuration below:
  11. [[jc-hello-wsca]]
  12. [source,java]
  13. ----
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.context.annotation.*;
  16. import org.springframework.security.config.annotation.authentication.builders.*;
  17. import org.springframework.security.config.annotation.web.configuration.*;
  18. @EnableWebSecurity
  19. public class WebSecurityConfig {
  20. @Bean
  21. public UserDetailsService userDetailsService() {
  22. InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
  23. manager.createUser(User.withDefaultPasswordEncoder().username("user").password("password").roles("USER").build());
  24. return manager;
  25. }
  26. }
  27. ----
  28. There really isn't much to this configuration, but it does a lot.
  29. You can find a summary of the features below:
  30. * Require authentication to every URL in your application
  31. * Generate a login form for you
  32. * Allow the user with the *Username* _user_ and the *Password* _password_ to authenticate with form based authentication
  33. * Allow the user to logout
  34. * https://en.wikipedia.org/wiki/Cross-site_request_forgery[CSRF attack] prevention
  35. * https://en.wikipedia.org/wiki/Session_fixation[Session Fixation] protection
  36. * Security Header integration
  37. ** https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security[HTTP Strict Transport Security] for secure requests
  38. ** https://msdn.microsoft.com/en-us/library/ie/gg622941(v=vs.85).aspx[X-Content-Type-Options] integration
  39. ** Cache Control (can be overridden later by your application to allow caching of your static resources)
  40. ** https://msdn.microsoft.com/en-us/library/dd565647(v=vs.85).aspx[X-XSS-Protection] integration
  41. ** X-Frame-Options integration to help prevent https://en.wikipedia.org/wiki/Clickjacking[Clickjacking]
  42. * Integrate with the following Servlet API methods
  43. ** https://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getRemoteUser()[HttpServletRequest#getRemoteUser()]
  44. ** https://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getUserPrincipal()[HttpServletRequest#getUserPrincipal()]
  45. ** https://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#isUserInRole(java.lang.String)[HttpServletRequest#isUserInRole(java.lang.String)]
  46. ** https://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#login(java.lang.String,%20java.lang.String)[HttpServletRequest#login(java.lang.String, java.lang.String)]
  47. ** https://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#logout()[HttpServletRequest#logout()]
  48. === AbstractSecurityWebApplicationInitializer
  49. The next step is to register the `springSecurityFilterChain` with the war.
  50. This can be done in Java Configuration with https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-container-config[Spring's WebApplicationInitializer support] in a Servlet 3.0+ environment.
  51. Not suprisingly, Spring Security provides a base class `AbstractSecurityWebApplicationInitializer` that will ensure the `springSecurityFilterChain` gets registered for you.
  52. The way in which we use `AbstractSecurityWebApplicationInitializer` differs depending on if we are already using Spring or if Spring Security is the only Spring component in our application.
  53. * <<abstractsecuritywebapplicationinitializer-without-existing-spring>> - Use these instructions if you are not using Spring already
  54. * <<abstractsecuritywebapplicationinitializer-with-spring-mvc>> - Use these instructions if you are already using Spring
  55. [[abstractsecuritywebapplicationinitializer-without-existing-spring]]
  56. === AbstractSecurityWebApplicationInitializer without Existing Spring
  57. If you are not using Spring or Spring MVC, you will need to pass in the `WebSecurityConfig` into the superclass to ensure the configuration is picked up.
  58. You can find an example below:
  59. [source,java]
  60. ----
  61. import org.springframework.security.web.context.*;
  62. public class SecurityWebApplicationInitializer
  63. extends AbstractSecurityWebApplicationInitializer {
  64. public SecurityWebApplicationInitializer() {
  65. super(WebSecurityConfig.class);
  66. }
  67. }
  68. ----
  69. The `SecurityWebApplicationInitializer` will do the following things:
  70. * Automatically register the springSecurityFilterChain Filter for every URL in your application
  71. * Add a ContextLoaderListener that loads the <<jc-hello-wsca,WebSecurityConfig>>.
  72. [[abstractsecuritywebapplicationinitializer-with-spring-mvc]]
  73. === AbstractSecurityWebApplicationInitializer with Spring MVC
  74. If we were using Spring elsewhere in our application we probably already had a `WebApplicationInitializer` that is loading our Spring Configuration.
  75. If we use the previous configuration we would get an error.
  76. Instead, we should register Spring Security with the existing `ApplicationContext`.
  77. For example, if we were using Spring MVC our `SecurityWebApplicationInitializer` would look something like the following:
  78. [source,java]
  79. ----
  80. import org.springframework.security.web.context.*;
  81. public class SecurityWebApplicationInitializer
  82. extends AbstractSecurityWebApplicationInitializer {
  83. }
  84. ----
  85. This would simply only register the springSecurityFilterChain Filter for every URL in your application.
  86. After that we would ensure that `WebSecurityConfig` was loaded in our existing ApplicationInitializer.
  87. For example, if we were using Spring MVC it would be added in the `getRootConfigClasses()`
  88. [[message-web-application-inititializer-java]]
  89. [source,java]
  90. ----
  91. public class MvcWebApplicationInitializer extends
  92. AbstractAnnotationConfigDispatcherServletInitializer {
  93. @Override
  94. protected Class<?>[] getRootConfigClasses() {
  95. return new Class[] { WebSecurityConfig.class };
  96. }
  97. // ... other overrides ...
  98. }
  99. ----
  100. [[jc-httpsecurity]]
  101. == HttpSecurity
  102. Thus far our <<jc-hello-wsca,WebSecurityConfig>> only contains information about how to authenticate our users.
  103. How does Spring Security know that we want to require all users to be authenticated?
  104. How does Spring Security know we want to support form based authentication?
  105. Actually, there is a bean that is being invoked behind the scenes called `SecurityFilterChain`.
  106. It is configured with the following default implementation:
  107. [source,java]
  108. ----
  109. @Bean
  110. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  111. http
  112. .authorizeRequests(authorize -> authorize
  113. .anyRequest().authenticated()
  114. )
  115. .formLogin(withDefaults())
  116. .httpBasic(withDefaults());
  117. return http.build();
  118. }
  119. ----
  120. The default configuration above:
  121. * Ensures that any request to our application requires the user to be authenticated
  122. * Allows users to authenticate with form based login
  123. * Allows users to authenticate with HTTP Basic authentication
  124. You will notice that this configuration is quite similar the XML Namespace configuration:
  125. [source,xml]
  126. ----
  127. <http>
  128. <intercept-url pattern="/**" access="authenticated"/>
  129. <form-login />
  130. <http-basic />
  131. </http>
  132. ----
  133. == Multiple HttpSecurity
  134. We can configure multiple HttpSecurity instances just as we can have multiple `<http>` blocks.
  135. The key is to register multiple `SecurityFilterChain` `@Bean`s.
  136. For example, the following is an example of having a different configuration for URL's that start with `/api/`.
  137. [source,java]
  138. ----
  139. @EnableWebSecurity
  140. public class MultiHttpSecurityConfig {
  141. @Bean <1>
  142. public UserDetailsService userDetailsService() throws Exception {
  143. // ensure the passwords are encoded properly
  144. UserBuilder users = User.withDefaultPasswordEncoder();
  145. InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
  146. manager.createUser(users.username("user").password("password").roles("USER").build());
  147. manager.createUser(users.username("admin").password("password").roles("USER","ADMIN").build());
  148. return manager;
  149. }
  150. @Bean
  151. @Order(1) <2>
  152. public SecurityFilterChain apiFilterChain(HttpSecurity http) throws Exception {
  153. http
  154. .antMatcher("/api/**") <3>
  155. .authorizeHttpRequests(authorize -> authorize
  156. .anyRequest().hasRole("ADMIN")
  157. )
  158. .httpBasic(withDefaults());
  159. return http.build();
  160. }
  161. @Bean <4>
  162. public SecurityFilterChain formLoginFilterChain(HttpSecurity http) throws Exception {
  163. http
  164. .authorizeHttpRequests(authorize -> authorize
  165. .anyRequest().authenticated()
  166. )
  167. .formLogin(withDefaults());
  168. return http.build();
  169. }
  170. }
  171. ----
  172. <1> Configure Authentication as normal
  173. <2> Register an instance of `SecurityFilterChain` that contains `@Order` to specify which `SecurityFilterChain` should be considered first.
  174. <3> The `http.antMatcher` states that this `HttpSecurity` will only be applicable to URLs that start with `/api/`
  175. <4> Register another instance of `SecurityFilterChain`.
  176. If the URL does not start with `/api/` this configuration will be used.
  177. This configuration is considered after `apiFilterChain` since it has an `@Order` value after `1` (no `@Order` defaults to last).
  178. [[jc-custom-dsls]]
  179. == Custom DSLs
  180. You can provide your own custom DSLs in Spring Security.
  181. For example, you might have something that looks like this:
  182. [source,java]
  183. ----
  184. public class MyCustomDsl extends AbstractHttpConfigurer<MyCustomDsl, HttpSecurity> {
  185. private boolean flag;
  186. @Override
  187. public void init(HttpSecurity http) throws Exception {
  188. // any method that adds another configurer
  189. // must be done in the init method
  190. http.csrf().disable();
  191. }
  192. @Override
  193. public void configure(HttpSecurity http) throws Exception {
  194. ApplicationContext context = http.getSharedObject(ApplicationContext.class);
  195. // here we lookup from the ApplicationContext. You can also just create a new instance.
  196. MyFilter myFilter = context.getBean(MyFilter.class);
  197. myFilter.setFlag(flag);
  198. http.addFilterBefore(myFilter, UsernamePasswordAuthenticationFilter.class);
  199. }
  200. public MyCustomDsl flag(boolean value) {
  201. this.flag = value;
  202. return this;
  203. }
  204. public static MyCustomDsl customDsl() {
  205. return new MyCustomDsl();
  206. }
  207. }
  208. ----
  209. NOTE: This is actually how methods like `HttpSecurity.authorizeRequests()` are implemented.
  210. The custom DSL can then be used like this:
  211. [source,java]
  212. ----
  213. @EnableWebSecurity
  214. public class Config {
  215. @Bean
  216. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  217. http
  218. .apply(customDsl())
  219. .flag(true)
  220. .and()
  221. ...;
  222. return http.build();
  223. }
  224. }
  225. ----
  226. The code is invoked in the following order:
  227. * Code in `Config`s configure method is invoked
  228. * Code in `MyCustomDsl`s init method is invoked
  229. * Code in `MyCustomDsl`s configure method is invoked
  230. If you want, you can add `MyCustomDsl` to `HttpSecurity` by default by using `SpringFactories`.
  231. For example, you would create a resource on the classpath named `META-INF/spring.factories` with the following contents:
  232. .META-INF/spring.factories
  233. ----
  234. org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer = sample.MyCustomDsl
  235. ----
  236. Users wishing to disable the default can do so explicitly.
  237. [source,java]
  238. ----
  239. @EnableWebSecurity
  240. public class Config {
  241. @Bean
  242. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  243. http
  244. .apply(customDsl()).disable()
  245. ...;
  246. return http.build();
  247. }
  248. }
  249. ----
  250. [[post-processing-configured-objects]]
  251. == Post Processing Configured Objects
  252. Spring Security's Java Configuration does not expose every property of every object that it configures.
  253. This simplifies the configuration for a majority of users.
  254. Afterall, if every property was exposed, users could use standard bean configuration.
  255. While there are good reasons to not directly expose every property, users may still need more advanced configuration options.
  256. To address this Spring Security introduces the concept of an `ObjectPostProcessor` which can be used to modify or replace many of the Object instances created by the Java Configuration.
  257. For example, if you wanted to configure the `filterSecurityPublishAuthorizationSuccess` property on `FilterSecurityInterceptor` you could use the following:
  258. [source,java]
  259. ----
  260. @Bean
  261. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  262. http
  263. .authorizeRequests(authorize -> authorize
  264. .anyRequest().authenticated()
  265. .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
  266. public <O extends FilterSecurityInterceptor> O postProcess(
  267. O fsi) {
  268. fsi.setPublishAuthorizationSuccess(true);
  269. return fsi;
  270. }
  271. })
  272. );
  273. return http.build();
  274. }
  275. ----