我正在尝试让oauth2与spring-boot一起工作,并保护我的rest方法调用,但并未取得太大成功。我曾尝试在rg.springframework.boot:spring-boot-starter-security:1.0.0.RC1.中使用spring-security-oauth2-javaconfig:1.0.0.CI-SNAPSHOT
*gradle: compile("org.springframework.boot:spring-boot-starter-security:1.0.0.RC1")
compile ('org.springframework.security.oauth:spring-security-oauth2-javaconfig:1.0.0.CI-SNAPSHOT'){
exclude module: 'spring-security-config'
exclude module: 'spring-security-core'
exclude module: 'spring-security-web'
}
现在,我只是尝试让身份验证和资源服务器正常工作。我已经从spring- sparklr2 -OAuth2-javaconfig示例中复制并尝试修改现有的安全示例。
我得到的最后一个错误是:" error ":"invalid_client","error_description":"Bad client credentials when I run curl -v --data "grant_type=password&username=marissa&password=koala&client_id=tonr&secret=secret“-X POST localhost:8100/oauth/token。
我从初学者的角度理解oauth2,关于oauth2的spring-boot和rest资源的匮乏使得它很难实现。有什么建议吗?
如果有人能提供一种类似食谱的方法来配置oauth2身份验证和授权,以保护rest api调用以及相关的curl命令,那就太棒了。
发布于 2014-02-13 15:26:34
Java config对oauth2的支持正在进行中,但是使用my fork可能会更成功。如果我是你,我现在会坚持使用oauth2的XML。下面是一个包含最少XML的bootified sparklr2。我最近没有检查它是否正常工作,但如果您将引导依赖项更新到1.0.0.RC2,那么它的状态应该不会太差。
更新:@Configuration
已经移到了主OAuth2 repo,所以fork和它的父分支现在基本上是多余的(可能很快就会被移除)。
更新: bootified示例现在也在使用@Configuration
。
发布于 2014-02-17 08:40:46
是。这就是我所做的让它以这种方式工作的方法。我相信这是正确的解决方案(除了在grant_type上使用client_credentials,但我不是专家:-)如果有更好的解决方案,那就太棒了。非常感谢你抽出时间来帮助我。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.config.annotation.authentication.configurers.InMemoryClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.OAuth2ServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.OAuth2ServerConfigurer;
import org.springframework.security.oauth2.provider.token.InMemoryTokenStore;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends OAuth2ServerConfigurerAdapter {
private final String applicationName = "restservice";
@Value("${client_id}")
private String client_id;
@Value("${client_secret}")
private String client_secret;
@Value("${grant_type}")
private String grant_type;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.and()
.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.apply(new OAuth2ServerConfigurer())
.tokenStore(new InMemoryTokenStore())
.resourceId(applicationName);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(new InMemoryUserDetailsManager(getUserDetails()))
.and()
.apply(new InMemoryClientDetailsServiceConfigurer())
.withClient(client_id)
.resourceIds(applicationName)
.scopes("read", "write")
.authorities("USER")
.authorizedGrantTypes(grant_type)
.secret(client_secret);
}
private static final Collection<UserDetails> getUserDetails() {
List<UserDetails> userDetails = new ArrayList<UserDetails>();
userDetails.add(new User("user", "password", AuthorityUtils.createAuthorityList(
"USER", "read", "write")));
return userDetails;
}
}
https://stackoverflow.com/questions/21747431
复制相似问题