如何将Spring OAuth2 Server集成到Liferay中

时间:2015-01-19 09:14:27

标签: java spring servlets spring-security liferay

我正在尝试将Spring OAuth2服务器集成到Liferay中。尽管存在一些问题,但基本的方法似乎很明确:

  1. 使用DispatcherServlet将URL映射到servlet
  2. 使用<context:component-scan>标记配置此servlet以查找并初始化Spring OAuth2服务器
  3. 实施AuthorizationServerConfigurer并添加@EnableAuthorizationServer标记。
  4. 但是,这似乎还不够,OAuth服务器永远不会被初始化,并且调用服务器应该监听的URL只会回答错误404.

    为了找到问题,我(几乎)成功添加了一个小的REST服务。这是我使用的代码:

    第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>
    

    第2步:springwebservice-servlet.xml

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

    第3步:MyRestController.java

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

    奇怪的是,这不起作用。它似乎忽略了@RestController标记,只是将其处理为正常@Controller,从而搜索名为“Hello World!”的视图。而不只是撤回该文本:

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

    但与此无关,它确实表明步骤1和2不应成为问题。但是,我的OAuth2服务器配置永远不会被调用!为什么会出现这种情况,我错过了什么?

    以下是OAuth2服务器配置的代码:

    @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");
        }
    
    }
    

1 个答案:

答案 0 :(得分:0)

多么愚蠢的错误(和一个愚蠢的问题) 我只是忘记了@Configuration注释之上的@EnableAuthorizationServer注释。

现在至少可以识别授权服务器:

  

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

我可以删除这个问题,但也许它会帮助其他想要实现类似功能的人(至少我无法为Liferay或Spring Security找到太多文档)。请注意,我仍然有一个关于自动装配的例外,所以这不是让一切工作的最终解决方案。