java - 将 WebSecurityConfigurerAdapter 与 Spring OAuth2 和 user-info-uri 一起使用

标签 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=monederobingosecret
security.oauth2.client.authorized-grant-types=authorization_code,refresh_token,password,client_credentials
security.oauth2.client.scope=company,client

然后,在一个单独的 spring boot 项目中,我创建了一个资源服务器。

@SpringBootApplication
@EnableResourceServer
public class App {
   ...
}

有了这个application.properties

server.port=9090
spring.application.name=app
security.oauth2.resource.user-info-uri=http://localhost:9000/user

现在,如果我使用授权服务检索到的适当 token 发送像这样的 localhost:9090/api 请求,一切正常。

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

为此,我在我的资源服务器 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();
    }

}

现在我不需要发送任何 token 来向 /login 发送请求。

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

{
  "timestamp": 1496027102659,
  "status": 403,
  "error": "Forbidden",
  "message": "Access Denied",
  "path": "/api/v1/points_configuration/314"
}

如何在 Spring Security OAuth2 中只为少数几个 URL 模式配置安全性?

最佳答案

有关 Spring OAuth 安全性的更多信息,请关注此链接:Secure Spring REST Api with OAuth

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

授权服务器

    @Configuration
    @EnableAuthorizationServer
    public class AuthorizationApplication extends AuthorizationServerConfigurerAdapter{

    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints)
                throws Exception {
            endpoints
                    .userDetailsService(userDetailsService)
                    .authenticationManager(this.authenticationManager).tokenStore(tokenStore()).approvalStoreDisabled();
        }

       @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients.withClientDetails(mongoClientDetailsService);
            /*inMemory()
                    .withClient(propertyResolver.getProperty(PROP_CLIENTID))
                    .scopes("read", "write")
                    .authorities("ROLE_CLIENT")
                    .authorizedGrantTypes("password", "refresh_token","client_credentials")
                    .secret(propertyResolver.getProperty(PROP_SECRET))
                    .accessTokenValiditySeconds(propertyResolver.getProperty(PROP_TOKEN_VALIDITY_SECONDS, Integer.class, 18000));*/
        }

//Do others stuff
    }

资源服务器

您要使用 OAuth 保护的所有 Url 都应在此服务器配置中提及。它启用了一个 Spring Security 过滤器,该过滤器使用传入的 OAuth2 token 对请求进行身份验证。虽然大多数 WebSecurityConfigurerAdapter 扩展类用于基本的安全配置,例如添加过滤器、允许不安全的 url 或实现 session 策略等。

@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
}

关于java - 将 WebSecurityConfigurerAdapter 与 Spring OAuth2 和 user-info-uri 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44234159/

相关文章:

spring - 如何在 Rest 模板中重用用户 (authorization_code) 的 oauth2 token

spring-security - JwtClaimsSetVerifier 已弃用。现代的等价物是什么?

java - Android: "next"单击表单以错误的 View 结束

java - 按指定值排序对象的优先级队列

java - Mockito 使用 Spring Boot 模拟 Java @Value

java - 如何使用动态 URL 匹配器配置配置 Spring security Oauth 2.0?

java - 用于求解小模型的 Cplex Java

java - 如何将 java 文件转换为应用程序?

java - HTTP Get - Python 与 Spring Rest 模板