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 8061069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:12:25+00:00 2026-06-05T10:12:25+00:00

I have static content configured like this: ContextHandler staticContext = new ContextHandler(); staticContext.setContextPath(/); staticContext.setResourceBase(.);

  • 0

I have static content configured like this:

    ContextHandler staticContext = new ContextHandler();
    staticContext.setContextPath("/");
    staticContext.setResourceBase(".");
    staticContext.setClassLoader(Thread.currentThread().getContextClassLoader());

    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setDirectoriesListed(true);
    resourceHandler.setWelcomeFiles(new String[]{"index.html"});

    resourceHandler.setResourceBase(webDir);

    staticContext.setHandler(resourceHandler);

And now I want to set Basic HTTP Auth for all my static files. How can I do this?

PS. I’m using embedded Jetty withour web.xml

  • 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-05T10:12:27+00:00Added an answer on June 5, 2026 at 10:12 am

    Override ResourceHandler#handle() with something like:

    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String authHeader = request.getHeader("Authorization");
    
        if (authHeader != null && authHeader.startsWith("Basic ")) {
            String[] up = parseBasic(authHeader.substring(authHeader.indexOf(" ") + 1));
            String username = up[0];
            String password = up[1];
            if (authenticateUser(username, password)) {
                super.handle(target, baseRequest, request, response);
                return;
            }
        }
    
        response.setHeader("WWW-Authenticate", "BASIC realm=\"SecureFiles\"");
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Please provide username and password");
    }
    
    private boolean authenticateUser(String username, String password) {
        // Perform authentication here
        return true; // .. if authentication is successful
    }
    
    private String[] parseBasic(String enc) {
        byte[] bytes = Base64.decodeBase64(enc.getBytes());
        String s = new String(bytes);
        int pos = s.indexOf( ":" );
        if( pos >= 0 )
            return new String[] { s.substring( 0, pos ), s.substring( pos + 1 ) };
        else
            return new String[] { s, null };
    }
    

    The Base64.decodeBase64 above is from Apache Commons Codec. Of course you can find a library that does Basic Auth for you, but here you can see what happens under the covers. Another approach could be to use a Basic Auth filter and install that into your context.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have our web applications pull static content (css, js, images)
I have configured my static settings like so: STATIC_ROOT = os.path.join(SITE_ROOT, 'static') STATIC_URL =
We have to develop an Ecommerce site with 20+ pages of static content which
We have a very simple ASP.NET web application comprising mostly static content and a
How can I serve some static content in liferay? I have a directory structure
I have the following code that reads the content of a url public static
I have a class like this : class User { String name; String password;
I have a small problem, I have some static content inside a div and
I have a general question about the new Django 1.3 static file framework. I
I have static method which returns me as it's name says data from domain

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.