国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringSecurity自定義成功失敗處理器的示例代碼

SpringSecurity自定義成功失敗處理器的示例代碼

2020-09-05 00:25劍指桃花落 Java教程

這篇文章主要介紹了SpringSecurity自定義成功失敗處理器,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

1. 新建SpringBoot工程

SpringSecurity自定義成功失敗處理器的示例代碼

2. 項目依賴

<dependencies>
  <!-- security -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
  </dependency>
  <!-- thymeleaf -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
  <!-- web -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <!-- tomcat -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
  </dependency>
  <!-- lombok -->
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
  </dependency>
  <!-- test -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
  </dependency>
</dependencies>

3. 定義登錄成功處理器

  • 新建一個類實現AuthenticationSuccessHandler
  • 重寫onAuthenticationSuccess方法
package zw.springboot.controller;

import lombok.SneakyThrows;
import org.json.JSONObject;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @className LoginSuccessHandler
 * @description 登錄成功處理器
 * @author 周威
 * @date 2020-09-03 13:50
 **/
@Component
public class LoginSuccessHandler implements AuthenticationSuccessHandler
{

  @SneakyThrows
  @Override
  public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException
  {
    // 設置response緩沖區字符集
    response.setCharacterEncoding("UTF-8");
    // 定義一個JSONObject對象
    JSONObject object = new JSONObject();
    // 填寫登錄成功響應信息
    object.put("code", 1);
    object.put("msg", "登錄成功");
    // 設置響應頭
    response.setContentType("application/json;charset=utf-8");
    // 獲得打印輸出流
    PrintWriter pw = response.getWriter();
    // 向客戶端寫入一個字符串
    pw.print(object.toString());
    // 關閉流資源
    pw.close();
  }
}

4. 定義登錄失敗處理器新建一個類實現AuthenticationFailureHandler接口重寫onAuthenticationFailure方法

package zw.springboot.controller;

import lombok.SneakyThrows;
import org.json.JSONObject;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @className LoginErrorHandler
 * @description 登錄失敗處理器
 * @author 周威
 * @date 2020-09-03 13:57
 **/
@Component
public class LoginErrorHandler implements AuthenticationFailureHandler
{
  @SneakyThrows
  @Override
  public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException authenticationException) throws IOException, ServletException
  {
    // 設置response緩沖區字符集
    response.setCharacterEncoding("UTF-8");
    // 定義一個JSONObject對象
    JSONObject object = new JSONObject();
    // 填寫登錄失敗響應信息
    object.put("code", -1);
    object.put("msg", "登錄失敗");
    // 設置響應頭
    response.setContentType("application/json;charset=utf-8");
    // 獲得打印輸出流
    PrintWriter pw = response.getWriter();
    // 向客戶端寫入一個字符串
    pw.print(object.toString());
    // 關閉流資源
    pw.close();
  }
}

5. 安全認證配置類

package zw.springboot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

/**
 * @className SpringSecurityConfig
 * @description 安全人認證配置類
 * @author 周威
 * @date 2020-09-03 13:42
 **/
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter
{
  @Autowired
  private AuthenticationSuccessHandler loginSuccessHandler;

  @Autowired
  private AuthenticationFailureHandler loginErrorHandler;

  // 定義用戶信息服務
  @Bean
  @Override
  protected UserDetailsService userDetailsService()
  {
    InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
    // 模擬兩個用戶身份
    manager.createUser(User.withUsername("admin").password(passwordEncoder().encode("123456")).authorities("p1").build());
    manager.createUser(User.withUsername("user").password(passwordEncoder().encode("654321")).authorities("p2").build());
    return manager;
  }

  // 定義密碼加密器
  @Bean
  public PasswordEncoder passwordEncoder()
  {
    return new BCryptPasswordEncoder();
  }

  // 定義攔截機制
  @Override
  protected void configure(HttpSecurity http) throws Exception
  {
    http
        .authorizeRequests()
        // 設置哪些請求需要認證
        .antMatchers("/**").authenticated()
    .and()
        // 啟用表單登錄認證
        .formLogin()
        // 指定登錄成功處理器
        .successHandler(loginSuccessHandler)
        // 指定登錄失敗處理器
        .failureHandler(loginErrorHandler);
  }
}

6. 項目運行測試

SpringSecurity自定義成功失敗處理器的示例代碼

7. 登錄成功測試

SpringSecurity自定義成功失敗處理器的示例代碼

8. 登錄失敗測試

SpringSecurity自定義成功失敗處理器的示例代碼

總結

到此這篇關于SpringSecurity自定義成功失敗處理器的示例代碼的文章就介紹到這了,更多相關SpringSecurity成功失敗處理器內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/qq_43625140/article/details/108386147

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 毛片免费观看视频 | 91精品久久久久久久久久入口 | 国产精品久久久久久久一区探花 | 天堂中文网 | 日韩国产一区二区三区 | 99pao成人国产永久免费视频 | 日韩视频一区二区三区 | 免费激情| 国产视频一区在线 | 亚洲专区中文字幕 | 午夜精品久久久久久久久久久久 | 91精品国产乱码久久久久久久久 | 精品黄色国产 | wwwav在线| 日韩综合一区二区 | 九九资源站 | 99精品欧美一区二区三区综合在线 | 欧美成人免费网站 | 欧美精品一区二区三区在线 | 欧美jjzz| 在线中文字幕第一页 | 在线观看免费黄色 | 久久久精品视频免费观看 | 欧美黄色一区 | 亚洲精彩视频在线 | 国产一区亚洲 | 91国内外精品自在线播放 | 色嫩紧中文字幕在线 | 精品无码久久久久久久动漫 | 国产精品九九久久99视频 | 一级毛片免费 | 亚洲精品区 | 午夜在线视频播放 | 日韩精品视频在线 | 日本不卡高字幕在线2019 | 一呦二呦三呦国产精品 | 国产v日产∨综合v精品视频 | 先锋av资源在线 | 激情综合五月天 | 亚洲天堂久久精品 | 日韩欧美一区二区视频 |