异常处理
最后更新于
httpSecurity.exceptionHandling(httpSecurityExceptionHandlingConfigurer -> {
// 认证异常处理
httpSecurityExceptionHandlingConfigurer.authenticationEntryPoint((httpServletRequest, httpServletResponse, e) -> {
if (e instanceof LockedException) {
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
httpServletResponse.getWriter().write("账号被锁定");
return;
}
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
httpServletResponse.getWriter().write("请登录");
});
// 权限异常处理
httpSecurityExceptionHandlingConfigurer.accessDeniedHandler((httpServletRequest, httpServletResponse, e) -> {
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
httpServletResponse.setStatus(HttpStatus.FORBIDDEN.value());
httpServletResponse.getWriter().write("权限不足");
});
// 设置当前用户请求未授权时跳转的页面
httpSecurityExceptionHandlingConfigurer.accessDeniedPage("/app/login");
// 为指定的请求配置默认的AccessDeniedHandler
httpSecurityExceptionHandlingConfigurer.defaultAccessDeniedHandlerFor((httpServletRequest, httpServletResponse, e) -> {
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
httpServletResponse.setStatus(HttpStatus.FORBIDDEN.value());
httpServletResponse.getWriter().write("权限不足");
}, httpServletRequest -> httpServletRequest.getRequestURI().startsWith("/app"));
// 为指定的请求配置默认的AuthenticationEntryPoint
httpSecurityExceptionHandlingConfigurer.defaultAuthenticationEntryPointFor((httpServletRequest, httpServletResponse, e) -> {
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
httpServletResponse.getWriter().write("请登录");
}, httpServletRequest -> httpServletRequest.getRequestURI().startsWith("/app"));
});@Configuration
@EnableWebSecurity
public class SpringSecurityConfigure {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
// some code ...
httpSecurity.exceptionHandling(httpSecurityExceptionHandlingConfigurer -> {
httpSecurityExceptionHandlingConfigurer.authenticationEntryPoint((httpServletRequest, httpServletResponse, e)->{
httpServletResponse.sendRedirect("/app/login");
});
});
return httpSecurity.build();
}
}@Configuration
@EnableWebSecurity
public class SpringSecurityConfigure {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
// some code ...
httpSecurity.exceptionHandling(httpSecurityExceptionHandlingConfigurer -> {
ObjectMapper objectMapper = new ObjectMapper();
httpSecurityExceptionHandlingConfigurer.authenticationEntryPoint((httpServletRequest, httpServletResponse, e) -> {
httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
Map<String, String> map = Map.of("message", "认证失败");
httpServletResponse.getWriter().println(objectMapper.writeValueAsString(map));
});
});
return httpSecurity.build();
}
}