Spring Security中requestMatchers传字符串编译报错求助
Spring Security requestMatchers传字符串编译失败问题
在使用Spring Security的authorize.requestMatchers方法传入字符串参数时出现编译不通过的问题,刚接触Spring Security,尚未找到解决方案,以下是相关代码及配置:
安全配置类代码
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; @Configuration @EnableWebSecurity public class SpringSecurity { @Autowired private UserDetailsService userDetailsService; @Bean public static PasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); } @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http.csrf().disable() .authorizeHttpRequests((authorize) -> authorize.requestMatchers("/register/**").permitAll() .requestMatchers("/index").permitAll() .requestMatchers("/users").hasRole("ADMIN") ).formLogin( form -> form.loginPage("/login") .loginProcessingUrl("/login") .defaultSuccessUrl("/users") .permitAll() ).logout( logout -> logout.logoutRequestMatcher(new AntPathRequestMatcher("/logout")) .permitAll() ); return http.build(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .userDetailsService(userDetailsService) .passwordEncoder(passwordEncoder()); } }
Pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xxxxxxxxx</groupId> <artifactId>xxxxxxxxxxxxxxx</artifactId> <version>0.0.1-SNAPSHOT</version> <name>xxxxxxxxxxxxxx</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> <squiggly.version>1.3.18</squiggly.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <dependency> <groupId>org.firebirdsql.jdbc</groupId> <artifactId>jaybird-jdk17</artifactId> <version>3.0.10</version> </dependency> <dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna</artifactId> <version>5.5.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> </dependency> <dependency> <groupId>org.hibernate.orm</groupId> <artifactId>hibernate-community-dialects</artifactId> <version>6.0.0.Alpha9</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>com.github.bohnman</groupId> <artifactId>squiggly-filter-jackson</artifactId> <version>1.3.18</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
解决方案
编译失败的核心原因是Lambda表达式的箭头符号错误:原代码中使用了单个减号-,而正确的Lambda箭头应该是->。修正所有Lambda处的箭头符号后,代码即可正常编译。
另外,Spring Boot 2.6.2对应的Spring Security版本是5.6.x,该版本支持requestMatchers直接传入字符串路径,无需额外转换为AntPathRequestMatcher,因此修正箭头后的代码逻辑完全可行。
内容的提问来源于stack exchange,提问作者Paulo Cesar Pereira da costa
相关产品推荐
相关产品推荐

