访问上下文路径(根文件夹)在 Spring Boot 应用程序中不显示登录页面
Posted
技术标签:
【中文标题】访问上下文路径(根文件夹)在 Spring Boot 应用程序中不显示登录页面【英文标题】:Accessing context path(root folder) does not show login page in Spring Boot Application 【发布时间】:2020-06-24 12:23:50 【问题描述】:在我的 Spring Boot(2.0) 应用程序中,我在 application.properties
文件中设置了上下文路径,如下所示
server.servlet.context-path=/myApp
另外,我有以下安全配置类扩展 WebSecurityConfigurerAdapter
@Override
protected void configure(HttpSecurity http) throws Exception
// TODO Auto-generated method stub
http
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.GET,"/uifiles/dist/css/**").permitAll()
.antMatchers(HttpMethod.GET,"/uifiles/plugins/icheck-bootstrap/**").permitAll()
.antMatchers(HttpMethod.GET,"/uifiles/plugins/fontawesome-free/css/**").permitAll()
.antMatchers(HttpMethod.GET,"/css/**").permitAll()
.antMatchers(HttpMethod.GET, "/uifiles/**").permitAll()
.antMatchers(HttpMethod.GET, "/error/**").permitAll()
.antMatchers(HttpMethod.GET,"/files/**").permitAll()
.antMatchers(HttpMethod.GET,"/files/fonts/**").permitAll()
.antMatchers(HttpMethod.GET,"/files/images/**").permitAll()
.and()
.authorizeRequests().antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.successForwardUrl("/home")
.defaultSuccessUrl("/home")
.permitAll()
.and()
.logout()
.invalidateHttpSession(true)
.clearAuthentication(true)
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login")
.and()
.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint());
当我运行应用程序(来自 Spring Tool Suit)并通过 url 访问应用程序时
http://localhost:8080/myApp/login
它工作正常并打开登录页面
但是,当我进入
http://localhost:8080/myApp/
它将我重定向到 http://localhost:8080/login (这给出了一个 404 页面)
两个都想要 http://localhost:8080/myApp/ & http://localhost:8080/myApp/login 打开登录页面
登录页面位于项目文件夹的根目录中。
另外,我的 application.properties 中有spring.mvc.view.suffix=.jsp
。所以控制器会自动为请求添加文件扩展名。
注销功能工作正常,它重定向到 http://localhost:8080/myApp/login
Spring Boot 2.1.7 Tomcat 9.0.21 JDK 1.8【问题讨论】:
能否添加控制器代码。 您是否使用任何反向代理? 【参考方案1】:能否调试代码在类中添加调试点
org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint
方法名称buildRedirectUrlToLoginPage
。很容易找出问题所在。
【讨论】:
很遗憾,由于当前的 COVID-19 情况,我有几天无法检查。一旦情况缓和,就会回来。【参考方案2】:对此一无所知 - 我隐约记得这是多年前的绝对重定向。
尝试“$server.servlet.context-path/login”作为 loginPage 的值。
如果它有效 - 很好,如果不是,我会尝试更深入地研究它,看看我能找到什么。
【讨论】:
很遗憾,由于当前的 COVID-19 情况,我有几天无法检查。一旦情况缓和,就会回来。【参考方案3】:您是否尝试过更改:
.loginPage("/login")
到
.loginPage("/login.html")
【讨论】:
我在根文件夹中有login.jsp
页面,在我的application.properties 文件中有spring.mvc.view.suffix=.jsp
。所以当我调用.loginPage("/login")
时,它会调用 login.jsp。以上是关于访问上下文路径(根文件夹)在 Spring Boot 应用程序中不显示登录页面的主要内容,如果未能解决你的问题,请参考以下文章
我的javaEE项目的spring mvc controller里如何获取某个文件夹的路径呢?
无法从子 WebApplicationContext 访问根 ApplicationContext 中配置的 Spring 拦截器