【问题标题】:Spring Oauth2 with WebSecurity带有 WebSecurity 的 Spring Oauth2
【发布时间】:2016-10-20 05:27:18
【问题描述】:

我正在尝试设置 Spring OAuth2 并使用自定义 WebSecurityConfigurerAdapter (@EnableWebSecurity)。

作为基础,我复制了以下两个项目:

  • vanilla-sample
  • 具有@EnableOAuth2Sso 和相应属性的客户端应用程序

这可以按预期开箱即用。

但是,当我尝试将带有 @EnableWebSecurity 的 WebSecurityConfigurerAdapter 添加到自动服务器(香草)时,它失败了。

我得到一个 身份验证失败:无法获取访问令牌 在客户端登录页面登录和授权后重定向回来。

我已经设置了一个 security.oauth2.resource.userInfoUri,它在没有 WebSecurityConfigurerAdapter 的情况下工作得很好。

任何想法如何使用 WebSecurityConfigurerAdapter 配置 oauth2?

【问题讨论】:

  • 您要创建什么样的 OAuth2 安全性?只有 oauth2 授权服务器或具有资源服务器的身份验证服务器?你在 github 上的某个地方有你的例子吗?
  • 我上传了一个示例here。它正在工作,它是如何签入的。但是如果您在 auth-server/Application 中取消注释 CustomWebSecurity,它就不再工作了。 (只需通过main方法启动auth-server和client,在Browser中打开localhost:8081/client
  • 如果您启用网络安全,可能 /oauth/authorize 和 /oauth/confirm_access 端点不可见。
  • 您能否提供更多信息,您是如何测试的?我认为它不适用于当前设置。
  • 如果我在 github 上启动这两个应用程序并打开 localhost:8081/client 我正确地得到重定向可以登录和授权并且也得到重定向回来,客户端还请求 userInfoUri 并验证用户。但是,如果我取消注释 CustomWebSecurity 并重新启动两个应用程序,则客户端应用程序的最后一步将失败,并显示“身份验证失败:无法获取访问令牌”。

标签: spring-security spring-boot spring-security-oauth2 oauth2 spring-oauth2


【解决方案1】:

将您的 http 安全配置更改为以下内容:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .requestMatchers()
                .antMatchers("/", "/login", "/oauth/authorize", "/oauth/confirm_access")
                .and()
                .authorizeRequests()
                .anyRequest().authenticated();
    }

【讨论】:

  • 这对我不起作用。我在 /oauth/authorize 上给了我 403。
猜你喜欢
  • 1970-01-01
  • 2015-07-15
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 2020-08-26
  • 2017-10-02
  • 1970-01-01
  • 2017-08-19
相关资源
最近更新 更多