【发布时间】:2015-03-24 12:06:48
【问题描述】:
我有一个应用程序正在使用 OAuth2 password grant type 来管理用户对其资源的授权。所有 App 资源只允许使用一次提供的令牌代表某个用户进行操作的客户端访问,但创建用户的 URI 除外,我希望只有经过身份验证的客户端才能访问它。我正在使用 spring-security-oauth2 作为我的 OAuth 实现,但无法弄清楚如何以一种比下面描述的方式更简单的方式来实现这一点:
POST /users 只能由经过身份验证的客户端访问。
目前我通过删除@EnableAuthorizationServer 并创建一个新类并扩展AuthorizationServerSecurityConfiguration 类和覆盖方法:configure( HttpSecurity http ) 并创建一个新的@Configuration 类和@Import AuthorizationServerEndpointsConfiguration 和@ 987654331@。
问题是,在我的新自定义 class 中,我需要在被覆盖的方法中覆盖并复制/粘贴整个方法的原始代码,以类似以下内容结尾:
@Override
protected void configure( HttpSecurity http ) throws Exception {
AuthorizationServerSecurityConfigurer configurer = new AuthorizationServerSecurityConfigurer();
FrameworkEndpointHandlerMapping handlerMapping = endpoints.oauth2EndpointHandlerMapping();
http.setSharedObject(FrameworkEndpointHandlerMapping.class, handlerMapping);
configure(configurer);
http.apply(configurer);
String tokenEndpointPath = handlerMapping.getServletPath("/oauth/token");
String tokenKeyPath = handlerMapping.getServletPath("/oauth/token_key");
String checkTokenPath = handlerMapping.getServletPath("/oauth/check_token");
http
.authorizeRequests()
.antMatchers(tokenEndpointPath).fullyAuthenticated()
.antMatchers( HttpMethod.POST, "/users/**").fullyAuthenticated()
.antMatchers(tokenKeyPath).access(configurer.getTokenKeyAccess())
.antMatchers(checkTokenPath).access(configurer.getCheckTokenAccess())
.and()
.requestMatchers()
.requestMatchers( new AntPathRequestMatcher(tokenKeyPath),
new AntPathRequestMatcher(tokenEndpointPath),
new AntPathRequestMatcher(checkTokenPath),
new AntPathRequestMatcher("/users/**", HttpMethod.POST.name()));
http.setSharedObject(ClientDetailsService.class, clientDetailsService);
}
我的第一个问题是,有更好的方法吗?
我想做的第二件事是在创建新用户时通过密码授予类型自动创建 AccessToken(在 URI POST /users 中),我想不出任何方法来做到这一点。
有人可以提供关于这两个需求的任何见解吗?
谢谢
【问题讨论】:
标签: java spring spring-mvc oauth spring-security-oauth2