<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bartosz Balukiewicz</title>
    <description>The latest articles on DEV Community by Bartosz Balukiewicz (@spooz).</description>
    <link>https://dev.to/spooz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F28095%2F1ca7fd35-89c1-4bb4-9b5f-c7877fd94ac6.jpeg</url>
      <title>DEV Community: Bartosz Balukiewicz</title>
      <link>https://dev.to/spooz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/spooz"/>
    <language>en</language>
    <item>
      <title>Spring Security and threads</title>
      <dc:creator>Bartosz Balukiewicz</dc:creator>
      <pubDate>Sun, 30 Jul 2017 12:12:56 +0000</pubDate>
      <link>https://dev.to/spooz/spring-security-and-threads</link>
      <guid>https://dev.to/spooz/spring-security-and-threads</guid>
      <description>

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;When using Spring Security to secure our applications, we must be aware of its inner workings. The foundation is &lt;a href="https://docs.spring.io/spring-security/site/docs/current/apidocs/org/springframework/security/core/context/SecurityContext.html"&gt;SecurityContext&lt;/a&gt; which holds data produced by the authentication process and needed for proper authorization. By definition it's thread-bound - &lt;a href="https://docs.oracle.com/javase/7/docs/api/java/lang/ThreadLocal.html"&gt;ThreadLocal&lt;/a&gt; is used as a holder, created during the security filter process of a request. &lt;a href="https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#technical-overview"&gt;(read more)&lt;/a&gt; &lt;br&gt;
The thread-bound solution is convenient, but there is one drawback - security context is not propagated to child threads by default. Luckly, Spring provides tools to deal with this problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  Delegating the context
&lt;/h3&gt;

&lt;p&gt;As stated in the &lt;a href="https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#concurrency"&gt;documentation&lt;/a&gt;, we are given &lt;a href="http://docs.spring.io/spring-security/site/docs/current/apidocs/org/springframework/security/concurrent/DelegatingSecurityContextRunnable.html"&gt;DelegatingSecurityContextRunnable&lt;/a&gt;  and &lt;a href="http://docs.spring.io/spring-security/site/docs/current/apidocs/org/springframework/security/concurrent/DelegatingSecurityContextExecutor.html"&gt;DelegatingSecurityContextExecutor&lt;/a&gt;. &lt;br&gt;
The first class is a low level wrapper for our &lt;a href="https://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html"&gt;Runnable&lt;/a&gt; instances, implemented using the &lt;a href="https://en.wikipedia.org/wiki/Delegation_pattern"&gt;delegation pattern&lt;/a&gt; . It simply takes given context and sets its during the execution of the &lt;strong&gt;run()&lt;/strong&gt; method. The usage is as simple as:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;SecurityContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SecurityContextHolder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getContext&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;DelegatingSecurityContextRunnable&lt;/span&gt; &lt;span class="n"&gt;wrappedRunnable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;DelegatingSecurityContextRunnable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;originalRunnable&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;DelegatingSecurityContextExecutor&lt;/strong&gt; is a more high-level abstraction. It delegates &lt;a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executor.html"&gt;Executor&lt;/a&gt;  instances instead of &lt;strong&gt;Runnables&lt;/strong&gt;, enabling the management of pools of threads aware of Spring's security context. &lt;/p&gt;

&lt;p&gt;In modern Java we would most likely use it with &lt;a href="https://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html"&gt;stream parallel API&lt;/a&gt; or &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html"&gt;CompletableFutures&lt;/a&gt;. Both of these abstractions by default use Java 8 default &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html#commonPool--"&gt;ForkJoinPool.commonPool&lt;/a&gt;, which is fine, but commonly we create custom pools dedicated to specific tasks. While &lt;strong&gt;ForkJoinPool&lt;/strong&gt; is designed to handle work-stealing divide and conquer algorithms, we can use good old &lt;a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html#newFixedThreadPool(int)"&gt;FixedThreadPool&lt;/a&gt; as well. &lt;a href="https://zeroturnaround.com/rebellabs/fixedthreadpool-cachedthreadpool-or-forkjoinpool-picking-correct-java-executors-for-background-tasks/"&gt;(read more)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following example shows the creation of custom &lt;strong&gt;FixedThreadPool&lt;/strong&gt; with &lt;strong&gt;DelegatingSecurityContextExecutor&lt;/strong&gt; and creating new &lt;strong&gt;CompletableFuture&lt;/strong&gt; task:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;SecurityContext&lt;/span&gt; &lt;span class="n"&gt;securityContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SecurityContextHolder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getContext&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;Executor&lt;/span&gt; &lt;span class="n"&gt;delegatedExecutor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Executors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newFixedThreadPool&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Executor&lt;/span&gt; &lt;span class="n"&gt;delegatingExecutor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;DelegatingSecurityContextExecutor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delegatedExecutor&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;securityContext&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;CompletableFuture&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;supplyAsync&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;veryHardTask&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;&lt;span class="n"&gt;delegatingExecutor&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  @Async methods
&lt;/h3&gt;

&lt;p&gt;Above example shows delegating security context with plain Java concurrent methods. When using Spring we often use &lt;a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html"&gt;@Async&lt;/a&gt; annotation to make our methods run asynchronously. It uses very own &lt;a href="http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/task/SimpleAsyncTaskExecutor.html"&gt;SimpleAsyncTaskExecutor&lt;/a&gt; with its own thread pool. In order to pass our context we could create another wrapping delegation. However, Spring Security again &lt;a href="https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#securitycontextholder-securitycontext-and-authentication-objects"&gt;gives us a convenient way to deal with the problem&lt;/a&gt;:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;SecurityContextHolder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setStrategyName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SecurityContextHolder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MODE_INHERITABLETHREADLOCAL&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This property can be configured with:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Bean&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;MethodInvokingFactoryBean&lt;/span&gt; &lt;span class="nf"&gt;methodInvokingFactoryBean&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;MethodInvokingFactoryBean&lt;/span&gt; &lt;span class="n"&gt;methodInvokingFactoryBean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;MethodInvokingFactoryBean&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;methodInvokingFactoryBean&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setTargetClass&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SecurityContextHolder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;methodInvokingFactoryBean&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setTargetMethod&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"setStrategyName"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;methodInvokingFactoryBean&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setArguments&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]{&lt;/span&gt;&lt;span class="n"&gt;SecurityContextHolder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MODE_INHERITABLETHREADLOCAL&lt;/span&gt;&lt;span class="o"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;methodInvokingFactoryBean&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It forces Spring to wrap its async executor with Security delegate &lt;a href="http://docs.spring.io/autorepo/docs/spring-security/4.0.0.M1/apidocs/org/springframework/security/task/DelegatingSecurityContextTaskExecutor.html"&gt;DelegatingSecurityContextTaskExecutor&lt;/a&gt;. Simple as that, we are safe to use @Async methods without worring about security context.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrap-up
&lt;/h3&gt;

&lt;p&gt;Spring Security by definition is thread-bound, but by default is not ready to be used in multithreading environment.  However, with simple steps we are able to deal fix the problem.&lt;/p&gt;


</description>
      <category>java</category>
      <category>spring</category>
      <category>springsecurity</category>
      <category>threads</category>
    </item>
  </channel>
</rss>
