java - 在同一上下文中使用 Spring Boot 2 OAuth 客户端和资源服务器

标签 java spring-boot spring-security oauth-2.0

我希望我的 Spring Boot 应用程序能够为 protected 前端提供服务,并同时作为该前端的 API 资源服务器,但我无法让 oauth 功能正常工作。

我想要的是当浏览器请求没有 token 的index.html时,Spring Boot应用程序返回302重定向到oauth服务器(在我的例子中是gitlab),因此用户被发送到登录表单。但我还希望 API 在没有 token 的情况下调用 API 时返回 401,因为我认为 302 重定向到登录页面在那里不是很有用。

伪代码:

if document_url == /index.html and token not valid
  return 302 https//gitlab/loginpage
if document_url == /api/restcall and token not valid
  return 401
server document_url

我正在使用 Spring Boot 2.1,关于我的 pom.xml 包含的 oauth

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-oauth2-client</artifactId>
    </dependency>

这是我在 SecurityConfig 中的天真的尝试

public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests().antMatchers("/index.html").authenticated()
        .and()
            .oauth2Login().loginPage("/oauth2/authorization/gitlab")

        .and()
            .authorizeRequests().antMatchers("/api/restcall").authenticated()
        .and()
            .oauth2ResourceServer().jwt();
    }
}

两种配置(oauth2Login 和 oauth2ResourceServer)都可以正常工作。但是,一旦我将它们组合起来,最后一个就会获胜(因此在上面的示例中,不会有 302,浏览器也会看到 index.html 的 401)。我认为他们共享一些配置对象,因此最后的写入获胜。

有没有一种(简单)的方法可以得到我想要的东西?我知道 spring 几乎可以做任何事情,但我非常不希望最终手动配置大量的 beans ...

更新:

我已经为我的代码做了一个最小的示例(包括@dur的建议)here

最佳答案

您需要创建多个配置,并使用 requestMatcher 将它们限制为特定的 URL 模式。根据您的示例,您的配置应如下所示:

SecurityConfigHTML

public class SecurityConfigHTML extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .requestMatchers().antMatchers("/index.html")
                .and()
                .authorizeRequests().anyRequest().authenticated()
                .and()
                .oauth2Login().loginPage("/oauth2/authorization/gitlab");
    }
}

SecurityConfigAPI

public class SecurityConfigAPI extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .requestMatchers().antMatchers("/api/call")
                .and()
                .authorizeRequests().anyRequest().authenticated()
                .and()
                .oauth2ResourceServer().jwt();
    }
}

关于java - 在同一上下文中使用 Spring Boot 2 OAuth 客户端和资源服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58805033/

相关文章:

java - 如何在Spring Security中为两个不同的URL指定两个不同的登录表单?

Spring Security OAuth2 - 将参数添加到授权 URL

java - 在 Gallery 中嵌入 ListView

java - 登录一个json文件

java - 如何使用elasticsearch获取搜索结果?

Spring JPA : The column name ** was not found in this ResultSet

java - 使用JPA(条件搜索)在springboot中根据除ID之外的其他字段查找表中的所有记录

java - 如何将音乐添加到 JFrame?

java - 从 WebView 打开相机

java - 使用第 3 方 OAuth2 服务器登录 Spring Security 应用程序