mrr*_*337 1 google-app-engine spring-security spring-boot
在寻找解决方案失败之后,我终于放弃了。我正在尝试部署一个使用Spring Boot和Spring Security的测试应用程序。当我在本地运行我的应用程序时,一切都可以正常工作-没有错误或异常,并且该应用程序可以很好地处理spring安全性。
我Gradle用来构建项目,然后将其部署到Google App Engine。部署之前,我尝试使用Google App Engine Gradle插件在本地运行我的项目,更具体地说,是使用appengineRunGradle任务。
执行完此特定任务后,我的应用程序正常启动,但是当我尝试在本地主机上访问“ /”时,出现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
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
Application.java:
@SpringBootApplication
@ComponentScan("packages")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
MyController.java:
@RestController
public class MyController {
@RequestMapping("/")
public String hello() {
return "test";
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
您需要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);
}
}
Run Code Online (Sandbox Code Playgroud)
这应该使其基本工作。我注意到添加此类后,您仍然会遇到一个不同但次要的问题,但是修复起来应该很简单。
| 归档时间: |
|
| 查看次数: |
936 次 |
| 最近记录: |