【发布时间】:2015-10-14 05:52:53
【问题描述】:
我有两个端点。
/foo - 是一个内部(半私有)端点。仅允许配置的客户端使用。 (不需要用户名和凭据;但 clientID 就足够了)
/greetings - 是一个私有端点。仅允许客户端和用户配置。 (需要clientID和用户名密码)
这是配置。
@Configuration
public class OAuth2ServerConfiguration {
private static final String RESOURCE_ID = "restservice";
@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends
ResourceServerConfigurerAdapter {
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
// @formatter:off
resources
.resourceId(RESOURCE_ID);
// @formatter:on
}
@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests()
.antMatchers("/users").hasRole("ADMIN")
.antMatchers("/greeting").authenticated()
.antMatchers("/foo").authenticated();
// @formatter:on
}
}
@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends
AuthorizationServerConfigurerAdapter {
private TokenStore tokenStore = new InMemoryTokenStore();
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Autowired
private CustomUserDetailsService userDetailsService;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
throws Exception {
// @formatter:off
endpoints
.tokenStore(this.tokenStore)
.authenticationManager(this.authenticationManager)
.userDetailsService(userDetailsService);
// @formatter:on
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
// @formatter:off
clients
.inMemory()
.withClient("clientapp")
.authorizedGrantTypes("password", "refresh_token","authorization_code")
.authorities("USER","ROLE_CLIENT")
.scopes("read", "write")
.resourceIds(RESOURCE_ID)
.secret("123456")
.accessTokenValiditySeconds(600);
// @formatter:on
}
@Bean
@Primary
public DefaultTokenServices tokenServices() {
DefaultTokenServices tokenServices = new DefaultTokenServices();
tokenServices.setSupportRefreshToken(true);
tokenServices.setTokenStore(this.tokenStore);
return tokenServices;
}
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception
{
oauthServer.checkTokenAccess("permitAll()");
}
}
}
这里是控制器
@RestController
public class GreetingController {
private static final String template = "Hello, %s! your password is %s";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@AuthenticationPrincipal User user) {
return new Greeting(counter.incrementAndGet(), String.format(template, user.getName(),user.getPassword()));
}
@RequestMapping("/foo")
public String foo(@AuthenticationPrincipal User user) {
System.out.println(user==null);
return "you are permitted here";
}
}
没有任何令牌我无法访问http://localhost:9001/foo。
所以当我尝试使用下面的 curl 获取访问令牌时(注意我没有传递用户名和密码,只传递了 client_id 和 client_secret)
curl -X POST -vu clientapp:123456 http://localhost:9001/oauth/token -H "Accept: application/json" -d "grant_type=password&scope=read%20write&client_secret=123456&client_id=clientapp"
我收到此错误
{"error":"invalid_grant","error_description":"Bad credentials"}
我的配置有问题。我是使用 Spring 安全 OAuth 的初学者。将不胜感激。
谢谢
【问题讨论】:
-
你查看了 fue 行 .antMatchers("/foo").authenticated();似乎游览配置正在请求身份验证 flor /foo
-
正如我所提到的,我希望 /
foo只能由客户端访问。不公开。但是客户端可以在不提供用户名和密码的情况下访问。我怎样才能做到这一点?
标签: spring-mvc spring-security spring-security-oauth2