個人網站建立教程網站建設黃頁免費觀看
文章目錄
- SpringSecurity 返回json
- 一、登錄成功處理器
- 1.1 統一響應類HttpResult
- 1.2 登錄成功處理器
- 1.3 配置登錄成功處理器
- 1.4 登錄
- 二、登錄失敗處理器
- 2.1 登錄失敗處理器
- 2.2 配置登錄失敗處理器
- 2.3 登錄
- 三、退出成功處理器
- 3.1 退出成功處理器
- 3.2 配置退出成功處理器
- 3.3 退出
- 四、訪問拒絕(無權限)處理器
- 4.1 訪問拒絕處理器
- 4.2 配置訪問拒絕處理器
- 4.3 被拒絕
- 五、自定義處理器
SpringSecurity 返回json
承接:1.SpringSecurity -快速入門、加密、基礎授權-CSDN博客
一、登錄成功處理器
前后端分離成為企業(yè)應用開發(fā)中的主流,前后端分離通過json進行交互,登錄成功和失敗后不用頁面跳轉,而是一段json提示
1.1 統一響應類HttpResult
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class HttpResult {private Integer code;private String msg;private Object data;public HttpResult(Integer code, String msg) {this.code = code;this.msg = msg;}
}
1.2 登錄成功處理器
/*** 認證成功就會調用該接口里的方法*/
@Component
public class AppAuthenticationSuccessHandle implements AuthenticationSuccessHandler {// JSON序列化器,進行序列化和反序列化@Resourceprivate ObjectMapper objectMapper;;@Overridepublic void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
// 定義返回對象httpResultHttpResult httpResult = HttpResult.builder().code(200).msg("登陸成功").build();String strResponse = objectMapper.writeValueAsString(httpResult);// 響應字符集response.setCharacterEncoding("UTF-8");
// 響應內容類型JSON,字符集utf-8response.setContentType("application/json;charset=utf-8");
// 響應給前端PrintWriter writer = response.getWriter();writer.println(strResponse);writer.flush();}
}
1.3 配置登錄成功處理器
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Resourceprivate AppAuthenticationSuccessHandle appAuthenticationSuccessHandle;@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests()//授權http請求.anyRequest() //任何請求.authenticated();//都需要認證http.formLogin().successHandler(appAuthenticationSuccessHandle) //認證成功處理器.permitAll();//允許表單登錄}}
1.4 登錄
登錄成功后如下所示
二、登錄失敗處理器
2.1 登錄失敗處理器
/*** 認證失敗就會調用下面的方法*/
@Component
public class AppAuthenticationFailHandle implements AuthenticationFailureHandler {// JSON序列化器,進行序列化和反序列化@Resourceprivate ObjectMapper objectMapper;;@Overridepublic void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {// 定義返回對象httpResultHttpResult httpResult = HttpResult.builder().code(401).msg("登錄失敗").build();String strResponse = objectMapper.writeValueAsString(httpResult);// 響應字符集response.setCharacterEncoding("UTF-8");
// 響應內容類型JSON,字符集utf-8response.setContentType("application/json;charset=utf-8");
// 響應給前端PrintWriter writer = response.getWriter();writer.println(strResponse);writer.flush();}
}
2.2 配置登錄失敗處理器
@Resource
private AppAuthenticationFailHandle appAuthenticationFailHandle;@Override
protected void configure(HttpSecurity http) throws Exception {http.authorizeRequests()//授權http請求.anyRequest() //任何請求.authenticated();//都需要認證http.formLogin().successHandler(appAuthenticationSuccessHandle) //認證成功處理器.failureHandler(appAuthenticationFailHandle) // 認證失敗處理器.permitAll();//允許表單登錄
}
2.3 登錄
輸入一個錯誤的密碼
如下圖所示
三、退出成功處理器
3.1 退出成功處理器
/*** 退出成功處理器*/
@Component
public class AppLogoutSuccessHandle implements LogoutSuccessHandler{// JSON序列化器,進行序列化和反序列化@Resourceprivate ObjectMapper objectMapper;;@Overridepublic void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
// 定義返回對象httpResultHttpResult httpResult = HttpResult.builder().code(200).msg("退出成功").build();String strResponse = objectMapper.writeValueAsString(httpResult);// 響應字符集response.setCharacterEncoding("UTF-8");
// 響應內容類型JSON,字符集utf-8response.setContentType("application/json;charset=utf-8");
// 響應給前端PrintWriter writer = response.getWriter();writer.println(strResponse);writer.flush();}
}
3.2 配置退出成功處理器
@Resource
private AppLogoutSuccessHandle appLogoutSuccessHandle;@Override
protected void configure(HttpSecurity http) throws Exception {http.authorizeRequests()//授權http請求.anyRequest() //任何請求.authenticated();//都需要認證http.formLogin().successHandler(appAuthenticationSuccessHandle) //認證成功處理器.failureHandler(appAuthenticationFailHandle) // 認證失敗處理器.permitAll();//允許表單登錄http.logout().logoutSuccessHandler(appLogoutSuccessHandle);//登錄成功處理器
}
3.3 退出
四、訪問拒絕(無權限)處理器
4.1 訪問拒絕處理器
@Component
public class AppAccessDenyHandle implements AccessDeniedHandler {// JSON序列化器,進行序列化和反序列化@Resourceprivate ObjectMapper objectMapper;;@Overridepublic void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {// 定義返回對象httpResultHttpResult httpResult = HttpResult.builder().code(403).msg("您沒有權限訪問該資源!!").build();String strResponse = objectMapper.writeValueAsString(httpResult);// 響應字符集response.setCharacterEncoding("UTF-8");
// 響應內容類型JSON,字符集utf-8response.setContentType("application/json;charset=utf-8");
// 響應給前端PrintWriter writer = response.getWriter();writer.println(strResponse);writer.flush();}
}
4.2 配置訪問拒絕處理器
@Resource
private AppAccessDenyHandle appAccessDenyHandle;@Override
protected void configure(HttpSecurity http) throws Exception {http.authorizeRequests()//授權http請求.anyRequest() //任何請求.authenticated();//都需要認證http.formLogin().successHandler(appAuthenticationSuccessHandle) //認證成功處理器.failureHandler(appAuthenticationFailHandle) // 認證失敗處理器.permitAll();//允許表單登錄http.logout().logoutSuccessHandler(appLogoutSuccessHandle);//登錄成功處理器;http.exceptionHandling()//異常處理.accessDeniedHandler(appAccessDenyHandle);//訪問被拒絕處理器
}
4.3 被拒絕
五、自定義處理器
SpringSecurity - 認證與授權、自定義失敗處理、跨域問題、認證成功/失敗處理器_我愛布朗熊的博客-CSDN博客