Spring boot HttpSecurity文件没有方法oauth2Login()

Spring boot HttpSecurity文件没有方法oauth2Login(),spring-boot,spring-security-oauth2,Spring Boot,Spring Security Oauth2,我正在做春季安全Oauth2。在客户端I重写 configure(HttpSecurity http)方法,并希望使用oauth2Login() HttpSecurity文件中的方法。但HttpSecurity没有这个功能 功能。我已经添加了spring-security-oauth2-client的依赖项, pom.xml中的spring boot starter security和spring-security-oauth2。 在HttpSecurity文件中,它写道“版权2002-2016

我正在做春季安全Oauth2。在客户端I重写
configure(HttpSecurity http)
方法,并希望使用
oauth2Login()
HttpSecurity文件中的方法。但HttpSecurity没有这个功能 功能。我已经添加了
spring-security-oauth2-client的依赖项,
pom.xml中的spring boot starter security和spring-security-oauth2
。 在
HttpSecurity
文件中,它写道“版权2002-2016原件 作者”。我如何更新这个


由于您使用的是spring boot,因此可以使用以下依赖项为oauth2自动配置spring安全性:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security.oauth.boot</groupId>
    <artifactId>spring-security-oauth2-autoconfigure</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>
有关参考信息,请参见:


请确保您的spring boot starter父版本正确。
下面是一个示例:
pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>your-artifactId</artifactId>
<version>your-version</version>
<packaging>jar</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath/>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-oauth2-client</artifactId>
    </dependency>

    <!--if you need to generate token-->
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.5.1</version>
    </dependency>
</dependencies>



<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>


</project>
  • 使用定制的实现
    登录页面
    授权端点
    令牌端点
    重定向端点
    用户信息端点


  • 但是我的项目中的HttpSecurity没有方法oauth2Login(),因此我不能使用http。oauth2Login()oauth2Login()是类HttpSecurity的一个方法,您应该可以访问它。您可以检查是否导入了正确的类以及包含的依赖项是否正确吗?参考:您好,您可以尝试将
    org.springframework.security-spring-security-config
    添加为依赖项并重建项目以查看该方法是否公开吗?谢谢,我通过更改spring-boot-starter父级的版本来修复它:)
    @EnableWebSecurity
    public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .oauth2Login()
                    .clientRegistrationRepository(this.clientRegistrationRepository())
                    .authorizedClientService(this.authorizedClientService())
                    .loginPage("/login")
                    .authorizationEndpoint()
                        .baseUri(this.authorizationRequestBaseUri())
                        .authorizationRequestRepository(this.authorizationRequestRepository())
                        .and()
                    .redirectionEndpoint()
                        .baseUri(this.authorizationResponseBaseUri())
                        .and()
                    .tokenEndpoint()
                        .accessTokenResponseClient(this.accessTokenResponseClient())
                        .and()
                    .userInfoEndpoint()
                        .userAuthoritiesMapper(this.userAuthoritiesMapper())
                        .userService(this.oauth2UserService())
                        .oidcUserService(this.oidcUserService())
                        .customUserType(GitHubOAuth2User.class, "github");
        }
    }
    
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.example</groupId>
    <artifactId>your-artifactId</artifactId>
    <version>your-version</version>
    <packaging>jar</packaging>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/>
    </parent>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-oauth2-client</artifactId>
        </dependency>
    
        <!--if you need to generate token-->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.5.1</version>
        </dependency>
    </dependencies>
    
    
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    
    
    </project>
    
    @Configuration
    public class SimpleTestSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    .anyRequest().authenticated()
                    .and()
                    .oauth2Login();
        }
    }
    
        @Configuration
        public class SimpleTestSecurityConfig extends WebSecurityConfigurerAdapter {
    
            private String[] PERMIT_ALL = {"unsecured-endpoint1", "unsecured-endpoint2", "..."};
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.authorizeRequests()
                        .antMatchers(PERMIT_ALL).permitAll()
                        .anyRequest().authenticated()
                        .and()
                        .oauth2Login()
                        .loginPage("/login")
                        .defaultSuccessUrl("/home")
                        .failureUrl("/error")
    
                        .authorizationEndpoint()
                        .baseUri("/oauth2/authorize-client") //default is "/oauth2/authorization"
                        .and()
    
                        .tokenEndpoint()
                        .accessTokenResponseClient(accessTokenResponseClient())
                        .and()
    
                        //.redirectionEndpoint()
                        //.baseUri("/oauth2/redirect") //base for google is "/login/oauth2/code"
                        //.and()
    
                        .userInfoEndpoint().oidcUserService(new OidcUserService(){
                            @Override
                            public OidcUser loadUser(OidcUserRequest userRequest) throws OAuth2AuthenticationException {
                                return super.loadUser(userRequest);
                            }
                });
    
            }
    
            @Bean
            public AuthorizationRequestRepository<OAuth2AuthorizationRequest> authorizationRequestRepository(){
                return new HttpSessionOAuth2AuthorizationRequestRepository();
            }
    
    
            @Bean
            public OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> accessTokenResponseClient(){
                return new NimbusAuthorizationCodeTokenResponseClient();
            }
    
        }
    
    
        spring:
            security:
                oauth2:
                    client:
                        registration:
                            google:
                                client-id: your-client-id
                                client-secret: your-client-secret
                                redirectUriTemplate: "http://localhost:8080/login/oauth2/code/google"
                                scope:
                                    - email
                                    - profile