我正在尝试使用授权代码授予或隐式授予在Spring Boot 2.xx中设置非常基本的oAuth2身份验证,但我似乎无法访问获得令牌后,资源服务器(与授权服务器位于同一Spring Boot应用程序中)。
以下是WebSecurityConfigurerAdapter
的配置
@EnableWebSecurity
@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
private static final String[] IGNORE_URIS = {
"/swagger-resources/**",
"/swagger-ui.html",
"/v2/api-docs",
"/webjars/**",
"/resources/**",
"/h2-console/**",
"/common/**",
"/configuration/ui",
"/configuration/security",
"/error"
};
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers(IGNORE_URIS);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/product/**")
.hasAnyRole("ADMIN").and()
.httpBasic().and().formLogin().and().authorizeRequests().anyRequest().authenticated();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("admin").password("{noop}admin").roles("ADMIN");
}
@Bean
public PasswordEncoder bCrypt() {
return new BCryptPasswordEncoder();
}
还有AuthorizationServerConfigurerAdapter
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
private final AuthenticationManager authenticationManager;
@Autowired
public AuthorizationServerConfiguration(AuthenticationConfiguration authenticationConfiguration) throws Exception {
this.authenticationManager = authenticationConfiguration.getAuthenticationManager();
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient("my-client-id")
.authorizedGrantTypes("authorization_code", "implicit")
.authorities("ADMIN")
.scopes("all")
.resourceIds("product_api")
.secret("{noop}secret").redirectUris("https://google.com").accessTokenValiditySeconds(0);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.tokenKeyAccess("permitAll()")
.checkTokenAccess("permitAll()");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
}
到目前为止,一切都很好。通过在浏览器中输入以下网址,我可以访问默认的Spring登录页面。
然后显示登录页面,然后输入我的凭据。
登录后,我便可以授予对“ my-client-id” 应用的访问权限。
最终,我批准了该应用程序后,我可以在浏览器的URL栏中看到新生成的访问令牌。
https://www.google.com/#access_token=f2153498-6a26-42c6-93f0-80825ef03b16&token_type=bearer&scope=all
我的问题是,当我还配置资源服务器时,所有这些流程都将不起作用。
@EnableResourceServer
@Configuration
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
resources.resourceId("product_api");
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.antMatchers("/**")
.and().authorizeRequests()
.antMatchers("/**").permitAll();
}
}
我在做什么错?当我尝试像以前一样访问oauth/authorize
网址时,得到以下信息:
为什么?一个人如何访问登录页面并检索令牌?我想念什么?
答案 0 :(得分:0)
您需要使用
@Order
用于指定WebMvc和ResourceServer类的顺序的注释
@EnableWebSecurity
@Configuration
@Order(1)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
...
}
以及资源服务器
@EnableResourceServer
@Configuration
@Order(2)
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
...
}
如果您想查看可行的示例,可以在此处https://github.com/alex-petrov81/stackoverflow-answers/tree/master/auth-server-also-resource进行检查。 我已经从您的代码示例中创建了它。