<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Authentication on locrian code</title>
    <link>http://blog.locrian.uk/tags/authentication/</link>
    <description>Recent content in Authentication on locrian code</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Mon, 15 Aug 2016 17:46:53 +0100</lastBuildDate>
    <atom:link href="http://blog.locrian.uk/tags/authentication/index.xml" rel="self" type="application/rss+xml" />
    
    <item>
      <title>Using Jetty form-based authentication with Dropwizard</title>
      <link>http://blog.locrian.uk/post/jetty-form-auth-dropwizard/</link>
      <pubDate>Mon, 15 Aug 2016 17:46:53 +0100</pubDate>
      
      <guid>http://blog.locrian.uk/post/jetty-form-auth-dropwizard/</guid>
      <description>&lt;p&gt;&lt;a href=&#34;http://www.dropwizard.io/&#34;&gt;Dropwizard&lt;/a&gt; is a rather nice Java REST framework. Although mainly designed
for web services, it can also be used for MVC-style web applications, with dropwizard-views
which supports both FreeMarker and Mustache templates on the front end.&lt;/p&gt;

&lt;p&gt;What&amp;rsquo;s missing out of the box is form-based authentication. The embedded Jetty web server does contain this functionality &amp;mdash; but there are a number of steps involved in getting it wired up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First, create /login and /logout pages&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let&amp;rsquo;s create a simple Jersey resource for each page. We&amp;rsquo;ll also create a &lt;code&gt;/login/error&lt;/code&gt; page to
handle failed logins.&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;&lt;span style=&#34;color: #AA22FF&#34;&gt;@Path&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;/login&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt;
&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color: #0000FF; font-weight: bold&#34;&gt;LoginResource&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;

	&lt;span style=&#34;color: #AA22FF&#34;&gt;@GET&lt;/span&gt;
	&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; View &lt;span style=&#34;color: #0000FF&#34;&gt;login&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;()&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;
		&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; LoginView&lt;span style=&#34;color: #666666&#34;&gt;();&lt;/span&gt;
	&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
	
	&lt;span style=&#34;color: #AA22FF&#34;&gt;@GET&lt;/span&gt; &lt;span style=&#34;color: #AA22FF&#34;&gt;@Path&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;error&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt;
	&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; String &lt;span style=&#34;color: #0000FF&#34;&gt;error&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;()&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;
		&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;Error logging in.&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;;&lt;/span&gt;
	&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;

&lt;span style=&#34;color: #AA22FF&#34;&gt;@Path&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;/logout&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt;
&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color: #0000FF; font-weight: bold&#34;&gt;LogoutResource&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;

	&lt;span style=&#34;color: #AA22FF&#34;&gt;@GET&lt;/span&gt;
	&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; String &lt;span style=&#34;color: #0000FF&#34;&gt;logout&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #AA22FF&#34;&gt;@Context&lt;/span&gt; HttpServletRequest req&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;
		
		req&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;getSession&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;().&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;invalidate&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;();&lt;/span&gt;
		&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;You have been logged out.&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;;&lt;/span&gt;
	&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The login page needs to post to the special &lt;code&gt;/j_security_check&lt;/code&gt; endpoint, so your login
view should look something like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;&amp;lt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;html&lt;/span&gt;&amp;gt;
&amp;lt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;head&lt;/span&gt;&amp;gt;&amp;lt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;title&lt;/span&gt;&amp;gt;Log in&amp;lt;/&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;title&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;head&lt;/span&gt;&amp;gt;
&amp;lt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;body&lt;/span&gt;&amp;gt;

&amp;lt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;form&lt;/span&gt; &lt;span style=&#34;color: #7D9029&#34;&gt;method&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;#39;POST&amp;#39;&lt;/span&gt; &lt;span style=&#34;color: #7D9029&#34;&gt;action&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;#39;/j_security_check&amp;#39;&lt;/span&gt;&amp;gt;
  &amp;lt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;input&lt;/span&gt; &lt;span style=&#34;color: #7D9029&#34;&gt;type&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;#39;text&amp;#39;&lt;/span&gt; &lt;span style=&#34;color: #7D9029&#34;&gt;name&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;#39;j_username&amp;#39;&lt;/span&gt;/&amp;gt;
  &amp;lt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;input&lt;/span&gt; &lt;span style=&#34;color: #7D9029&#34;&gt;type&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;#39;password&amp;#39;&lt;/span&gt; &lt;span style=&#34;color: #7D9029&#34;&gt;name&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;#39;j_password&amp;#39;&lt;/span&gt;/&amp;gt;
  &amp;lt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;input&lt;/span&gt; &lt;span style=&#34;color: #7D9029&#34;&gt;type&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;#39;submit&amp;#39;&lt;/span&gt; &lt;span style=&#34;color: #7D9029&#34;&gt;value&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;#39;Login&amp;#39;&lt;/span&gt;/&amp;gt;
