【问题标题】:How to allow access to resource from client only in Spring Security OAuth2?如何仅在 Spring Security OAuth2 中允许从客户端访问资源?
【发布时间】:2015-05-25 23:15:50
【问题描述】:

我使用带有 Spring Security OAuth2 的 Spring-Boot 开发了一个简单的 Web 应用程序,我希望只允许从客户端应用程序访问所有资源。有些资源需要对客户端进行身份验证,而有些则不需要。

我有以下配置:

@Configuration
@EnableResourceServer
protected static class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.requestMatchers()
            .and()
            .authorizeRequests()
            .antMatchers("/account/**").permitAll()
            .antMatchers("/user/**").access("#oauth2.hasScope('read')")
            .antMatchers("/transaction/**").access("#oauth2.hasScope('read')");
    }

}
@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
        endpoints.authenticationManager(authenticationManager);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
            .withClient("my-trusted-client")
            .authorizedGrantTypes("authorization_code", "password", "refresh_token")
            .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
            .scopes("read", "write", "trust")
            .accessTokenValiditySeconds(3600);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.allowFormAuthenticationForClients();
    }

}

我正在尝试做的是资源“/account/**”,它不应该要求客户端进行身份验证,但仍然只允许通过客户端访问。

对于其他资源,它只允许通过客户端访问,并且必须经过身份验证。

不幸的是,在上面的当前设置中,我仍然能够从客户端外部访问“/account/**”。

感谢任何帮助。

更新 1

我忘记包含的其他信息,我使用的是 grant_type 密码。

所以我想要的是这个。例如:

/account - 只能通过 client_id/client_secret 访问。不需要用户进行身份验证,这意味着用户不需要拥有访问令牌。

/user - 只能通过 client_id/client_secret 访问。并且需要对用户进行身份验证,这意味着用户必须拥有访问令牌。

我指的客户端是具有client_id和client_secret的移动应用程序。

如果我仍然不清楚,请告诉我。

【问题讨论】:

  • “它不应该要求客户端进行身份验证,但仍然只允许通过客户端访问”是什么意思。 - CLIENT 不必经过身份验证,或者 USER?
  • “只允许通过客户端访问”和“不应该要求客户端进行身份验证”似乎是矛盾的目标。如果无法对请求进行身份验证,如何控制访问?
  • 我已经更新了我上面的帖子,以进一步解释我想要实现的目标。

标签: spring spring-security spring-boot spring-security-oauth2


【解决方案1】:

然后是这样的:

.authorizeRequests()
   .antMatchers("/account/**").access("#oauth2.isClient()")
   .antMatchers("/user/**").access("#oauth2.isUser() && #oauth2.hasScope('read')")

【讨论】:

  • 我在使用 #oath2.isClient() 时收到 401 Unauthorized 错误。 curl -X POST -F client_id=my-trusted-client -F description=SomeUser -F email_address=someemail@somedomain.com -F pass word=somepassword -i http://localhost:8080/account/registration 返回{"error":"unauthorized","error_description":"Full authentication is required to access this resource"}
  • 这不是 oauth2 请求(没有访问令牌)。您需要获取访问令牌,然后将其添加到标头“-H Authorization: $TOKEN”中。
  • 好的。我明白你的意思。但是用户不需要经过身份验证才能获得访问令牌吗?或者有没有办法在不进行身份验证的情况下生成它?我这样做的原因是因为我有一个资源 /account/register 接受参数并注册用户。但我只想允许通过移动应用程序注册。不是通过浏览器,因此这就是我想做的事情。
  • 我猜您可以使用访问令牌对用户注册进行身份验证,但是客户端如何获得秘密(对于移动客户端来说真的没有多大意义)?我看到的大多数注册过程都涉及未经身份验证的请求,然后是验证(例如电子邮件或短信)。因此,在这个问题的上下文中,我猜您可能希望初始 POST 未经身份验证,但将帐户置于非活动状态。
  • 如果它不是移动客户端,那么访问令牌当然更有意义。
猜你喜欢
  • 1970-01-01
  • 2015-03-24
  • 2012-10-22
  • 1970-01-01
  • 2021-02-03
  • 2013-06-20
  • 2020-02-11
  • 2019-01-13
  • 2019-04-06
相关资源
最近更新 更多