中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

個人網站建立教程網站建設黃頁免費觀看

個人網站建立教程,網站建設黃頁免費觀看,政府網站建設報價表,1建設網站的重要性文章目錄 SpringSecurity 返回json一、登錄成功處理器1.1 統一響應類HttpResult1.2 登錄成功處理器1.3 配置登錄成功處理器1.4 登錄 二、登錄失敗處理器2.1 登錄失敗處理器2.2 配置登錄失敗處理器2.3 登錄 三、退出成功處理器3.1 退出成功處理器3.2 配置退出成功處理器3.3 退出…

文章目錄

  • 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 登錄

image-20231016223324743

登錄成功后如下所示

image-20231016223344428

二、登錄失敗處理器

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 登錄

輸入一個錯誤的密碼

image-20231016224805298

如下圖所示

image-20231016224824503

三、退出成功處理器

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 退出

image-20231016231114408

四、訪問拒絕(無權限)處理器

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 被拒絕

image-20231016231313240

五、自定義處理器

SpringSecurity - 認證與授權、自定義失敗處理、跨域問題、認證成功/失敗處理器_我愛布朗熊的博客-CSDN博客

http://www.risenshineclean.com/news/54358.html

相關文章:

  • 網站建設最好的公司網站域名費一年多少錢
  • 廣州網站優(yōu)化關鍵詞排名最新疫情新聞100字
  • 長治制作公司網站的公司百度免費推廣怎么操作
  • 可以做來電名片的網站seo高端培訓
  • 怎么做網站首頁圖片不會失真云南今日頭條新聞
  • 內容管理系統開源佛山網站建設十年樂云seo
  • 一個好的網站應該具有什么西安百度推廣競價托管
  • 舉例說明網絡營銷的概念小紅書seo排名帝搜軟件
  • 網站建設商務的術語怎么注冊網站平臺
  • wordpress安裝顯示404寧波優(yōu)化網站排名軟件
  • 可信賴的深圳網站建設百度seo霸屏軟件
  • 門戶網站建設方案文檔如何自己建立一個網站
  • 京東商城企業(yè)網站建設分析收錄網
  • 網站內的搜索怎么做的市場調研問卷
  • 網站標題寫什么作用seo顧問服務 樂云踐新專家
  • 做了個網站 怎么做seo百度網盤網頁版登錄首頁
  • 空間網站鏈接怎么做淘寶店鋪買賣交易平臺
  • seo優(yōu)化方案書鄭州seo優(yōu)化推廣
  • 做網站-信科網絡軟文推廣網
  • 酒店網站素材百度電腦版網頁版入口
  • 建網站哪個好 優(yōu)幫云優(yōu)化公司結構
  • 網上做網站 干對縫兒生意廣州seo公司品牌
  • 網站開發(fā)編程的工作方法可以看任何網站的瀏覽器
  • 做網站的網站怎么在網上做廣告宣傳
  • 甘肅農村網站建設汽車seo是什么意思
  • 佛山網絡公司網站建設凡科建站登錄官網
  • 建設網站需要多久qq營銷軟件
  • 汕頭市網站建設公司任務放單平臺
  • 做網站換服務器怎么整成人教育機構排行前十名
  • design工業(yè)設計seo優(yōu)化工作