Wildfly 34.0.0.Beta1中POST方法不支持问题求助
Spring Boot集成Spring Security部署到Wildfly后POST请求返回405错误
将集成Spring Security的Spring Boot应用部署到最新版本Wildfly后,调用POST接口时返回405错误,提示:该URL不支持POST方法。以下是相关配置及问题修复方案:
相关配置
WebSecurityConfig
Customizer<LogoutConfigurer<HttpSecurity>> logoutConfigurerCustomizer = httpSecurityLogoutConfigurer -> { httpSecurityLogoutConfigurer.deleteCookies("JSESSIONID"); httpSecurityLogoutConfigurer.permitAll(); }; Customizer<SessionManagementConfigurer<HttpSecurity>> sessionManagementConfigurerCustomizer = httpSecuritySessionManagementConfigurer -> httpSecuritySessionManagementConfigurer.maximumSessions(1); http .csrf(AbstractHttpConfigurer::disable) .exceptionHandling(exception -> exception.authenticationEntryPoint(unauthorizedHandler)) .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .authorizeHttpRequests(auth -> auth.requestMatchers("/prog/auth/**").permitAll() .requestMatchers("/prog/url/**").hasAnyAuthority("AUTH1","AUTH2")
Controller
@RestController @RequestMapping("/auth") public class AuthController { //... @CrossOrigin(origins = "*") @PostMapping("/signin") public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) { logger.info("Login"); try { authenticationManager.authenticate( new UsernamePasswordAuthenticationToken( loginRequest.getUsername(), loginRequest.getPassword() ) ); //...
jboss-web.xml
<?xml version="1.0" encoding="UTF-8"?> <jboss-web xmlns="http://www.jboss.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd"> <context-root>/sat</context-root> </jboss-web>
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- ~ Copyright (c) 2023. Marco Sciuto ITA for Challenging. All rights reserved. --> <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> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>3.3.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <groupId>it.test</groupId> <artifactId>prog</artifactId> <version>v1</version> <packaging>war</packaging> <name>prog</name> <description>PROJECT</description> <properties> <java.version>21</java.version> <full-artifact-name>target/${project.artifactId}-${project.version}.jar</full-artifact-name> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <exclusions> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <exclusions> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> <exclusions> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> <exclusions> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-api</artifactId> <version>0.11.5</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-impl</artifactId> <version>0.11.5</version> <scope>runtime</scope> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-jackson</artifactId> <version>0.11.5</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <dependency> <groupId>jakarta.servlet.jsp</groupId> <artifactId>jakarta.servlet.jsp-api</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.14.0</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>webjars-locator-core</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-jdbc</artifactId> </dependency> <dependency> <groupId>org.jetbrains</groupId> <artifactId>annotations</artifactId> <version>24.0.1</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.5</version> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.12.0</version> </dependency> <dependency> <groupId>io.vavr</groupId> <artifactId>vavr</artifactId> <version>0.10.4</version> </dependency> <dependency> <groupId>org.threeten</groupId> <artifactId>threeten-extra</artifactId> <version>1.8.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.4.0</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>21</source> <target>21</target> </configuration> </plugin> <plugin> <groupId>org.wildfly.plugins</groupId> <artifactId>wildfly-maven-plugin</artifactId> <version>5.0.0.Final</version> </plugin> </plugins> </build> </project>
测试请求与错误
请求URL:
http://localhost:8080/prog/auth/signin
请求体:
{ "password": "password", "username": "username" }
错误信息:HTTP method POST is not supported by this URL(该URL不支持POST方法)
问题修复方案
1. 修正上下文路径匹配
jboss-web.xml中配置的上下文路径是/sat,但测试请求使用了/prog,导致请求无法定位到正确的Controller。
解决方式二选一:
- 修改请求URL为正确路径:
http://localhost:8080/sat/auth/signin - 或修改jboss-web.xml的上下文路径为
/prog:
<context-root>/prog</context-root>
2. 修正Spring Security路径配置
Spring Security的路径配置是相对于应用上下文的,无需包含容器级别的上下文路径。修改WebSecurityConfig的权限规则:
http .csrf(AbstractHttpConfigurer::disable) .exceptionHandling(exception -> exception.authenticationEntryPoint(unauthorizedHandler)) .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .authorizeHttpRequests(auth -> auth.requestMatchers("/auth/**").permitAll() .requestMatchers("/url/**").hasAnyAuthority("AUTH1","AUTH2") .anyRequest().authenticated() // 补充默认权限规则,避免配置不完整 ) .logout(logoutConfigurerCustomizer); // 补全logout配置
注意:移除冲突的
maximumSessions(1)配置——无状态模式下不会创建会话,该配置无意义且会导致Spring Security初始化异常。
3. 补全不完整的配置
原WebSecurityConfig代码中authorizeHttpRequests的lambda表达式未闭合,且缺少默认的权限规则,需补充.anyRequest().authenticated()保证配置完整性。
内容的提问来源于stack exchange,提问作者Marco Sciuto
相关产品推荐
相关产品推荐

