【问题标题】:Spring Security is not working with Authorization: Bearer token from OAuth2Spring Security 不适用于授权:来自 OAuth2 的承载令牌
【发布时间】:2019-12-21 19:27:32
【问题描述】:

我有两个微服务,第一个用于 OAuth2,第二个用于 API。当我从浏览器登录时,一切正常,授权通过并重定向到我的 API 工作正常。但是当我尝试通过 Postman 执行此操作时,我无法访问 API。

请看这个链接,我从https://www.baeldung.com/sso-spring-security-oauth2复制了很多代码

技术栈:Java 8、Spring Boot、Spring Web、Spring Security、OAuth2。

我尝试使用不同的配置和许多选项,但到目前为止我已将代码返回到传出状态,以便您告诉我可能是什么错误。

认证模块:

server:
  port: 8081
  servlet:
    context-path: /auth
@SpringBootApplication
@EnableResourceServer
public class AuthApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(AuthApplication.class, args);
    }

}
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.tokenKeyAccess("permitAll()")
                   .checkTokenAccess("isAuthenticated()");
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
               .withClient("my-client")
               .secret(passwordEncoder.encode("secret"))
               .authorizedGrantTypes("authorization_code", "client_credentials")
               .scopes("user_info", "read", "write", "trust")
               .autoApprove(true)
               .accessTokenValiditySeconds(5000)
               .redirectUris("http://localhost:8080/api/login");
    }
}
@Configuration
@Order(1)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

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

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("john")
            .password(passwordEncoder().encode("john"))
            .roles("USER");
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

}

API 模块:

server:
  servlet:
    context-path: /api
security:
  oauth2:
    client:
      clientId: my-client
      clientSecret: secret
      accessTokenUri: http://localhost:8081/auth/oauth/token
      userAuthorizationUri: http://localhost:8081/auth/oauth/authorize
    resource:
      userInfoUri: http://localhost:8081/auth/user/me

@Configuration
@EnableOAuth2Sso
@EnableWebSecurity
public class OAuthConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/**")
            .authorizeRequests()
            .antMatchers("/login**")
            .permitAll()
            .anyRequest()
            .authenticated()
            .and()
            .logout().permitAll()
            .and()
            .httpBasic().disable();
    }
}
@RestController
public class DashboardController {

    @GetMapping("/demo")
    public String demo() {
        return "Hello";
    }
}

当我获得 access_token 时 - 我无法访问 API。 请看下面的截图

【问题讨论】:

    标签: spring spring-boot spring-security oauth-2.0 bearer-token


    【解决方案1】:

    此问题的根本原因是 OAuth2 应用程序上的 @EnableResourceServer 注释。为什么?因为实际上 OAuth2 服务器不能同时是 Resource 服务和 Authentification 服务。所以我们需要简化OAuth2资源端的逻辑,去掉下面的注解。我要结束这个问题。

    如果有人有问题 - 请写评论

    【讨论】:

      【解决方案2】:

      一般来说,我会专注于编写您的 API 和使用第三方授权服务器。没有人应该编写自己的授权服务器 - 一些在线 Java 指南具有高度误导性。消费者将如何获得令牌并调用您的 API?以下示例消息工作流程可能会帮助您思考:https://authguidance.com/2017/09/26/basicspa-oauthworkflow/ 如果这有帮助,请随时联系我跟进问题

      【讨论】:

      • 你说得对,但如果只是自学项目,我需要做什么?我知道开发自己的授权服务器不是一个好主意,我逐渐放弃了这个想法,但是还有一个有趣的问题,如何让它工作?我已经深入评估了这种情况,我认为这在 API 方面是错误的(也许我忘了添加一个属性)
      • 使用免费的云提供商 - 我通常向开发人员推荐 Okta,因为它是基于标准的具有漂亮 UI 的 - 请参阅本文的第 2 步:authguidance.com/2017/09/25/basicspa-execution
      • 如果您遇到任何困难,请随时与我联系 - 我的代码示例包括一些可能有用的 Java API 内容
      猜你喜欢
      • 2018-05-03
      • 2020-07-20
      • 2015-05-16
      • 2019-03-22
      • 2021-09-19
      • 2014-05-10
      • 2013-12-01
      • 2015-03-06
      • 2016-09-06
      相关资源
      最近更新 更多