Java 将WebSecurity配置适配器与Spring OAuth2和用户信息uri一起使用

Java 将WebSecurity配置适配器与Spring OAuth2和用户信息uri一起使用,java,spring-boot,spring-security-oauth2,spring-oauth2,Java,Spring Boot,Spring Security Oauth2,Spring Oauth2,我创建了一个授权服务,如下所示 @SpringBootApplication @EnableAuthorizationServer public class AuthorizationApplication { ... } 使用此application.properties server.port=9000 security.oauth2.client.client-id=monederobingo security.oauth2.client.client-secret=moneder

我创建了一个授权服务,如下所示

@SpringBootApplication
@EnableAuthorizationServer
public class AuthorizationApplication {
   ...
}
使用此
application.properties

server.port=9000
security.oauth2.client.client-id=monederobingo
security.oauth2.client.client-secret=monederobingosecret
security.oauth2.client.authorized-grant-types=authorization_code,refresh_token,password,client_credentials
security.oauth2.client.scope=company,client
server.port=9090
spring.application.name=app
security.oauth2.resource.user-info-uri=http://localhost:9000/user
然后,在一个单独的spring引导项目中,我创建了一个资源服务器

@SpringBootApplication
@EnableResourceServer
public class App {
   ...
}
使用此
application.properties

server.port=9000
security.oauth2.client.client-id=monederobingo
security.oauth2.client.client-secret=monederobingosecret
security.oauth2.client.authorized-grant-types=authorization_code,refresh_token,password,client_credentials
security.oauth2.client.scope=company,client
server.port=9090
spring.application.name=app
security.oauth2.resource.user-info-uri=http://localhost:9000/user
现在,如果我用授权服务检索到的适当令牌发送一个像这样的请求,那么一切都可以正常工作

但是,在向
localhost:9090/login
发送请求时,我不想发送此令牌

为此,我在我的资源服务器spring boot应用程序中创建了这个类

@Configuration
public class SpringConfig extends WebSecurityConfigurerAdapter {
    @Override protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/login")
                .permitAll()
                .antMatchers("/api/**")
                .authenticated();
    }

}
现在我不需要发送任何令牌来向
/login
发送请求

但是,我现在在使用有效令牌向
/api
发送请求时收到以下消息

{
  "timestamp": 1496027102659,
  "status": 403,
  "error": "Forbidden",
  "message": "Access Denied",
  "path": "/api/v1/points_configuration/314"
}
如何在Spring security OAuth2中仅为几个URL模式配置安全性?

请遵循以下内容了解有关Spring OAuth安全性的更多信息:

为了在Spring boot中实现OAuth安全性,您必须通过分别从
AuthorizationServerConfigurerAdapter
ResourceServerConfigurerAdapter
扩展来创建授权和资源服务器

授权服务器 资源服务器 在这个服务器配置中,应该提到您希望使用OAuth保护的所有Url。它启用了一个Spring安全过滤器,该过滤器使用传入的OAuth2令牌对请求进行身份验证。而扩展类主要用于基本的安全配置,如添加过滤器、允许不安全的url或实现会话策略等

@Configuration
@EnableResourceServer
public class App extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
    http.requestMatchers().antMatchers("/api/**").and().authorizeRequests()
                .antMatchers("/api/**").authenticated();
}
  //Do others stuff
}

Add.antMatchers(“/api/**”).authenticated();在资源服务器配置中,然后重试again@Afridi我在
SpringConfig
类中已经有了它。我不是在谈论WebSecurityConfigureAdapter扩展类。从ResourceServerConfigurerAdapter扩展您的“App”类,然后重写此方法:@override public void configure(HttpSecurity http)抛出异常{http.authorizeRequests().antMatchers(“/api/**”).authorized();}@有效。请你加上这个作为答案,我会把它标记为接受。另外,为什么使用
ResourceServerConfigurerAdapter
而不是
WebSecurityConfigureAdapter
可以实现这一点?userDetailsService(userDetailsService)在中不可用AuthorizationServerConfigurerAdapter@wildthing81我使用了
authorizationServerEndpointsConfiger.userDetailsService(userDetailsService)
不是
AuthorizationServerConfigurerAdapter.*
端点。userDetailsService(userDetailsService).
我的错…我是说AuthorizationServerEndpointsConfiguration。方法不存在这对我有帮助。谢谢