【发布时间】:2019-03-21 13:46:44
【问题描述】:
我正在尝试使用@EnableAuthorizationServer 和内存中的客户端为 Spring Boot 中的 OAuth2 授权服务器开发一个简单的 POC。
我的网络安全配置类如下所示:
package com.example.authservice;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**")
.authorizeRequests().
antMatchers("/", "/login**", "/oauth/authorize", "/oauth/authorize**")
.permitAll().
anyRequest()
.authenticated();
}
}
授权服务器配置如下:
package com.example.authservice;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory().
withClient("auth-client").
secret("secret-key").
authorizedGrantTypes("authorization_code").
scopes("openid");
}
}
这是基于授权代码授予流程,当我尝试获取代码(将在下一次调用中用于获取访问令牌)时,我收到未经授权的错误。
curl -X GET \
'http://localhost:8080/oauth/authorize?client_id=auth-client&client_secret=secret-key&grant_type=authorization_code&response_type=code'
错误:
{
"timestamp": "2019-03-20T15:35:41.009+0000",
"status": 403,
"error": "Forbidden",
"message": "Access Denied",
"path": "/oauth/authorize"
}
我假设由于我的网络安全配置中允许/oauth/authorize,它应该返回一个可用于获取访问令牌的代码。有没有人知道可能出了什么问题。
【问题讨论】:
标签: java spring spring-boot spring-security spring-security-oauth2