我有一个使用@PreAuthorize进行安全检查的spring boot应用程序。如果我通过执行gradle任务bootRun启动验证,则验证在我的应用程序中有效。如果我从Spring Tool Suite(使用标准运行配置)启动应用程序,那么安全检查将无法进行。
我认为问题是从STS开始时无法识别PreAuthorize批注。我可以使用与我的实际应用程序相同的继承层次结构,通过一个简单的gradle spring boot应用程序来重现它。
以下是相关代码:
public class DemoEntity extends BaseDemo {}
public class BaseDemo extends AbstractBaseEntity {}
public abstract class AbstractBaseEntity {}
public class DemoService extends AbstractService<BaseDemo, DemoEntity> implements IDemoService {
@Override
@DemoAnnotation
public void create(DemoEntity entity) {}
}
public interface IDemoService extends IEntityService<DemoEntity> {}
abstract class AbstractService<B extends AbstractBaseEntity, E extends B> implements IEntityService<E> {
@Override
public void create(E entity) {
throw new UnsupportedOperationException();
}
}
public interface IEntityService<E> {
void create(E entity);
}
@Retention(RetentionPolicy.RUNTIME)
public @interface DemoAnnotation {}
注意:
我用以下代码打印方法及其注释。
@SpringBootApplication
public class AnnotationDemoApplication {
public static void main(String[] args) {
SpringApplication.run(AnnotationDemoApplication.class, args);
for (final Method method : DemoService.class.getMethods()) {
if (method.getName().contains("create")) {
System.out.println("Method: " + method.toString());
final Annotation[] annotations = method.getAnnotations();
System.out.println(" #Annotations: " + annotations.length);
for (final Annotation annotation : annotations) {
System.out.println(" Annotation: " + annotation.toString());
}
System.out.println();
}
}
}
}
从gradle任务bootRun开始时,我得到以下结果:
Method: public void com.example.annotationdemo.DemoService.create(java.lang.Object)
#Annotations: 1
Annotation: @com.example.annotationdemo.DemoAnnotation()
Method: public void com.example.annotationdemo.DemoService.create(com.example.annotationdemo.AbstractBaseEntity)
#Annotations: 1
Annotation: @com.example.annotationdemo.DemoAnnotation()
Method: public void com.example.annotationdemo.DemoService.create(com.example.annotationdemo.DemoEntity)
#Annotations: 1
Annotation: @com.example.annotationdemo.DemoAnnotation()
在继承层次结构中,每个方法都有注释。 从STS启动应用程序时,结果为:
Method: public void com.example.annotationdemo.DemoService.create(com.example.annotationdemo.DemoEntity)
#Annotations: 1
Annotation: @com.example.annotationdemo.DemoAnnotation()
Method: public void com.example.annotationdemo.DemoService.create(com.example.annotationdemo.AbstractBaseEntity)
#Annotations: 0
Method: public void com.example.annotationdemo.AbstractService.create(java.lang.Object)
#Annotations: 0
我认为这个演示在真实的应用程序中再现了我的问题的原因。 我是使用Spring Boot 2.2.0,Java 11和没有其他依赖项的spring initializr创建的项目。
有人可以告诉我,为什么STS除了运行bootRun还会产生其他结果? 我是否需要在STS中添加参数或设置,使其行为与bootRun相同?