配置自定义UserDetailsS​​ervice时出现StackOverflowError

时间:2017-01-06 05:53:46

标签: java spring-boot spring-security-oauth2

问题:当我使用内存中的用户详细信息配置时,一切正常。一旦我尝试使用自定义UserDetailsS​​ervice,我就会在进行身份验证时获得无限循环。

我使用Spring Security的OAuth2包处理身份验证创建了一个简单的应用程序。以下是配置的快速浏览。

我需要一个授权服务器,所以我为我的应用创建了一个带有内存存储客户端凭据的简单服务器:

Data Source=.\SQLEXPRESS
Data Source=(local)\SQLEXPRESS
Data Source=YourServer\SQLEXPRESS

接下来,我希望TokenEndpoint能够为我的用户凭据使用ResourceOwnerPasswordTokenGranter。我能做到这一点的唯一方法是在端点configurer中设置AuthenticationManager:

@Configuration
@EnableAuthorizationServer
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {
  @Override
  public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory()
        .withClient("myApp")
        .secret("");
  }

最后,我将AuthenticationManager配置为具有内存中的一组用户凭据,如in the Spring Docs所述:

  @Qualifier("authenticationManager")
  @Autowired
  private AuthenticationManager authManager;

  @Override
  public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    LOG.info("Adding authManager");
    endpoints.authenticationManager(authManager);
  }

通过此设置,我可以正常运行项目。在日志中,我看到 @Configuration public static class UserPasswordAuthConfig extends GlobalAuthenticationConfigurerAdapter { @Override public void init(AuthenticationManagerBuilder auth) throws Exception { LOG.info("Adding userDetailsService"); auth.inMemoryAuthentication() .withUser("me") .password("horsebatterystaple") .authorities("ALL_POWER"); } } } ,然后是Adding userDetailsService,我的凭据就像我期望的那样被接受。当我更改配置以使用自定义UserDetailsS​​ervice时,出现问题。我改变的只是Adding authManager

UserPasswordAuthConfig

当我运行它时,我从未在日志中看到 @Configuration public static class UserPasswordAuthConfig extends GlobalAuthenticationConfigurerAdapter { @Autowired private CustomUserDetailsService userDetailsService; @Autowired private PasswordEncoder passwordEncoder; @Override public void init(AuthenticationManagerBuilder auth) throws Exception { LOG.info("Adding userDetailsService"); auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder); } } ,告诉我它永远不会运行我的Adding userDetailsService方法。我也看到了这个:

init(AuthenticationManagerBuilder auth)

当我尝试获取令牌时,我得到无限递归:

AuthenticationManagerBuilder : No authenticationProviders and no parentAuthenticationManager defined. Returning null.

在我通过它进行调试时,我看到AuthorizationServerEndpointsConfigurer创建了一组默认令牌粒度。在这样做时,我注意到配置器有一个对自身代理的AuthenticationManager的引用

Debugger with infinite proxies

我有两个主要问题:

  • 当我更改内容时,为什么Spring不运行我的java.lang.StackOverflowError: null at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:227) ~[spring-aop-4.2.6.RELEASE.jar!/:4.2.6.RELEASE] > at com.sun.proxy.$Proxy87.authenticate(Unknown Source) ~[na:na] at sun.reflect.GeneratedMethodAccessor71.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_101] at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_101] at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) ~[spring-aop-4.2.6.RELEASE.jar!/:4.2.6.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) ~[spring-aop-4.2.6.RELEASE.jar!/:4.2.6.RELEASE] > at com.sun.proxy.$Proxy87.authenticate(Unknown Source) ~[na:na] at sun.reflect.GeneratedMethodAccessor71.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_101] at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_101] at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) ~[spring-aop-4.2.6.RELEASE.jar!/:4.2.6.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) ~[spring-aop-4.2.6.RELEASE.jar!/:4.2.6.RELEASE] > at com.sun.proxy.$Proxy87.authenticate(Unknown Source) ~[na:na] 方法?
  • 切换到CustomUserDetailsS​​ervice时,为什么会出现疯狂的错误?我错误配置了什么吗?

0 个答案:

没有答案