【问题标题】:Spring boot security does not work on Google App EngineSpring Boot 安全性在 Google App Engine 上不起作用
【发布时间】:2018-04-02 22:07:46
【问题描述】:

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

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

执行此特定任务后,我的应用程序正常启动,但当我尝试访问本地主机上的“/”时,出现 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
}

appengine-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;
        }
}

【问题讨论】:

  • 有人帮忙,请:)
  • 我认为这与 Spring Boot Security 无关。我完成删除了SecurityConfig 类,本地开发服务器仍然返回403。

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


【解决方案1】:

您需要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);
  }

}

这应该使它基本上可以工作。我注意到你在添加这个类之后仍然会遇到一个不同但很小的问题,但它应该很容易修复。

【讨论】:

  • @mrrobot1337,如果您认为 Chanseok 的回答对您有效,请考虑接受。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-12
  • 2011-06-29
  • 2019-12-18
  • 1970-01-01
  • 2021-05-24
相关资源
最近更新 更多