Fub*_*000 3 spring-mvc h2 spring-security spring-boot web-console
我的最终目标是在我的应用程序中使用h2db-web-console,作为我本地开发环境的一部分。
我收到错误:
原因:java.lang.IllegalArgumentException:此方法无法确定这些模式是否是 Spring MVC 模式。如果此端点是 Spring MVC 端点,请使用 requestMatchers(MvcRequestMatcher); 否则,请使用 requestMatchers(AntPathRequestMatcher)。
这是因为您的 servlet 上下文中有多个可映射 servlet:{org.h2.server.web.JakartaWebServlet=[/my-h2-console/*], org.springframework.web.servlet.DispatcherServlet=[/] }。
对于每个 MvcRequestMatcher,调用 MvcRequestMatcher#setServletPath 来指示 servlet 路径。
虽然这听起来很有描述性,但它让我发疯,因为看起来我为 JakartaWebServlet=[ /my-h2-console/ *]使用哪条路径并不重要,因为 DispatcherServlet=[ / ] 简单地匹配所有内容。以“/”开头......这就是一切。
...请使用 requestMatchers(MvcRequestMatcher); 否则,请使用 requestMatchers(AntPathRequestMatcher)...
好吧,这些在 Spring 3.xx补丁说明中已被弃用,因此我尝试使用 RequestMatcher(这应该自动与“授权”一起使用)。
安全配置
@Bean
@Order(GENERAL_SECURITY_CONFIGURATION_ORDER)
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http {
...
}
securityMatcher("/**")
headers { frameOptions { disable() } }
csrf { ignoringRequestMatchers("/my-h2-console/**") }
authorizeRequests {
authorize("/my-h2-console/**", permitAll)
authorize("/ping", permitAll)
authorize("/**", denyAll)
}
}
return http.build()
}
Run Code Online (Sandbox Code Playgroud)
pom.xml
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope> <!-- Should be "test", revert later -->
</dependency>
Run Code Online (Sandbox Code Playgroud)
应用程序.yml
spring:
h2:
console:
enabled: true
path: /my-h2-console
settings:
trace: false
web-allow-others: false
datasource:
url: jdbc:h2:mem:testdb
driverClassName: org.h2.Driver
username: sa
password:
jpa:
database-platform: org.hibernate.dialect.H2Dialect
sql:
init:
mode: embedded
schema-locations: classpath:sql/schema.testdb.local.sql
Run Code Online (Sandbox Code Playgroud)
请记住,我对 spring-boot 总体来说是新手,所以请保持温柔。欢迎提供有关此主题的任何信息。:)
我试过:
=> 仅关闭/打开
spring:
h2:
console:
enabled: true
Run Code Online (Sandbox Code Playgroud)
似乎会带来任何改变。
此配置帮助我解决了 H2 问题:
@Configuration
public class SecurityConfig {
// My enpdoints start from /v1 so this pattern is ok for me
private static final String API_URL_PATTERN = "/v1/**";
@Bean
public SecurityFilterChain getSecurityFilterChain(HttpSecurity http,
HandlerMappingIntrospector introspector) throws Exception {
MvcRequestMatcher.Builder mvcMatcherBuilder = new MvcRequestMatcher.Builder(introspector);
http.csrf(csrfConfigurer ->
csrfConfigurer.ignoringRequestMatchers(mvcMatcherBuilder.pattern(API_URL_PATTERN),
PathRequest.toH2Console()));
http.headers(headersConfigurer ->
headersConfigurer.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin));
http.authorizeHttpRequests(auth ->
auth
.requestMatchers(mvcMatcherBuilder.pattern(API_URL_PATTERN)).permitAll()
//This line is optional in .authenticated() case as .anyRequest().authenticated()
//would be applied for H2 path anyway
.requestMatchers(PathRequest.toH2Console()).authenticated()
.anyRequest().authenticated()
);
http.formLogin(Customizer.withDefaults());
http.httpBasic(Customizer.withDefaults());
return http.build();
}
}
Run Code Online (Sandbox Code Playgroud)
更多信息。
更新了 H2 控制台的代码,以便在浏览器中正常工作。
也PathRequest.toH2Console()
可以用来代替AntPathRequestMatcher.antMatcher("/h2-console/**")
归档时间: |
|
查看次数: |
6259 次 |
最近记录: |