Spring引导安全性无法在Google App Engine

时间:2018-04-02 22:07:46

标签: google-app-engine spring-boot spring-security

在寻找解决方案失败后,我终于放弃了。我正在尝试部署一个使用Spring Boot和Spring Security的测试应用程序。当我在本地运行我的应用程序时,一切都运行得很好 - 没有错误或异常,应用程序很好地处理弹簧安全性。

我使用Gradle构建项目,然后将其部署到Google App Engine。在部署之前,我尝试使用Google App Engine Gradle插件在本地运行我的项目,更具体地说,使用appengineRun Gradle任务。

执行此特定任务后,我的应用程序正常启动,但是当我尝试访问" /"在我的localhost上,出现403错误。我的项目中没有web.xml配置文件,但我不认为这是问题的关键。我有一个appengine-web.xml文件,这是Google App Engine所必需的。

如何让我的应用程序使用此特定App Engine任务,我该怎么做?我的主要目标是在GAE上部署Spring Boot应用程序并使Spring Security正常工作。

我包括我的java类,build.gradle文件和WEB-INF内容:

build.gradle:

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.cloud.tools:appengine-gradle-plugin:+'
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.0.RELEASE")

    }
}
repositories {

    maven {
        url 'https://oss.sonatype.org/content/repositories/snapshots'
        mavenCentral()
        jcenter()
    }

    apply plugin: 'java'
    apply plugin: 'war'
    apply plugin: 'com.google.cloud.tools.appengine'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    bootJar {
        baseName = 'gs-spring-boot'
        version = '0.1.0'
    }

    dependencies {
        compile group: 'com.google.appengine', name: 'appengine-api-1.0-sdk', version: '1.9.63'
        providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
        compile 'jstl:jstl:1.2'
        compile("org.springframework.boot:spring-boot-starter-web")
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '2.0.0.RELEASE'
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.0.0.RELEASE'
        compile 'com.google.cloud:google-cloud:+'
        testCompile 'junit:junit:4.12'
        testCompile 'com.google.truth:truth:0.33'
        testCompile 'org.mockito:mockito-all:1.10.19'
        testCompile 'com.google.appengine:appengine-testing:+'
        testCompile 'com.google.appengine:appengine-api-stubs:+'
        testCompile 'com.google.appengine:appengine-tools-sdk:+'
    }

    appengineDeploy.dependsOn test
    appengineStage.dependsOn test

    appengine {
        run {
            port = 8090
        }

        deploy {   // deploy configuration
        }
    }

    test {

        useJUnit()
        testLogging.showStandardStreams = true
        beforeTest { descriptor ->
            logger.lifecycle("test: " + descriptor + "  Running")
        }

        onOutput { descriptor, event ->
            logger.lifecycle("test: " + descriptor + ": " + event.message)
        }
        afterTest { descriptor, result ->
            logger.lifecycle("test: " + descriptor + ": " + result)
        }
    }

    group = "com.example.appenginej8"
    version = "1.0.0-SNAPSHOT"

    sourceCompatibility = 1.8
    targetCompatibility = 1.8
}

应用服务引擎-web.xml中:

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <version>1</version>
    <threadsafe>true</threadsafe>
    <runtime>java8</runtime>
</appengine-web-app>

Application.java:

@SpringBootApplication
@ComponentScan("packages")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

MyController.java:

@RestController
public class MyController {


    @RequestMapping("/")
    public String hello() {
        return "test";
    }
}

SecurityConfig.java:

@EnableWebSecurity
public class SecurityConfig {

        @Bean
        public UserDetailsService userDetailsService() {
            InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
            manager.createUser(User.withDefaultPasswordEncoder().username("user").password("password").roles("ADMIN").roles("ACTUATOR").build());
            return manager;
        }
}

1 个答案:

答案 0 :(得分:4)

你需要SpringBootServletInitializer神奇地将Spring Boot功能作为传统的Servlet WAR。

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
  }

}

这应该使它基本上起作用。我注意到在添加这个类之后你仍然会遇到一个不同但很小的问题,但修复它应该是微不足道的。