Misch Misch - 6 months ago 72
Java Question

How to integrate Spring OAuth2 Server into Liferay

I'm trying to integrate a Spring OAuth2 Server into Liferay. Despite some problems, the basic way to do this seems to be clear:


  1. Use a
    DispatcherServlet
    to map a URL to a servlet

  2. Configure this servlet with a
    <context:component-scan>
    tag to find and initialize the Spring OAuth2 Server

  3. Implement the
    AuthorizationServerConfigurer
    and add the
    @EnableAuthorizationServer
    tag.



However, this doesn't seem to be enough, the OAuth server is never initialized and calls to the URL where the server should be listening are simply answered with an error 404.

To locate the Problem, I (almost) successfully added a little REST Service. Here is the code I used:

Step 1: web.xml



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

<servlet-mapping>
<servlet-name>springwebservice</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>


Step 2: springwebservice-servlet.xml



<context:component-scan base-package="mypackage" />


Step 3: MyRestController.java



@RestController
public class MyRestController {

@RequestMapping("/test")
public String testRest() {
return "Hello World!";
}
}


Strangely, this doesn't work. It seems to be ignoring the
@RestController
tag and just handling it as if it were a normal
@Controller
, thus searching a view called "Hello World!" instead of just returing that text:

WARN
[PageNotFound:1114] No mapping found for HTTP request with URI
[/TestLiferaySpring-1.0-SNAPSHOT/rest/Hello World!]
in DispatcherServlet with name 'springwebservice'


But independent of that, it does show that steps 1 and 2 shouldn't be the problem. However, my OAuth2 server configuration is never called at all! Why could this be the case, what am I missing?

Here is the code of the OAuth2 server configuration:

@EnableAuthorizationServer
public class OAuth2Config implements AuthorizationServerConfigurer {

@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
System.out.println("configure1");
}

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
System.out.println("configure2");
}

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
System.out.println("configure3");
}

}

Answer

What a stupid mistake (and a stupid question) I merely forgot the @Configuration annotation above the @EnableAuthorizationServer annotation.

Now at least the Authorization server is recognized:

Mapped "{[/oauth/authorize],methods=[POST], ... onto ...

I could delete this question, but maybe it will help somebody else who wants to achieve something similar (I at least wasn't able to find much documentation for either Liferay or Spring Security). Just be aware that I still get an exception about Autowiring, so this isn't the final solution to get everything to work.

Comments