问题:当我使用内存中的用户详细信息配置时,一切正常。一旦我尝试使用自定义UserDetailsService,我就会在进行身份验证时获得无限循环。
我使用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
,我的凭据就像我期望的那样被接受。当我更改配置以使用自定义UserDetailsService时,出现问题。我改变的只是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的引用
我有两个主要问题:
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]
方法?