如何在我在Spring ElasticBeanstalk和Nginx上使用OAuth2的Spring Boot应用程序上强制使用SSL?

Chl*_*loe 7 spring amazon-web-services spring-boot amazon-elastic-beanstalk

我正在尝试使用参考文档强制使用SSL

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-security.html#howto-enable-https

但是,我已经有了

@Configuration
class WebSecurityConfiguration  {
Run Code Online (Sandbox Code Playgroud)

当我添加extends WebSecurityConfigurerAdapter,甚至不添加时,protected void configure(HttpSecurity http)对非Oauth2页面的请求将被无/home/方向重定向/login.它适用于属性设置.只需通过扩展类extends WebSecurityConfigurerAdapter打破应用程序.OAuth2还有其他不相关的路由.我在设置Oauth2之前已经看到过这种非确定性的随机行为.

这是WebSecurityConfiguration班级的概要.

@Configuration
class WebSecurityConfiguration {

    @Autowired
    UserMapper userMapper;

    @Bean
    PasswordEncoder passwordEncoder() {

    @Bean
    protected UserDetailsService userDetailsService() {
Run Code Online (Sandbox Code Playgroud)

就是这样.

我试着添加一个Nginx配置来重定向到SSL,在这个答案/sf/answers/3731769121/,但它没有用.它确实重定向到SSL,但我得到所有路径的404错误

HTTP状态404 - /home
类型状态报告
消息/ home
描述请求的资源不可用.
Apache Tomcat/8.0.47

tomcat 404

因此它强制使用SSL并访问Tomcat,但Spring Boot应用程序完全搞砸了.就好像ZIP中的WAR文件从未部署过一样.

参考:https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-tomcat-proxy.html#java-tomcat-proxy-nginx

Chl*_*loe 2

为此我放弃了使用 Spring Boot,因为它太脆弱了,转而采用了 Nginx 配置选项。这很有效,尽管对于仅仅制作 ZIP 来说似乎过于冗长。Elastic Beanstalk 中还存在一个错误问题!

AWS Elastic Beanstalk Tomcat 适用于 .war 但不适用于 .zip

部署ZIP时,不会部署WAR!所以我必须创建一个解决方法来在 ZIP 中创建两个WAR 文件。(只有一个,即使称为ROOT.war,也是行不通的。)

我找不到使用 Maven 创建空文件的方法,因此我empty.war在项目根目录中创建了一个空文件,并将其捆绑在 ZIP 中,以欺骗 Elastic Beanstalk 正常工作和部署应用程序。真是一团糟!OY合租!

pom.xml
        <plugin> <!-- To add .ebextensions/ Nginx config for ElasticBeanstalk -->
          <artifactId>maven-assembly-plugin</artifactId>
          <configuration>
            <descriptors>
              <descriptor>assembly.xml</descriptor>
            </descriptors>
          </configuration>
          <executions>
            <execution>
              <id>make-assembly</id>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>
        </plugin>           
Run Code Online (Sandbox Code Playgroud) assembly.xml
<assembly 
  xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
  <id>bin</id>
  <baseDirectory>/</baseDirectory>
  <formats>
    <format>zip</format>
  </formats>
  <files>
    <file>
      <source>empty.war</source>
      <outputDirectory/>
    </file>
    <file>
      <source>${project.build.directory}/AppName-0.0.3-SNAPSHOT.war</source>
      <outputDirectory/>
      <destName>ROOT.war</destName>
    </file>
  </files>

  <fileSets>
    <fileSet>
      <directory>${project.basedir}</directory>
      <outputDirectory>/.ebextensions/nginx/conf.d/elasticbeanstalk/</outputDirectory>
      <includes>
        <include>force-https.conf</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>
Run Code Online (Sandbox Code Playgroud)

配置文件就在项目根目录中。我不知道还能把它放在哪里——它不是源代码。

force-ssl.conf
if ($http_x_forwarded_proto = 'http') {
    return 301 https://$host$request_uri;
}
Run Code Online (Sandbox Code Playgroud)

http://maven.apache.org/plugins/maven- assembly-plugin/ assembly.html