最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring security登錄過程邏輯詳解

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

1. 新建項(xiàng)目

引入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. 編寫啟動(dòng)類和控制器方法和自定義登錄頁(yè)面

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()
        //指定處理登錄頁(yè)面
        .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\":\"歡迎登錄\"}");
          }
        })
        //指定登錄失敗時(shí)的處理邏輯
        .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. 運(yùn)行結(jié)果

當(dāng)輸入密碼錯(cuò)誤時(shí)

當(dāng)輸入密碼正確時(shí)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot 配置使用swagger2操作

    springboot 配置使用swagger2操作

    這篇文章主要介紹了springboot 配置使用swagger2操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • java語(yǔ)言實(shí)現(xiàn)猜數(shù)字游戲

    java語(yǔ)言實(shí)現(xiàn)猜數(shù)字游戲

    這篇文章主要為大家詳細(xì)介紹了java語(yǔ)言實(shí)現(xiàn)猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Java中常用的6種排序算法詳細(xì)分解

    Java中常用的6種排序算法詳細(xì)分解

    這篇文章主要介紹了Java中常用的6種排序算法詳細(xì)分解,著重說明每個(gè)算法的計(jì)算過程分解,是探究實(shí)現(xiàn)原理級(jí)的文章,對(duì)于深入理解這些算法有很大幫助,需要的朋友可以參考下
    2014-07-07
  • Spring?Boot整合持久層之JPA多數(shù)據(jù)源

    Spring?Boot整合持久層之JPA多數(shù)據(jù)源

    JPA(Java Persistence API)Java 持久化 API,是 Java 持久化的標(biāo)準(zhǔn)規(guī)范,Hibernate 是持久化規(guī)范的技術(shù)實(shí)現(xiàn),而 Spring Data JPA 是在 Hibernate 基礎(chǔ)上封裝的一款框架
    2022-08-08
  • java如何動(dòng)態(tài)執(zhí)行while循環(huán)

    java如何動(dòng)態(tài)執(zhí)行while循環(huán)

    這篇文章主要介紹了java如何動(dòng)態(tài)執(zhí)行while循環(huán)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • IntelliJ IDEA報(bào)錯(cuò)Error:java: Compilation failed: internal java compiler error的解決辦法

    IntelliJ IDEA報(bào)錯(cuò)Error:java: Compilation failed: internal java

    今天小編就為大家分享一篇關(guān)于IntelliJ IDEA報(bào)錯(cuò)Error:java: Compilation failed: internal java compiler error的解決辦法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • java 線程中start方法與run方法的區(qū)別詳細(xì)介紹

    java 線程中start方法與run方法的區(qū)別詳細(xì)介紹

    這篇文章主要介紹了java 線程中start方法與run方法的區(qū)別詳細(xì)介紹的相關(guān)資料,在java線程中調(diào)用start方法與run方法的區(qū)別在哪里? 這兩個(gè)問題是兩個(gè)非常流行的初學(xué)者級(jí)別的多線程面試問題,這里進(jìn)行詳細(xì)說明,需要的朋友可以參考下
    2016-11-11
  • 淺談hibernate之映射文件VS映射注解

    淺談hibernate之映射文件VS映射注解

    下面小編就為大家?guī)硪黄獪\談hibernate之映射文件VS映射注解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • SpringBoot中實(shí)現(xiàn)@Scheduled動(dòng)態(tài)定時(shí)任務(wù)

    SpringBoot中實(shí)現(xiàn)@Scheduled動(dòng)態(tài)定時(shí)任務(wù)

    SpringBoot中的@Scheduled注解為定時(shí)任務(wù)提供了一種很簡(jiǎn)單的實(shí)現(xiàn),本文主要介紹了SpringBoot中實(shí)現(xiàn)@Scheduled動(dòng)態(tài)定時(shí)任務(wù),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • java實(shí)現(xiàn)八皇后問題示例分享

    java實(shí)現(xiàn)八皇后問題示例分享

    這篇文章主要介紹了java實(shí)現(xiàn)八皇后問題示例,八皇后問題,是一個(gè)古老而著名的問題,是回溯算法的典型案例。該問題是國(guó)際西洋棋棋手馬克斯·貝瑟爾于1848年提出
    2014-03-03

最新評(píng)論

桂林市| 平谷区| 普洱| 延寿县| 阳春市| 齐齐哈尔市| 皋兰县| 济南市| 南汇区| 绍兴市| 东港市| 邢台市| 通道| 芷江| 塘沽区| 甘谷县| 平利县| 饶平县| 彭泽县| 额尔古纳市| 始兴县| 什邡市| 图们市| 罗源县| 玛多县| 许昌县| 韶山市| 锡林浩特市| 基隆市| 太康县| 阿拉善盟| 隆德县| 青神县| 陈巴尔虎旗| 额尔古纳市| 博湖县| 申扎县| 徐州市| 兴业县| 浮梁县| 黔东|