笔记参考与柏码知识库
基于Session分离
使用Cookie中的JSESSIONID数据。
登录授权和跨域处理
前后端分离后,后端只需返回前端数据,不需要进行重定向,可以手动设置SuccessHandler和FailureHandler来实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { return http ... .formLogin(conf -> { conf.loginProcessingUrl("/api/auth/login"); conf.failureHandler(this::onAuthenticationFailure); conf.successHandler(this::onAuthenticationSuccess); conf.permitAll(); }) ... }
void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) {
}
void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) { }
|
返回的JSON推荐使用Rest API标志进行编写。
创建实体类装载响应数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public record RestBean<T> (int code, T data, String message) { public static <T> RestBean<T> success(T data){ return new RestBean<>(200, data, "请求成功"); }
public static <T> RestBean<T> failure(int code, String message){ return new RestBean<>(code, null, message); }
public static <T> RestBean<T> failure(int code){ return failure(code, "请求失败"); } public String asJsonString() { return JSONObject.toJSONString(this, JSONWriter.Feature.WriteNulls); } }
|
设置Handler:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException { response.setContentType("application/json;charset=utf-8"); PrintWriter writer = response.getWriter(); writer.write(RestBean.failure(401, exception.getMessage()).asJsonString()); }
void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException { response.setContentType("application/json;charset=utf-8"); PrintWriter writer = response.getWriter(); writer.write(RestBean.success(authentication.getName()).asJsonString()); }
|
跨域配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { return http ... .cors(conf -> { CorsConfiguration cors = new CorsConfiguration(); cors.addAllowedOrigin("http://localhost:8080"); cors.setAllowCredentials(true); cors.addAllowedHeader("*"); cors.addAllowedMethod("*"); cors.addExposedHeader("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", cors); conf.configurationSource(source); }) ... .build(); }
|