@EnableOAuth2Sso doesn't work with spring-cloud-security if you don't configure the oauth2 client
See original GitHub issueI’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:
- Created 8 years ago
- Comments:8 (5 by maintainers)
Top GitHub Comments
Please don’t cross post: http://stackoverflow.com/questions/34003604/enableeurekaclient-cannot-work-with-enableoauth2sso-it-causes-springsecurityfil
Fixed here: https://github.com/spring-cloud/spring-cloud-security/commit/ee8b4cf339299a18cf95701673353d5e34c3eebb.