&amp;lt;/&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;form&lt;/span&gt;&amp;gt;

&amp;lt;/&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;body&lt;/span&gt;&amp;gt;
&amp;lt;/&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;html&lt;/span&gt;&amp;gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Automatically redirect 403 Forbidden responses to the login page&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;HTTP 403 responses are represented by the JAX-RS class &lt;code&gt;ForbiddenException&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To handle these, we need an ExceptionMapper that will store the target URI in session and
then redirect the user to the login page. Once they have logged in, Jetty will retrieve the URI
and forward the user to their original destination.&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color: #0000FF; font-weight: bold&#34;&gt;ForbiddenExceptionMapper&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;implements&lt;/span&gt; ExceptionMapper&lt;span style=&#34;color: #666666&#34;&gt;&amp;lt;&lt;/span&gt;ForbiddenException&lt;span style=&#34;color: #666666&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;
	
	&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;private&lt;/span&gt; UriInfo ui&lt;span style=&#34;color: #666666&#34;&gt;;&lt;/span&gt;
	&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;private&lt;/span&gt; HttpServletRequest req&lt;span style=&#34;color: #666666&#34;&gt;;&lt;/span&gt;
	
	&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color: #0000FF&#34;&gt;ForbiddenExceptionMapper&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #AA22FF&#34;&gt;@Context&lt;/span&gt; UriInfo ui&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; &lt;span style=&#34;color: #AA22FF&#34;&gt;@Context&lt;/span&gt; HttpServletRequest req&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;
		&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;this&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;ui&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; ui&lt;span style=&#34;color: #666666&#34;&gt;;&lt;/span&gt;
		&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;this&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;req&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; req&lt;span style=&#34;color: #666666&#34;&gt;;&lt;/span&gt;
	&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;

	&lt;span style=&#34;color: #AA22FF&#34;&gt;@Override&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; Response &lt;span style=&#34;color: #0000FF&#34;&gt;toResponse&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;ForbiddenException e&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;

		String location &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; ui&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;getPath&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;();&lt;/span&gt;
		
		&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;if&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;location &lt;span style=&#34;color: #666666&#34;&gt;!=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;null&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;
			req&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;getSession&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;().&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;setAttribute&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;FormAuthenticator&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;__J_URI&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; location&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;
		&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
		&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;return&lt;/span&gt; Response&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;temporaryRedirect&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;URI&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;create&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;/login&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;)).&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;build&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;();&lt;/span&gt;
	&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
	
&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Create a new server factory&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To work, the auth service needs to be initialised at the correct point in Jetty&amp;rsquo;s lifecycle;
after the server has been created but before the app servlet is added. We&amp;rsquo;ll need a custom
&lt;code&gt;ServerFactory&lt;/code&gt; so we can hook into Jetty at the right time.&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;&lt;span style=&#34;color: #AA22FF&#34;&gt;@JsonTypeName&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;jettyauthserver&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt;
&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color: #0000FF; font-weight: bold&#34;&gt;JettyAuthServerFactory&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;extends&lt;/span&gt; DefaultServerFactory &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;

    &lt;span style=&#34;color: #AA22FF&#34;&gt;@Override&lt;/span&gt;
    &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;protected&lt;/span&gt; Handler &lt;span style=&#34;color: #0000FF&#34;&gt;createAppServlet&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;Server server&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; JerseyEnvironment jersey&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt;
        ObjectMapper objectMapper&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; Validator validator&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; MutableServletContextHandler handler&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt;
        Servlet jerseyContainer&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; MetricRegistry metricRegistry&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;

        setupJettyAuth&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;handler&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;

        &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;super&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;createAppServlet&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;server&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; jersey&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; objectMapper&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; validator&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; handler&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt;
            jerseyContainer&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; metricRegistry&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;
    &lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;

    &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;private&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color: #B00040&#34;&gt;void&lt;/span&gt; &lt;span style=&#34;color: #0000FF&#34;&gt;setupJettyAuth&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;MutableServletContextHandler context&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;
    	
    	context&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;setSessionHandler&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; SessionHandler&lt;span style=&#34;color: #666666&#34;&gt;());&lt;/span&gt;
    	
    	Constraint constraint &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; Constraint&lt;span style=&#34;color: #666666&#34;&gt;();&lt;/span&gt;
    	constraint&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;setName&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;Constraint&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;__FORM_AUTH&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;
    	constraint&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;setRoles&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; String&lt;span style=&#34;color: #666666&#34;&gt;[]{&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;user&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;admin&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;moderator&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;});&lt;/span&gt;
    	constraint&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;setAuthenticate&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;true&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;

    	ConstraintSecurityHandler securityHandler &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; ConstraintSecurityHandler&lt;span style=&#34;color: #666666&#34;&gt;();&lt;/span&gt;

    	HashLoginService loginService &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; HashLoginService&lt;span style=&#34;color: #666666&#34;&gt;();&lt;/span&gt;
    	loginService&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;putUser&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;defaultuser&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; Password&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;defaultpass&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;),&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; String&lt;span style=&#34;color: #666666&#34;&gt;[]&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;admin&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;});&lt;/span&gt;
    	
    	FormAuthenticator authenticator &lt;span style=&#34;color: #666666&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; FormAuthenticator&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;/login&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; &lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;/login/error&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;,&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;false&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;
    	securityHandler&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;setAuthenticator&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;authenticator&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;

    	context&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;setSecurityHandler&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;securityHandler&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;
    &lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;

