Java 如何使用经典身份验证将SpringSecurityOAuth服务器添加到现有的web应用程序中?

Java 如何使用经典身份验证将SpringSecurityOAuth服务器添加到现有的web应用程序中?,java,spring,spring-security-oauth2,Java,Spring,Spring Security Oauth2,我有一个基于cookie的经典身份验证web应用程序。这就是它的配置方式: @配置 @启用Web安全性 @EnableGlobalMethodSecurity(Prespenabled=true) 公共类SecurityConfig扩展了WebSecurity配置适配器{ @自动连线 私人用户服务; @自动连线 专用密码编码器; @凌驾 受保护的void configure(最终HttpSecurity http)引发异常{ http.exceptionHandling().authentica

我有一个基于cookie的经典身份验证web应用程序。这就是它的配置方式:

@配置
@启用Web安全性
@EnableGlobalMethodSecurity(Prespenabled=true)
公共类SecurityConfig扩展了WebSecurity配置适配器{
@自动连线
私人用户服务;
@自动连线
专用密码编码器;
@凌驾
受保护的void configure(最终HttpSecurity http)引发异常{
http.exceptionHandling().authenticationEntryPoint(新的http403禁止入口点());
http.authorizeRequests()
.antMatchers(“/some/restricted/url*”).access(“hasRole('admin')”)
.antMatchers(“/some/public/url”).permitAll()
.antMatchers(“/**”).access(“hasRole('registered')”);
}
@自动连线
受保护的无效配置AuthenticationManager(最终AuthenticationManagerBuilder auth)引发异常{
认证
.userDetailsService(userService)
.密码编码器(passwordEncoder);
}
@豆子
@凌驾
公共AuthenticationManager authenticationManagerBean()引发异常{
返回super.authenticationManagerBean();
}
}
现在我想使用OAuth2启用外部应用程序来访问我的一些用户数据。首先,我添加了如下身份验证:

@配置
@EnableAuthorizationServer
公共类OAuthServerConfig扩展了AuthorizationServerConfigurerAdapter{
@自动连线
私有数据源;
@自动连线
私有身份验证管理器身份验证;
@豆子
公共JdbcTokenStore tokenStore(){
返回新的JdbcTokenStore(数据源);
}
@豆子
受保护的AuthorizationCodeServices AuthorizationCodeServices(){
返回新的JdbcAuthorizationCodeServices(数据源);
}
@豆子
公共JdbcClientDetailsService clientDetailsService(){
返回新的JdbcClientDetailsService(数据源);
}
@凌驾
public void configure(AuthorizationServerEndpointsConfigurer端点)引发异常{
最终ApprovalStoreUserApprovalHandler approvalHandler=新ApprovalStoreUserApprovalHandler();
setClientDetailsService(clientDetailsService());
端点
.authorizationCodeServices(authorizationCodeServices())
.authenticationManager(身份验证)
.tokenStore(tokenStore())
.userApprovalHandler(approvalHandler)
.pathMapping(“/oauth/confirm\u access”,“/api/oauth2/confirm\u access”)
.pathMapping(“/oauth/token”,“/api/oauth2/token”)
.pathMapping(“/oauth/check_-token”,“/api/oauth2/check_-token”)
.pathMapping(“/oauth/token\u key”,“/api/oauth2/token\u key”)
.pathMapping(“/oauth/authorize”,“/api/oauth2/authorize”);
}
}
一切似乎都很好。我的web应用程序总体上似乎可以工作,我可以获取代码并将其交换为访问令牌。仅仅访问令牌本身是非常无用的,所以我想添加一个需要有效访问令牌的服务。为此,我添加了以下内容:

@配置
@EnableResourceServer
公共类OAuth2ResourceConfiguration Adapter扩展了ResourceServerConfigurerAdapter{
@自动连线
私有身份验证管理器身份验证;
@自动连线
专用OAuthServerConfig OAuthServerConfig;
@凌驾
public void configure(最终资源服务器安全配置器资源)引发异常{
资源
.authenticationManager(身份验证)
.tokenStore(oAuthServerConfig.tokenStore());
}
}
但这给我带来了一个例外:

Caused by: java.lang.IllegalStateException: Cannot apply org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer@6dd1c3ed to already built object
    at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.add(AbstractConfiguredSecurityBuilder.java:192)

我尽我所能阅读了调试过的源代码和谷歌搜索,但我就是不知道这里发生了什么。我发现的最贴切的提示是,但是它没有解释问题,而且我在编写我自己的配置时非常不成功,正如票证中所建议的那样,可能是因为我不知道问题是什么,因此我需要改变什么。

与此同时,robsilvia在上面列出的github罚单中给出了答案。简而言之,它表示必须将此方法添加到
OAuth2ResourceConfiguration适配器中

@覆盖
public void configure(HttpSecurity http)引发异常{
http.authorizeRequests().anyRequest().authorized();
}
然而,这导致我的oauth配置干扰了我的经典web应用程序安全配置,该配置不再正常工作。然而,我确实发现了,因此我最终通过使用此变体解决了问题:

@覆盖
public void configure(HttpSecurity http)引发异常{
http
.requestMatcher(新的或requestMatcher(
新的AntPathRequestMatcher(“/path/to/oauth/endpoints/*”,
新的AntPathRequestMatcher(“/oauth/protected/resource”)
))
.authorizeRequests().anyRequest().authorized();
}

与此同时,robsilvia在上面列出的github罚单中给出了答案。简而言之,它表示必须将此方法添加到
OAuth2ResourceConfiguration适配器中

@覆盖
public void configure(HttpSecurity http)引发异常{
http.authorizeRequests().anyRequest().authorized();
}
然而,这导致我的oauth配置干扰了我的经典web应用程序安全配置,该配置不再正常工作。然而,我确实发现了,因此我最终通过使用此变体解决了问题:

@覆盖
public void configure(HttpSecurity http)引发异常{
http
.requestMatcher(新的或requestMatcher(
新的AntPathRequestMatcher(“/path/to/oauth/endpoints/*”,
新的AntPathRequestMatcher(“/oauth/protected/resource”)
))
.authorizeRequests().anyRequest().authorized();
}

您能邮寄完整的stacktra吗