【发布时间】: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