&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note: this example authenticates using a fixed, in-memory list of roles and user names.
For a real application, you&amp;rsquo;ll want to actually use a database.&lt;/p&gt;

&lt;p&gt;To do this, replace the &lt;code&gt;HashLoginService&lt;/code&gt; with a &lt;code&gt;JDBCLoginService&lt;/code&gt; (or your own login
service implementation).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Register the server factory&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a text file called &lt;code&gt;io.dropwizard.server.ServerFactory&lt;/code&gt; and save it in
&lt;code&gt;src/main/java/resources/META-INF/services/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The file needs to contain the fully qualified name of the new server factory. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;com.example.JettyAuthServerFactory
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally, register the server name in your application YAML config:&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;server:
  type:
    jettyauthserver
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Application setup&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To hook everything up, these lines need to go in the &lt;code&gt;run()&lt;/code&gt; method of your Dropwizard application.&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;&lt;span style=&#34;color: #408080; font-style: italic&#34;&gt;// Enable the Jersey security annotations on resources &lt;/span&gt;
environment&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;jersey&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;().&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;getResourceConfig&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;().&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;register&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;RolesAllowedDynamicFeature&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;class&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;

&lt;span style=&#34;color: #408080; font-style: italic&#34;&gt;// Register custom exception mapper to redirect 403 errors to the login page&lt;/span&gt;
environment&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;jersey&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;().&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;register&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;ForbiddenExceptionMapper&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;class&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;);&lt;/span&gt;

&lt;span style=&#34;color: #408080; font-style: italic&#34;&gt;// Register the Login and Logout resources&lt;/span&gt;
environment&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;jersey&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;().&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;register&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; LoginResource&lt;span style=&#34;color: #666666&#34;&gt;());&lt;/span&gt;
environment&lt;span style=&#34;color: #666666&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;jersey&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;().&lt;/span&gt;&lt;span style=&#34;color: #7D9029&#34;&gt;register&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;new&lt;/span&gt; LogoutResource&lt;span style=&#34;color: #666666&#34;&gt;());&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Try it out&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With all the pieces in place, you can now use Jersey&amp;rsquo;s &lt;a href=&#34;https://jersey.java.net/documentation/latest/security.html#annotation-based-security&#34;&gt;declarative security annotations&lt;/a&gt; to protect resources:&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #f8f8f8&#34;&gt;&lt;pre style=&#34;line-height: 125%&#34;&gt;&lt;span style=&#34;color: #AA22FF&#34;&gt;@Path&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;/topsecret&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt;
&lt;span style=&#34;color: #AA22FF&#34;&gt;@RolesAllowed&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color: #BA2121&#34;&gt;&amp;quot;admin&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #666666&#34;&gt;)&lt;/span&gt;
&lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color: #008000; font-weight: bold&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color: #0000FF; font-weight: bold&#34;&gt;TopSecretResource&lt;/span&gt; &lt;span style=&#34;color: #666666&#34;&gt;{&lt;/span&gt;

    &lt;span style=&#34;color: #408080; font-style: italic&#34;&gt;/* ... */&lt;/span&gt;

&lt;span style=&#34;color: #666666&#34;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
</description>
    </item>
    
  </channel>
</rss>