欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring security登錄過程邏輯詳解

 更新時間:2020年04月07日 14:47:29   作者:if年少有為  
這篇文章主要介紹了SSpringsecurity登錄過程邏輯詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

1. 新建項目

引入web和security包

完整的pom.xml文件如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.example</groupId>
  <artifactId>spring-demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring-demo</name>
  <description>Demo project for Spring Boot</description>

  <properties>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

2. 編寫啟動類和控制器方法和自定義登錄頁面

package com.example.springdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class SpringDemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(SpringDemoApplication.class, args);
  }

  @GetMapping("/")
  public String hello() {
    return "hello spring security";
  }
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<form action="myLogin.html" method="post">
  <input type="text" name="username">
  <input type="password" name="password">
  <input type="submit" value="登錄">
</form>
</body>
</html>

3. 編寫配置類

package com.example.springdemo.conf;

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.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .anyRequest().authenticated()
        .and()
        .formLogin()
        //指定處理登錄頁面
        .loginPage("/myLogin.html")
        //指定登錄成功的處理邏輯
        .successHandler(new AuthenticationSuccessHandler() {
          @Override
          public void onAuthenticationSuccess(HttpServletRequest request,
                            HttpServletResponse response,
                            Authentication authentication)
              throws IOException, ServletException {
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            writer.write("{\"error_code\":\"0\",\"message\":\"歡迎登錄\"}");
          }
        })
        //指定登錄失敗時的處理邏輯
        .failureHandler(new AuthenticationFailureHandler() {
          @Override
          public void onAuthenticationFailure(HttpServletRequest request,
                            HttpServletResponse response,
                            AuthenticationException e)
              throws IOException, ServletException {
            response.setStatus(401);
            PrintWriter writer = response.getWriter();
            writer.write("{\"error_code\":\"401\",\"name\":\"" + e.getClass() + "\",\"message\":\"" + e.getMessage() + "\"}");

          }
        })
        .permitAll()
        .and()
        .csrf().disable();
  }
}

4. 運行結果

當輸入密碼錯誤時

當輸入密碼正確時

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Kotlin基礎教程之dataclass,objectclass,use函數,類擴展,socket

    Kotlin基礎教程之dataclass,objectclass,use函數,類擴展,socket

    這篇文章主要介紹了Kotlin基礎教程之dataclass,objectclass,use函數,類擴展,socket的相關資料,需要的朋友可以參考下
    2017-05-05
  • Springboot設置統(tǒng)一的返回格式的方法步驟

    Springboot設置統(tǒng)一的返回格式的方法步驟

    在我們應用中我們通常與前端交互使用json格式,設置統(tǒng)一的返回json 格式是非常必要的,本文主要介紹了Springboot設置統(tǒng)一的返回格式的方法步驟,具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • SpringBoot優(yōu)先加載指定Bean的實現

    SpringBoot優(yōu)先加載指定Bean的實現

    SpringBoot框架在啟動時可以自動將托管的Bean實例化,一般情況下它的依賴注入特性可以正確處理Bean之間的依賴關系,無需手動指定某個 Bean優(yōu)先創(chuàng)建實例,文中有詳細的代碼示例,需要的朋友可以參考下
    2023-05-05
  • Redis原子計數器incr,防止并發(fā)請求操作

    Redis原子計數器incr,防止并發(fā)請求操作

    這篇文章主要介紹了Redis原子計數器incr,防止并發(fā)請求操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • java自定義注解接口實現方案

    java自定義注解接口實現方案

    java注解是附加在代碼中的一些元信息,用于一些工具在編譯、運行時進行解析和使用,起到說明、配置的功能,本文將詳細介紹,此功能的實現方法
    2012-11-11
  • java實現撲克牌牌面小程序

    java實現撲克牌牌面小程序

    這篇文章主要為大家詳細介紹了java實現撲克牌牌面小程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • java后臺批量下載文件并壓縮成zip下載的方法

    java后臺批量下載文件并壓縮成zip下載的方法

    這篇文章主要為大家詳細介紹了java后臺批量下載文件并壓縮成zip下載的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • 解決偶現的MissingServletRequestParameterException異常問題

    解決偶現的MissingServletRequestParameterException異常問題

    這篇文章主要介紹了解決偶現的MissingServletRequestParameterException問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java線程的start方法回調run方法的操作技巧

    Java線程的start方法回調run方法的操作技巧

    面試過程中經常會被面試官問到為什么我們調用start()方法時會執(zhí)行run()方法,為什么不能直接調用run()方法,問的一頭霧水,今天小編給大家介紹下Java線程的start方法回調run方法的操作技巧,需要的朋友參考下吧
    2017-11-11
  • java連接postgresql數據庫代碼及maven配置方式

    java連接postgresql數據庫代碼及maven配置方式

    這篇文章主要介紹了java連接postgresql數據庫代碼及maven配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09

最新評論