我需要在两种类型的错误的情况下进行重定向:403和500。
过滤器中出现错误,因此在我看来可能的解决方案是添加另一个过滤器,如果前面过滤器的错误代码与所需的过滤器匹配(403和500),则会执行重定向。
@Configuration
@EnableWebSecurity
@EnableOAuth2Client
@RequiredArgsConstructor
public class Oauth2EpamConfiguration extends WebSecurityConfigurerAdapter {
// ...
private final AuthProblemsFilter authProblemsFilter;
@Override
protected void configure(HttpSecurity http) throws Exception {
// 500 can be thrown out here
http.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint());
http.authorizeRequests()
.antMatchers(tokenProcessUrl).permitAll()
.and()
.csrf().disable();
http.authorizeRequests()
.antMatchers("/**").authenticated();
// 403 can be thrown out here
http.addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
// the solution I propose
http.addFilterAfter(authProblemsFilter, BasicAuthenticationFilter.class);
过滤类:
@Component
public class AuthProblemsFilter extends OncePerRequestFilter {
@Override
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws IOException {
int status = response.getStatus();
if (status == 200)
response.getWriter().write("200");
}
最终,在运行应用程序后,我看到状态代码为200,即先前的错误已成功保留在之前的回复中。
如何不丢失先前错误的代码?