当将Spring Boot应用程序部署到Tomcat时,可能会遇到请求被拒绝的问题,并显示错误消息“URL没有被规范化”。这通常是由于应用程序中的URL路径不符合Tomcat的规范化要求导致的。以下是解决此问题的一些方法和代码示例:
确保应用程序中的URL路径使用斜杠(/)作为路径分隔符。例如,正确的URL路径格式应为“/path/to/resource”,而不是“\path\to\resource”。
使用Spring Boot的UriComponentsBuilder
类来构建URL路径,以确保生成的URL符合规范化要求。例如:
import org.springframework.web.util.UriComponentsBuilder;
// 构建URL路径
String path = UriComponentsBuilder.fromPath("/path/to/resource")
.build().toUriString();
application.properties
或application.yml
)中,配置server.servlet.context-path属性,并设置为应用程序的上下文路径。例如:在application.properties
中:
server.servlet.context-path=/myapp
在application.yml
中:
server:
servlet:
context-path: /myapp
这将确保应用程序的URL路径以配置的上下文路径为前缀,并符合Tomcat的规范化要求。
WebSecurityConfigurerAdapter
的子类中添加以下配置:import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/path/to/resource").permitAll() // 允许访问特定URL路径
.anyRequest().authenticated(); // 其他URL路径需要身份验证
// 其他安全配置...
}
}
请注意,上述代码示例仅提供了一些解决方法的示例,具体的解决方法可能因应用程序的特定配置和要求而有所不同。