我正在尝试将 Spring OAuth2 服务器集成到 Liferay 中。尽管存在一些问题,但执行此操作的基本方法似乎很清楚:
- 使用
DispatcherServlet
将 URL 映射到 servlet - 用
<context:component-scan>
配置这个 servlet用于查找和初始化 Spring OAuth2 服务器的标记 - 实现
AuthorizationServerConfigurer
并添加@EnableAuthorizationServer
标签。
然而,这似乎还不够,OAuth 服务器从未初始化,并且对服务器应该监听的 URL 的调用只是简单地用错误 404 回答。
为了定位问题,我(几乎)成功地添加了一点 REST 服务。这是我使用的代码:
第一步: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>
第二步:springwebservice-servlet.xml
<context:component-scan base-package="mypackage" />
第三步:MyRestController.java
@RestController
public class MyRestController {
@RequestMapping("/test")
public String testRest() {
return "Hello World!";
}
}
奇怪的是,这不起作用。它似乎忽略了 @RestController
标记并像处理普通 @Controller
一样处理它,从而搜索名为“Hello World!”的 View 而不是仅仅返回该文本:
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");
}
}
最佳答案
多么愚蠢的错误(也是一个愚蠢的问题)
我只是忘记了 @EnableAuthorizationServer
注释上方的 @Configuration
注释。
现在至少可以识别授权服务器:
Mapped "{[/oauth/authorize],methods=[POST], ... onto ...
我可以删除这个问题,但也许它会帮助其他想要实现类似目标的人(我至少找不到 Liferay 或 Spring Security 的很多文档)。请注意,我仍然得到一个关于 Autowiring 的异常,所以这不是让一切正常工作的最终解决方案。
关于java - 如何将 Spring OAuth2 Server 集成到 Liferay 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28021187/