首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >让oauth2与spring-boot和rest一起工作

让oauth2与spring-boot和rest一起工作
EN

Stack Overflow用户
提问于 2014-02-13 15:06:18
回答 2查看 6.6K关注 0票数 2

我正在尝试让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")

代码语言:javascript
运行
复制
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命令,那就太棒了。

EN

回答 2

Stack Overflow用户

发布于 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

票数 3
EN

Stack Overflow用户

发布于 2014-02-17 08:40:46

是。这就是我所做的让它以这种方式工作的方法。我相信这是正确的解决方案(除了在grant_type上使用client_credentials,但我不是专家:-)如果有更好的解决方案,那就太棒了。非常感谢你抽出时间来帮助我。

代码语言:javascript
运行
复制
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;
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21747431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档