java - Spring Boot 应用程序可以在调试器中运行,但不能作为 jar 运行

标签 java spring spring-boot

我已经研究了几个小时但无法弄清楚。我在 Windows 上有一个 Spring Boot 应用程序。标准设置:一个公共(public)登录页面,然后是一堆经过身份验证的路由。这些路由转到客户端的 React Router - 我不知道这是否相关。

该应用程序在 Eclipse 调试器中运行得非常出色。我正在尝试将其部署为 jar,因此我调用

mvnw clean package
java -jar target/[app].jar

它运行并显示登录屏幕。它正确地拒绝了错误的登录,一切看起来都很好。但是,当我输入正确的凭据并进行身份验证时,它就会卡在主屏幕上,并显示一堆“404”错误,就好像它找不到任何脚本、CSS 等。公共(public)登录屏幕没有问题找到它们因为它显示图像并运行脚本正常。命令窗口不显示任何其他输出。

我相当确定它与身份验证故障有关,但为什么它在从 Eclipse 运行时可以工作,但不能作为打包的 jar 运行?

很难知道要包含哪些片段,但我将从这些开始。来 self 的 POM:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.lmco.energy.web2.WebApplication</mainClass>
                    <addResources>true</addResources>
                </configuration>

                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
               </executions>
            </plugin>
        </plugins>
    </build>

我的 WebSecurityConfig 类:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/login").permitAll()
                .antMatchers("/admin/**").hasRole("admin")
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .successHandler(new RoleBasedAuthenticationSuccessHandler())
                .permitAll()
                .and()
            .csrf()
                .ignoringAntMatchers("/api/**", "/login")
                .and()
            .logout()
                .logoutSuccessUrl("/login")
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .deleteCookies("JSESSIONID")
                .invalidateHttpSession(true);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**", "/css/**", "/js/**", "/images/**", "/fonts/**");
    }

    @Bean
    public UserDetailsService userDetailsService() {
      return new UserDetailsServiceImp();
    };

    @Bean
    public DaoAuthenticationProvider authProvider() {
        DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
        authProvider.setUserDetailsService(userDetailsService());
        authProvider.setPasswordEncoder(new CustomPasswordEncoder());
        return authProvider;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authProvider());
    }
}

最佳答案

终于明白了!我希望它可以帮助任何将来为此苦苦挣扎的人......

@ptomli 很接近。尽管返回 404 错误的资源的 URL 与我从 Eclipse 调试器运行 Web 应用程序时使用的 URL 完全相同,但存在大小写差异。特别是一个名为“/ThirdParty”的子文件夹,但在 URL 中引用为“thirdParty”,它导致了大量错误,并导致我认为无法找到所有资源。虽然 url 通常不区分大小写,但我想这里的故事的寓意是,当它们捆绑到 jar 中时,路径会变得区分大小写。最终,身份验证方面是一个转移注意力的问题。

关于java - Spring Boot 应用程序可以在调试器中运行,但不能作为 jar 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58822827/

相关文章:

spring-boot - 当 Flyway 在 Spring Boot 上不起作用时如何调试?

spring-boot - 如何避免使用 Zuul 重定向到另一个主机?

java - 如何在Android中将日期时间转换为十六进制

java - 如何为 Karate 框架指定 SNI header

java - 如何使用 Cucumber runner 加载 Spring 应用程序上下文

Spring JUnit : java. lang.NoSuchFieldError: NULL

java - List<String> 的列表到字符串有序的字符串数组

带状态的 Java Streams - 一个简单的练习

spring - 访问 Web 应用程序包中的 Spring 配置文件

java - 在java中发送邮件需要太多时间,我该如何克服这个问题