Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Embedded Jetty 9 with Spring security Oauth2.

Hello,

While gathering inormation about what went wrong, i got Spring security Oauth2 Sparklr example working with embedded Jetty.
I did things wrong with some paths and tried to use Spring security Oauth 1.0.5.

I will try to describe the solution here.


I did some changes in the pom for the following artifacts:
Spring security Oauth2 version 1.1.0.BUILD-SNAPSHOT (The example does not work with 1.0.5 Release)
Spring framework 3.1.1 Release

The embedded server is started from a main class, creating, starting and joining this class:

package org.springframework.security.oauth.examples.sparklr;


import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jetty.deploy.DeploymentManager;
import org.eclipse.jetty.deploy.PropertiesConfigurationManager;
import org.eclipse.jetty.deploy.providers.WebAppProvider;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.*;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class StandaloneHttpServer {

    private static Logger _logger = LoggerFactory.getLogger(StandaloneHttpServer.class);
   
    private Server _server = null;


    public StandaloneHttpServer() {
        _server = new Server();

        ServerConnector http = new ServerConnector(_server);

        http.setPort(8081);

        http.setHost("localhost");

        http.setIdleTimeout(30000);
        http.setAcceptQueueSize(2);

        _server.addConnector(http);

        URL webXml = StandaloneHttpServer.class.getResource("/web.xml");
        URL resources = StandaloneHttpServer.class.getResource("/web/");

        WebAppContext webContext = new WebAppContext();

        webContext.setDescriptor(webXml.toString());
        webContext.setResourceBase(resources.toString());
        webContext.setContextPath("/");
        webContext.setParentLoaderPriority(true);
       
        ContextHandlerCollection contexts = new ContextHandlerCollection();
        contexts.addHandler(webContext);

        DeploymentManager deployer = new DeploymentManager();
        deployer.setContexts(contexts);
        deployer.setContextAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/servlet-api-[^/]*\\.jar$");
        WebAppProvider webapp_provider = new WebAppProvider();
       
        List<String> directories = new ArrayList<>();
        directories.add(resources.toString());
       
        webapp_provider.setMonitoredDirectories(directories);

        webapp_provider.setDefaultsDescriptor("");
        webapp_provider.setScanInterval(10);
        webapp_provider.setExtractWars(false);
        webapp_provider.setConfigurationManager(new PropertiesConfigurationManager());
        deployer.addAppProvider(webapp_provider);
        _server.addBean(deployer);

        ResourceHandler resHandler = new ResourceHandler();
        resHandler.setResourceBase(resources.toString());
        resHandler.setServer(_server);

        ContextHandler contextHandler = new ContextHandler();
        contextHandler.setContextPath("/");
        contextHandler.setHandler(resHandler);

        HandlerList handlers = new HandlerList();

        handlers.addHandler(contexts);

        handlers.addHandler(new DefaultHandler());
        handlers.addHandler(new RequestLogHandler());
        handlers.addHandler(resHandler);
        handlers.addHandler(contextHandler);
       
        _server.setHandler(handlers);
    }

    public void start() throws Exception {
        _logger.info("Starting http server.");

        _server.start();

        _logger.info("Server started");
    }

    public void joinServerThread(){
        try{
            _server.join();
        } catch(InterruptedException ie){
            _logger.warn("joinServer interrupted: " + ie.getMessage());
        }
    }
   
    public void shutdown(){
        try{
            _server.stop();
        } catch(Exception e){
            _logger.warn("joinServer interrupted: " + e.getMessage());
        }       
    }
}


And the web.xml is  changed so the context is loaded from classpath.
The webroot is moved to resources/web.

web.xml loading context from classpath:

<servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/spring-servlet.xml</param-value>
        </init-param>
    </servlet>


Greetings,
Peter

On 12/12/2013 04:31 AM, Greg Wilkins wrote:

Peter,

No direct experience, but I have certainly worked with oauth2 within jetty and I'm 90% it was spring.    If you can point to a concrete issue of what is not being done (eg filter not mapped), then perhaps we can assist with the jetty specifics.

cheers



 


On 9 December 2013 19:56, I-Real - Peter van Alst <p.vanalst@xxxxxxxxx> wrote:
I am trying to get the Spring Security Oath2 Sparklr2 example working with a web.xml. Jetty initialisation is done programmatically.
All files are loaded from classpath.

I can get the filter fired using a DefaultServlet, but not Oauth2 parts. Maybe i need a Spring DispatcherServlet to get it to handle the Oauth2 endpoint.
I can't get the DispatcherServlet to load in the same Spring context than the filter.

Have people here, experience with embedded Jetty 9 and Spring Security+Oauth2?

Thanks
Peter

_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jetty-users




--
Greg Wilkins <gregw@xxxxxxxxxxx>
http://www.webtide.com
Developer advice and support from the Jetty & CometD experts.
Intalio, the modern way to build business applications.


_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jetty-users


Back to the top