spring-boot - Spring Boot OAuth2 示例应用程序

标签 spring-boot oauth-2.0 client-server openjdk-11

应用程序.yml

server:
  port: 8082

spring:
  security:
    oauth2:
      client:
        registration:
          custom-client:
            client-id: R2dpxQ3vPrtfgF72
            client-secret: fDw7Mpkk5czHNuSRtmhGmAGL42CaxQB9
            client-name: Auth Server
            scope: user_info
            provider: custom-provider
            redirect-uri-template: http://localhost:8082/login/oauth2/code/
            client-authentication-method: basic
            authorization-grant-type: authorization_code
        provider:
          custom-provider:
            token-uri: http://localhost:8081/auth/oauth/token
            authorization-uri: http://localhost:8081/auth/oauth/authorize
            user-info-uri: http://localhost:8081/auth/user/me
            user-name-attribute: name

的扩展WebSecurityConfigurerAdapter
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {

        http.antMatcher("/**") //
                .authorizeRequests()//
                .antMatchers("/", "/login**")//
                .permitAll() //
                .anyRequest() //
                .authenticated() //
                .and() //
                .oauth2Login();

    }

}

依赖关系
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>jakarta.mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-oauth2-client</artifactId>
        <exclusions>
            <exclusion>
                <groupId>com.sun.mail</groupId>
                <artifactId>javax.mail</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-oauth2-jose</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
    </dependency>
    <!-- jaxb模块引用 - start -->
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>
    <!-- jaxb模块引用 - end -->
</dependencies>

我得到的异常(exception):
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientRegistrationRepository' defined in class path resource [org/springframework/boot/autoconfigure/security/oauth2/client/servlet/OAuth2ClientRegistrationRepositoryConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository]: Factory method 'clientRegistrationRepository' threw exception; nested exception is java.lang.IllegalArgumentException: redirectUriTemplate cannot be empty

更多信息:

工具:Maven、Eclipse、OpenJDK11

问题:我错过了什么?该错误似乎与 bean redirectUriTemplate 的错误配置有关。不能为空。我写的 yml 没有任何问题。
不知道我哪里出错了。

最佳答案

属性的实际名称不是 redirect-uri-template但实际上只有 redirect-uri .

引用:https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#oauth2login-boot-property-mappings

关于spring-boot - Spring Boot OAuth2 示例应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58720382/

相关文章:

java - 使用 derby 通过 EmbeddedDatabaseBuilder 重新执行应用程序后,表正在刷新

struts2 - 使用 spring boot 运行 struts 2 web 应用程序

angular-oauth2-oidc 未设置 access_token

swift - 在 Vapor 中使用第三方 OAuth API

facebook - 为 facebook OAuth2 指定多个重定向 URI

java - 通过互联网与客户端/服务器应用程序交互的最佳方式?

spring-boot - 使用 Spring boot 时出现 403 禁止错误 - 安全

javascript - 如何在 Meteor 中验证服务器上的用户创建

c - 在客户端 C 中多路复用 stdin 和 socket

java - Derby 嵌入式驱动程序在哪里?