question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@EnableOAuth2Sso doesn't work with spring-cloud-security if you don't configure the oauth2 client

See original GitHub issue

I’m using spring-cloud-starter-parent version Brixton.M3 to test spring security oauth2. Everything works fine before I enable Eureka client.

After enable Eureka client, it reported error as below,

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.0.RELEASE:run (default-cli) on project spring-security-oauth2-brixton-demo: An exception occured while running. null: InvocationTargetException: 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.security.config.annotation.AlreadyBuiltException: This object has already been built -> [Help 1]

My maven pom.xml is

<?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>

    <parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>Brixton.M3</version>
        <relativePath />
    </parent>

    <artifactId>spring-security-oauth2-brixton-demo</artifactId>
    <groupId>com.gaoshin</groupId>
    <packaging>jar</packaging>

    <properties>
        <start-class>com.gaoshin.Application</start-class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>http://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

Application.java

package com.gaoshin;

import java.security.Principal;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableOAuth2Sso
@RestController
@EnableEurekaClient
@RequestMapping(produces=MediaType.APPLICATION_JSON_VALUE)
public class Application {
    @RequestMapping(value="/", method = RequestMethod.GET)
    public String hi(Principal p) {
        return p!=null ? "Hello " + p.getName() : "Hello guest";
    }

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

ResourceServer.java

package com.gaoshin;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;

@Configuration
@EnableResourceServer 
public class ResourceServer extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.headers().cacheControl().disable();
        http.authorizeRequests().antMatchers("/**").permitAll();
    }

}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

github_iconTop Results From Across the Web

OAuth2ClientContext (spring-security-oauth2) not persisted in ...
I post here the configuration which can be used to setup the RequestContextFilter and enable spring-session persistence of ...
Read more >
OAuth2 - @EnableResourceServer vs @EnableOAuth2Sso
Learn how to set up an OAuth2 Client and Resource Server using Spring's @EnableResourceServer and @EnableOAuth2Sso annotations.
Read more >
Spring Cloud Security
If your app is a user facing OAuth2 client (i.e. has declared @EnableOAuth2Sso or @EnableOAuth2Client ) then it has an OAuth2ClientContext in request...
Read more >
Secure a Spring Microservices Architecture ... - Okta Developer
If you don't have one yet, create a forever-free Okta Developer ... security.oauth2.client.scope=openid profile email security.oauth2.
Read more >
Google Sign-In with Spring Security (no Spring Boot)
It only does some basic configuration and it does not do the whole functionality provided by @EnableOAuth2Sso. The work delivered with the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found