Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9218147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:47:46+00:00 2026-06-18T02:47:46+00:00

I’ve written a simple REST server using JAX-RS, Jersey and Grizzly. This is how

  • 0

I’ve written a simple REST server using JAX-RS, Jersey and Grizzly. This is how I start the server:

URI baseUri = UriBuilder.fromUri("http://localhost/api")
                        .port(8081)
                        .build();

ResourceConfig rc = new PackagesResourceConfig("se.aioobe.resources");
HttpServer httpServer = GrizzlyServerFactory.createHttpServer(baseUri, rc);

Now I need to protect the resources using Basic HTTP authentication, and I can’t figure out how to do this.

I can switch from Grizzly to for instance Jetty if it is simpler to get it to work, but I really value the simple configuration / start up that Grizzly provides.

I’ve read a lot of tutorials. They all mention the web.xml but in my current configuration I don’t have one. (Do I need to add one for HTTP authentication?) I’ve found the following questions, neither of them is of any help 🙁

(No SSL required at this point. The authentication is at this point just to prevent the public from peeking at our beta.)

TL;DR: How do I add basic HTTP authentication to a Jersey / Grizzly webapp?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:47:47+00:00Added an answer on June 18, 2026 at 2:47 am

    I managed to get it working after a couple of hours, based on this blog post.

    My solution involves:

    • Maven artifacts:
      • jersey-server (v 1.17)
      • jersey-grizzly2 (v 1.17)
    • Hard coded username / password (replace with database lookup if you like)
    • No web.xml (programatically configured server)
    • No SSL involved

    I created this ContainerRequestFilter:

    public class AuthFilter implements ContainerRequestFilter {
    
        // Exception thrown if user is unauthorized.
        private final static WebApplicationException unauthorized =
           new WebApplicationException(
               Response.status(Status.UNAUTHORIZED)
                       .header(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"realm\"")
                       .entity("Page requires login.").build());
    
        @Override
        public ContainerRequest filter(ContainerRequest containerRequest) 
                throws WebApplicationException {
    
            // Automatically allow certain requests.
            String method = containerRequest.getMethod();
            String path = containerRequest.getPath(true);
            if (method.equals("GET") && path.equals("application.wadl"))
                return containerRequest;
    
            // Get the authentication passed in HTTP headers parameters
            String auth = containerRequest.getHeaderValue("authorization");
            if (auth == null)
                throw unauthorized;
    
            auth = auth.replaceFirst("[Bb]asic ", "");
            String userColonPass = Base64.base64Decode(auth);
    
            if (!userColonPass.equals("admin:toHah1ooMeor6Oht"))
                throw unauthorized;
    
            return containerRequest;
        }
    }
    

    And I then changed the startup code to include the filter:

    URI baseUri = UriBuilder.fromUri("http://localhost/api")
                            .port(8081)
                            .build();
    
    ResourceConfig rc = new PackagesResourceConfig("se.aioobe.resources");
    
    // Add AuthFilter ////////////
    rc.getProperties().put("com.sun.jersey.spi.container.ContainerRequestFilters",
                           "<YOUR PACKAGE FOR AuthFilter>.AuthFilter");
    //////////////////////////////
    
    HttpServer httpServer = GrizzlyServerFactory.createHttpServer(baseUri, rc);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am using JSon response to parse title,date content and thumbnail images and place
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.