Ensure JPA/Security are not fighting with each other on startup
This is *really* nasty (and led me to discover a related bug https://jira.spring.io/browse/SPR-11844), but fortunately easy to hide from users once you have a test case. The problem is that Spring Security registers a `BeanPostProcessor` to handle `GlobalAuthenticationConfigurerAdapters`, and Boot registers a `BeanPostProcessor` to handle injecting the packages to scan into an `EntityManagerFactory` from `@EntityScan`. The clash comes because the `EntityScanBeanPostProcessor` wants to be postprocessed by the Security postprocessor, but if the Security configuration depends on JPA it won't be ready in time. The fix (or workaround) depending on how you look at it is to prevent the other bean post processors from taking an interest in `EntityScanBeanPostProcessor` at all (mark it as synthetic). Fixes gh-1008pull/1016/head
parent
49a09c807c
commit
1f82ef4deb
@ -0,0 +1,31 @@
|
||||
package org.springframework.boot.autoconfigure.security.jpa;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* The EntityScanRegistrar can cause problems with Spring security and its eager
|
||||
* instantiation needs. This test is designed to fail if the Entities can't be scanned
|
||||
* because the registrar doesn't get a callback with the right beans (essentially because
|
||||
* their instantiation order was accelerated by Security).
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
* @since 1.1
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Main.class)
|
||||
public class JpaUserDetailsTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(Main.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.security.jpa;
|
||||
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.user.SecurityConfig;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@Import({ DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class, SecurityAutoConfiguration.class })
|
||||
@ComponentScan(basePackageClasses = SecurityConfig.class)
|
||||
public class Main {
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.security.user;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.orm.jpa.EntityScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||
|
||||
@Configuration
|
||||
@EnableGlobalMethodSecurity(securedEnabled = true)
|
||||
@EntityScan(basePackageClasses = User.class)
|
||||
@EnableJpaRepositories(basePackageClasses = User.class)
|
||||
public class SecurityConfig extends GlobalAuthenticationConfigurerAdapter {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Override
|
||||
public void init(AuthenticationManagerBuilder auth) throws Exception {
|
||||
System.err.println("Global security config");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package org.springframework.boot.autoconfigure.security.user;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String email;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() + ":" + id;
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package org.springframework.boot.autoconfigure.security.user;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Integer> {
|
||||
public User findByEmail(String email);
|
||||
}
|
Loading…
Reference in New Issue