生产用MySQL、开发用H2时出现Whitelabel 403禁止错误
我在Spring Boot项目中配置了生产环境用MySQL、开发环境用H2,现在切换到MySQL环境(Eclipse中设置运行环境)后,访问页面出现Whitelabel错误页,提示:
This application has no explicit mapping for /error, so you are seeing this as a fallback. There was an unexpected error (type=Forbidden, status=403).
Forbidden.
开发环境(H2)application.properties
##H2 CONSOLE - DEVELOPMENT spring.h2.console.path=/h2-console spring.datasource.url=jdbc:mysql://localhost:3306/reusablerocket spring.datasource.username=root spring.datasource.password=1234 spring.jpa.database=h2 spring.h2.console.enabled=true spring.jpa.show-sql=true
生产环境(MySQL)application.properties
##MYSQL DATABASE - PRODUCTION spring.jpa.database=mysql spring.datasource.url=mysql://localhost:3306/reusablerocket spring.datasource.username=root spring.datasource.password=1234
SecurityConfig代码
@Configuration public class SecurityConfig { @Autowired private DataSource dataSource; @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Bean public UserDetailsService userDetailsService() { InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager(); userDetailsManager.createUser(User.withUsername("pablocarmona1@hotmail.com") .password(passwordEncoder().encode("1234")) .roles("USER") .build()); return userDetailsManager; } @Bean public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception { return authConfig.getAuthenticationManager(); } @Bean public DaoAuthenticationProvider authenticationProvider(UserDetailsService userDetailsService) { DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider(); authProvider.setUserDetailsService(userDetailsService); authProvider.setPasswordEncoder(passwordEncoder()); return authProvider; } @Bean public AccessDeniedHandler accessDeniedHandler() { return new AccessDeniedHandlerImpl(); } @Bean public SecurityFilterChain filterChain(HttpSecurity http, AccessDeniedHandler accessDeniedHandler) throws Exception { http.authorizeRequests() .requestMatchers("/secure/**").hasRole("USER") .requestMatchers("/", "/js/**", "/css/**", "/images/**", "/**").permitAll() .anyRequest().authenticated() .and().formLogin() .loginPage("/login").permitAll() .and().logout() .invalidateHttpSession(true) .clearAuthentication(true) .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) .logoutSuccessUrl("/login?logout").permitAll() .and().exceptionHandling() .accessDeniedHandler(accessDeniedHandler); http.authenticationProvider(authenticationProvider(userDetailsService())); http.headers().frameOptions().sameOrigin(); return http.build(); } }
我已经尝试过修改配置、移除依赖、调整账号密码,但问题依然存在,求解决办法。
问题排查与解决方案
1. 修复MySQL数据源URL配置
你的MySQL配置文件中spring.datasource.url缺少jdbc:前缀,正确格式应为:
spring.datasource.url=jdbc:mysql://localhost:3306/reusablerocket
该错误会导致数据源初始化失败,进而引发Spring Security权限拦截异常。
2. 调整Security规则顺序
当前authorizeRequests中,/**的permitAll规则会覆盖所有前置规则,包括/secure/**的角色校验和后续的anyRequest().authenticated()。需调整为具体路径优先:
http.authorizeRequests() .requestMatchers("/secure/**").hasRole("USER") .requestMatchers("/login", "/js/**", "/css/**", "/images/**").permitAll() .anyRequest().authenticated()
移除"/"和"/**"的permitAll配置,确保受保护路径和全局认证规则生效。
3. 修正H2开发环境配置
H2配置中错误使用了MySQL数据源URL,应改为H2嵌入式数据库格式:
spring.datasource.url=jdbc:h2:mem:reusablerocket
保证开发环境数据源与H2数据库匹配。
4. 验证认证流程
访问/secure/**路径前,需先通过/login页面完成登录。若登录后仍报403,检查用户角色配置:你设置的roles("USER")对应权限为ROLE_USER,hasRole("USER")会自动添加前缀,配置本身无问题。
5. 检查MySQL连接状态
确认MySQL服务已启动,reusablerocket数据库已创建,root账号拥有该数据库的访问权限,可通过MySQL客户端直接测试连接是否正常。
内容的提问来源于stack exchange,提问作者Pabss3664

