我在Tomcat容器中部署了一个Spring应用程序。
不幸的是,我们有一个基于XML和Java的弹簧安全配置的奇怪组合,这使问题复杂化......
我正在尝试使用@EnableAuthorizationServer并扩展AuthorizationServerConfigurerAdapter在应用程序中启用OAUTH2授权服务器。问题是configure(AuthorizationServerSecurityConfigurer security)方法没有被执行,显然一些非常重要的东西,比如密码编码器没有被初始化。
我假设这是因为我们在XML文件中也有安全配置,但无论我试图解决多长时间,我都找不到解决方案。
我们的设置是:HTTP安全性和authenticationManager的定义在XML文件中,OAUTH授权服务器的定义是基于Java的。
任何想法都会受到欢迎......
编辑1:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.passwordEncoder(passwordEncoder);
}
@Override
public void configure(ClientDetailsServiceConfigurer clientsConfigurer) throws Exception {
clientsConfigurer.inMemory()
.withClient("client")
.secret("secret")
.authorizedGrantTypes("password")
.authorities("ROLE_USER")
.scopes("read");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
.authenticationManager(authenticationManager)
.tokenEnhancer(accessTokenConverter())
.tokenStore(tokenStore());
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
@Bean
public DefaultTokenServices tokenServices(ClientDetailsService clientDetailsService) {
DefaultTokenServices tokenServices = new DefaultTokenServices();
tokenServices.setClientDetailsService(clientDetailsService);
tokenServices.setTokenEnhancer(accessTokenConverter());
tokenServices.setTokenStore(tokenStore());
tokenServices.setAccessTokenValiditySeconds(accessTokenValidity);
return tokenServices;
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
return converter;
}
}
然后,这是xml配置
<http pattern="/oauth/*" create-session="stateless">
<csrf disabled="true"/>
<intercept-url pattern="/oauth/token" access="isFullyAuthenticated()"/>
<intercept-url pattern="/oauth/token_key" access="permitAll()"/>
<http-basic/>
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userDetailsService">
<password-encoder ref="passwordEncoder"/>
</authentication-provider>
<authentication-provider>
<user-service>
<user name="client" password="secret" authorities="ROLE_USER"/>
</user-service>
</authentication-provider>
</authentication-manager>
我还有一个问题,如果我不在上面的身份验证管理器中添加第二个身份验证提供程序,OAUTH2 /令牌端点将失败并告诉我它找不到提到的客户端....我没有为什么它需要身份验证提供程序,我一直认为授权服务器中客户端的定义涵盖了这一点。
@Configuration
@EnableWebSecurity
@ComponentScan("com.authentication")
@ImportResource("classpath:spring-security.xml")
public class SecurityConfig {
}