【问题标题】:Accessing my api using google bearer token使用谷歌不记名令牌访问我的 api
【发布时间】:2017-04-12 07:59:25
【问题描述】:

我通过定义以下配置以及相应的 application.yml 文件来启用 google auth。

            @EnableOAuth2Sso
            @Configuration
            public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
                @Override
                protected void configure(HttpSecurity http) throws Exception {
                    http
                            .antMatcher("/**")
                            .authorizeRequests()
                            .antMatchers("/", "/login**").permitAll()
                            .anyRequest().authenticated()
                            .and()
                            .exceptionHandling()
                            .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/"));
                }
            }

我可以通过浏览器访问我的各种端点。但客户端可能并不总是浏览器。

我有一个控制器方法定义如下

@GetMapping("/user")
public Principal getUser(Principal principal) {
    return principal;
}

从返回的 Principal 我可以看到类型为 Bearer 的 tokenValue。我怎样才能对我的api使用它?或其他代币。我只是想使用 google auth 和 oauth 访问我自己的 api。

wget -i http://localhost:8080/user -H "Authorization: Bearer $TOKEN"

将我重定向到登录页面。

为了澄清一点,我想使用 google auth 进行身份验证,但能够使用 oauth 访问我的 api。天气是谷歌返回的令牌还是春天生成的令牌都没有关系。关于如何实现这一点的任何线索?

【问题讨论】:

    标签: spring spring-security oauth google-oauth spring-security-oauth2


    【解决方案1】:

    您可以查看spring's tutorial focusing on oauth2,并查看 github 项目。他们有一个不错的身份验证服务器项目,您可以在其中找到您想要实现的目标。

    测试场景的步骤如下:

    • 查看教程项目git clone https://github.com/spring-guides/tut-spring-boot-oauth2.git
    • 运行名为auth-server的spring boot项目 cd auth-server && mvn spring-boot:run
    • 通过http://localhost:8080进行身份验证

    您会发现在 auth-server 端(api 服务器),OAuth2Authentication 主体将可用,并提供不记名令牌。如果用户通过身份验证,您可以使用此 auth-server 示例设计一个返回此令牌的控制器。

    • 然后您将能够使用此类请求 wget 或 curl 身份验证服务器:

    curl -X GET "http://localhost:8080/me" -H "Authorization: Bearer 22e70fcf-eb60-483c-9105-xxxx"

    在我的测试中,我得到了以下回复:{"name":"674008369426415"}

    没有持票人,幸好得到:

    curl -X GET "http://localhost:8080/me"

    {"error":"unauthorized","error_description":"Full authentication is required to access this resource"}

    缺少部分代码

    查看您的代码,我认为您缺少 spring 教程的 SSO 过滤器部分:

    http.antMatcher("/**")
    // more configuration here
    .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
    

    @Bean
    public FilterRegistrationBean oauth2ClientFilterRegistration(OAuth2ClientContextFilter filter) {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(filter);
        registration.setOrder(-100);
        return registration;
    }
    

    一定是在某处截获了客户端的请求,所以这可能值得一看。

    【讨论】:

    • 我没有时间尝试这个。但这听起来是对的,既然你只有一个回答的人,我会奖励你赏金。我稍后会更新。
    • 谢谢,spring 的教程真的很不错。我想你会在这里找到你需要的信息。欢迎您的更新。使用第三方 oauth2 来保护 API 是个好主意。
    猜你喜欢
    • 2014-10-17
    • 2020-12-05
    • 2013-07-20
    • 2020-10-15
    • 1970-01-01
    • 2019-07-07
    • 2017-11-13
    • 2013-06-15
    • 2016-10-22
    相关资源
    最近更新 更多