gpt4 book ai didi

java - 独立运行 spring boot 应用程序时映射不起作用

转载 作者:行者123 更新时间:2023-11-30 08:07:06 24 4
gpt4 key购买 nike

我在我的项目中使用了spring boot。当我使用 ide 启动应用程序时一切正常,但是当我运行 jar 文件并在浏览器中打开第一页时,我看到以下文本:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

There was an unexpected error (type=Not Found, status=404).
/login.html

Spring Boot 配置类是:

@EnableAutoConfiguration
@EnableTransactionManagement
@EnableJpaAuditing
@EnableCaching
@EnableWebMvc
@EnableWebMvcSecurity
@Import({SecurityLauncher.class})
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class Launcher extends WebMvcConfigurerAdapter {

public static void main(String[] args) {
SpringApplication.run(Launcher.class);
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
}

@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}

@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}

如何解决这个问题?

编辑 -> 项目树

├── src
│   ├── main
│   │   ├── java
│   │   ├── resources
│   │   └── webapp
│   │   ├── app
│   │   │   ├── css
│   │   │   ├── js
│   │   │   │   ├── controllers
│   │   │   │   ├── directives
│   │   │   │   ├── factory
│   │   │   │   ├── filters
│   │   │   │   └── services
│   │   │   ├── lib
│   │   │   ├── resources
│   │   │   ├── templates
│   │   │   └── view
│   │   └── WEB-INF
| | |__ index and login files
│   └── test
│   └── java

编辑 2 -> Spring 安全配置:

public class SecurityLauncher extends WebSecurityConfigurerAdapter {


@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/login/*", "/login.html*").permitAll().antMatchers("/*").authenticated()
.and().formLogin().loginProcessingUrl("/login").loginPage("/login.html")
.passwordParameter("password").usernameParameter("username").defaultSuccessUrl("/").and()
.logout().logoutUrl("/logout").deleteCookies("JSESSIONID").permitAll().and().csrf().disable();
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth, UserSecurityProvider securityProvider) throws Exception {
auth.userDetailsService(securityProvider);
}

}

最佳答案

@SpringBootApplication 正在错误包中扫描 Controller ,但没有找到 Controller ,因此您必须删除@SpringBootApplication 并让@ComponentScan(basePackages = "...") 查找 Controller 以正确扫描。当您启动 login.html 时,您是否在日志中看到映射消息?

关于java - 独立运行 spring boot 应用程序时映射不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33894515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com