Spring Boot中CustomerController登录后404,WelcomeController正常访问问题求助
Spring Boot项目中CustomerController访问404且AOP未检测到调用的问题
问题现象
- WelcomeController可正常访问
- 登录Spring Security后访问
http://localhost:8080/Customer/Home返回404 - AOP未检测到CustomerController被调用
相关代码
主应用类
package com.MBS.Consulting.jsp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.Configuration; @Configuration @SpringBootApplication(scanBasePackages="com.MBS.Consulting.jsp") public class SampleWebJspApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SampleWebJspApplication.class); } public static void main(String[] args) throws Exception { SpringApplication.run(SampleWebJspApplication.class, args); } }
WelcomeController(可正常访问)
package com.MBS.Consulting.jsp.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class WelcomeController { @GetMapping("/") public String index() { return "static/index"; } @GetMapping("/Home") public String welcome() { return "Welcome/welcome"; } @GetMapping("/ContactUs") public String contactUs() { return"Welcome/contact_Us"; } @GetMapping("/AboutUs") public String aboutUs() { return "Welcome/about_us"; } }
CustomerController(访问404,AOP无检测)
package com.MBS.Consulting.jsp.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestMapping; import com.MBS.Consulting.jsp.entity.Customers; import com.MBS.Consulting.jsp.entity.Users; import com.MBS.Consulting.jsp.services.CustomersService; @Controller @RequestMapping("/Customer") public class CustomerController { @Autowired private CustomersService customerService; @GetMapping("/Home") public String customerHome() { return "Customer/Customer_Home"; } }
安全配置
package com.MBS.Consulting.jsp.config; import java.util.HashMap; import java.util.Map; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.DelegatingPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder; import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder; import org.springframework.security.web.SecurityFilterChain; @Configuration @EnableWebSecurity public class DemoSecurityConfig { @Autowired @Qualifier("securityDataSource") private DataSource securityDataSource; @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests((auth) -> { try { auth .antMatchers("/Admin/**").hasRole("ADMIN") .antMatchers("/Billing/**").hasAnyRole("ADMIN", "CUSTOMER", "EMPLOYEE") .antMatchers("/Contacts/**").hasAnyRole("ADMIN", "EMPLOYEE") .antMatchers("/Customer/**").hasAnyRole("ADMIN", "CUSTOMER") .antMatchers("/Order/**").hasAnyRole("ADMIN", "CUSTOMER", "EMPLOYEE") .antMatchers("/Plan/**").hasAnyRole("ADMIN", "CUSTOMER", "EMPLOYEE") .antMatchers("/Services/**").hasAnyRole("ADMIN", "CUSTOMER", "EMPLOYEE") .antMatchers("/Welcome", "/Login").permitAll() .and() .formLogin() .and() .logout() .permitAll() .and() .exceptionHandling() .accessDeniedPage("/access-denied"); } catch (Exception e) { e.printStackTrace(); } } ) .httpBasic(); return http.build(); } }
项目结构

已排查内容
- 控制器包路径正确(位于主应用类的子包下)
- 模板映射配置已检查
- 确认DispatcherServlet为Spring Boot自动配置
问题分析与解决方案
1. 验证Spring Security授权逻辑
虽然返回的是404而非访问拒绝页面,但仍需排除角色匹配问题:
- 检查数据库中用户角色是否以
ROLE_为前缀(Spring Security默认要求角色名格式为ROLE_CUSTOMER,而非CUSTOMER) - 开启Spring Security调试日志,查看请求的认证授权流程,确认请求是否被拦截
2. 确认CustomerController是否被Spring注册
查看项目启动日志,搜索RequestMappingHandlerMapping相关内容,如果没有Mapped "{[/Customer/Home],methods=[GET]}"的日志,说明控制器未被扫描注册:
- 检查
CustomerController上的@Controller注解是否正确导入(需为org.springframework.stereotype.Controller) - 验证主应用类
scanBasePackages是否覆盖com.MBS.Consulting.jsp.controller包 - 检查项目编译目录,确认
CustomerController.class文件是否生成(排除编译错误导致类缺失)
3. 检查视图解析配置
控制器返回"Customer/Customer_Home",需确认视图文件路径是否符合配置:
- 若使用JSP,文件应位于
src/main/webapp/WEB-INF/jsp/Customer/Customer_Home.jsp(Spring Boot默认JSP前缀为/WEB-INF/jsp/,后缀为.jsp) - 若使用其他模板引擎(如Thymeleaf),需确认模板文件路径和依赖是否正确
4. 快速验证步骤
- 临时注释Spring Security配置,直接访问
/Customer/Home,排除安全配置干扰 - 修改
CustomerController的路径为@RequestMapping("/test")、@GetMapping("/home"),访问http://localhost:8080/test/home测试是否可达 - 检查AOP切面的切入点表达式,确认是否包含
CustomerController的方法
内容的提问来源于stack exchange,提问作者Que1101
相关产品推荐
相关产品推荐

