新版的 Springsecurity request.getRequestDispatcher).forward(request, response); 404 问题,已解决...

本文讨论了旧版本SpringSecurity中直接转发登录请求的问题,并指出新版本转发会出现404错误的原因在于SpringSecurity过滤器不支持转发。文章提供了配置解决方案,通过在security配置中添加dispatcher-types属性,设置为FORWARD, INCLUDE, REQUEST, ASYNC, ERROR,以解决转发问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

旧版本的 可以直接 转发登陆

request.getRequestDispatcher).forward(request, response);

新版本的转发会404,原因 SpringSecurity 过滤器不支持转发

可以加上以下配置解决

security:
  filter:
    dispatcher-types: FORWARD,INCLUDE,REQUEST,ASYNC,ERROR

转载于:https://www.cnblogs.com/sweetchildomine/p/9941918.html

package com.kucun.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 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.access.AccessDeniedHandler; // 2. 基础安全配置 @Configuration @EnableWebSecurity // 启用Web安全功能 public class SecurityConfig extends WebSecurityConfigurerAdapter{ /** * 核心安全过滤器链配置 * @param http HTTP安全构建器 * @return 安全过滤器链 * @throws Exception 配置异常 * * █ 配置逻辑说明: * 1. authorizeHttpRequests: 定义访问控制规则 * 2. formLogin: 配置表单登录 * 3. logout: 配置注销行为 * 4. exceptionHandling: 处理权限异常[^3] */ @Override public void configure(HttpSecurity http) throws Exception { http .authorizeRequests(auth -> auth // 按从具体到泛型的顺序配置 .antMatchers("/login.html").permitAll() .antMatchers(HttpMethod.POST, "/users/login").permitAll() .antMatchers("/users/guanli/**").hasRole("ADMIN") .antMatchers("/js/**", "/css/**", "/fonts/**", "/images/**").permitAll() .anyRequest().authenticated() ) .formLogin(form -> form .loginPage("/login.html") .loginProcessingUrl("/users/login") .defaultSuccessUrl("/index.html", true) .failureUrl("/login.html?error=true") ) .logout(logout -> logout .logoutUrl("/logout") .logoutSuccessUrl("/login.html") ) .csrf(csrf -> csrf .ignoringAntMatchers("/users/login") // 禁用CSRF保护 ) .headers(headers -> headers .frameOptions().sameOrigin() ) // 异常处理 .exceptionHandling(ex -> ex .accessDeniedPage("/error/403") // 权限不足跳转页面[^3] ); //return http.build(); } /** * 密码编码器(必须配置) * 使用BCrypt强哈希算法加密 */ @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Bean public AccessDeniedHandler accessDeniedHandler() { System.out.println("0000"); return (request, response, accessDeniedException) -> { if (!response.isCommitted()) { request.getRequestDispatcher("/error/403").forward(request, response); } }; } }
05-29
package com.kucun.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 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.access.AccessDeniedHandler; // 2. 基础安全配置 @Configuration @EnableWebSecurity // 启用Web安全功能 public class SecurityConfig extends WebSecurityConfigurerAdapter{ /** * 核心安全过滤器链配置 * @param http HTTP安全构建器 * @return 安全过滤器链 * @throws Exception 配置异常 * * █ 配置逻辑说明: * 1. authorizeHttpRequests: 定义访问控制规则 * 2. formLogin: 配置表单登录 * 3. logout: 配置注销行为 * 4. exceptionHandling: 处理权限异常[^3] */ @Override public void configure(HttpSecurity http) throws Exception { http .authorizeRequests(auth -> auth // 按从具体到泛型的顺序配置 .antMatchers("/login.html").permitAll() .antMatchers(HttpMethod.POST, "/users/login").permitAll() .antMatchers("/users/guanli/**").hasRole("ADMIN") .antMatchers("/js/**", "/css/**", "/fonts/**", "/images/**").permitAll() .anyRequest().authenticated() ) .formLogin(form -> form .loginPage("/login.html") .loginProcessingUrl("/users/login") .defaultSuccessUrl("/index.html", true) .failureUrl("/login.html?error=true") ) .logout(logout -> logout .logoutUrl("/logout") .logoutSuccessUrl("/login.html") ) .csrf(csrf -> csrf .ignoringAntMatchers("/users/login") // 禁用CSRF保护 ) .headers(headers -> headers .frameOptions().sameOrigin() ) // 异常处理 .exceptionHandling(ex -> ex .accessDeniedPage("/error/403") // 权限不足跳转页面[^3] ); //return http.build(); } /** * 密码编码器(必须配置) * 使用BCrypt强哈希算法加密 */ @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Bean public AccessDeniedHandler accessDeniedHandler() { System.out.println("0000"); return (request, response, accessDeniedException) -> { if (!response.isCommitted()) { request.getRequestDispatcher("/error/403").forward(request, response); } }; } } <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.12.RELEASE</version> <!-- 请根据需要选择版本 --> <relativePath/> <!-- lookup parent from repository --> </parent>
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值