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

SpringSecurity自定義登錄界面

 更新時間:2022年09月06日 11:07:26   作者:搞錢自律  
這篇文章主要為大家詳細介紹了SpringSecurity自定義登錄界面,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

為什么需要自定義登錄界面?

答:因為SpringBoot整合SpringSecurity時,只需要一個依賴,無需其他配置,就可以實現(xiàn)認證功能。但是它的認證登錄界面是固定那樣的,如下圖所示,但是我們希望自己搞個好看的登錄界面,所以需要自定義登錄界面。

第一步:創(chuàng)建springboot項目

<?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.6.6</version>
? ? ? ? <relativePath/> <!-- lookup parent from repository -->
? ? </parent>
? ? <groupId>com.example</groupId>
? ? <artifactId>spring-security-03</artifactId>
? ? <version>0.0.1-SNAPSHOT</version>
? ? <name>spring-security-03</name>
? ? <description>Demo project for Spring Boot</description>
? ? <properties>
? ? ? ? <java.version>1.8</java.version>
? ? </properties>
? ? <dependencies>
? ? ? ? <!--thymeleaf-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? <artifactId>spring-boot-starter-thymeleaf</artifactId>
? ? ? ? </dependency>
? ? ? ? <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>
? ? ? ? </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>

第二步:添加配置application.properties

#修改springSecurity默認用戶名和密碼
spring.security.user.name=root
spring.security.user.password=root

#設(shè)置 thymeleaf 緩存為false,表示立即生效
spring.thymeleaf.cache=false

第三步:Controller

package com.example.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

? ? @RequestMapping("/hello")
? ? public String hello(){
? ? ? ? System.out.println("hello spring security");
? ? ? ? return "hello spring security";
? ? }
}
package com.example.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

? ? @RequestMapping("/index")
? ? public String hello(){
? ? ? ? System.out.println("hello index");
? ? ? ? return "hello index";
? ? }
}
package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController {

? ? @RequestMapping("/loginHtml")
? ? public String loginHtml(){
? ? ? ? return "login";
? ? }
}

第四步:login.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/" lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <title>用戶登錄</title>
</head>
<body>
? ? <h1>用戶登錄</h1>
? ? <form th:action="@{/doLogin}" method="post">
? ? ? ? 用戶名:<input type="text" name="username"> <br>
? ? ? ? 密碼:<input type="text" name="password"><br>
? ? ? ? <input type="submit" value="登錄">
? ? </form>
</body>
</html>

第五步:配置自定義登錄界面

package com.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {


? ? @Override
? ? public void configure(HttpSecurity http) throws Exception {

? ? ? ? //【注意事項】放行資源要放在前面,認證的放在后面
? ? ? ? http.authorizeRequests()
? ? ? ? ? ? ? ? .mvcMatchers("/index").permitAll() //代表放行index的所有請求
? ? ? ? ? ? ? ? .mvcMatchers("/loginHtml").permitAll() //放行l(wèi)oginHtml請求
? ? ? ? ? ? ? ? .anyRequest().authenticated()//代表其他請求需要認證
? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? .formLogin()//表示其他需要認證的請求通過表單認證
? ? ? ? ? ? ? ? //loginPage 一旦你自定義了這個登錄頁面,那你必須要明確告訴SpringSecurity日后哪個url處理你的登錄請求
? ? ? ? ? ? ? ? .loginPage("/loginHtml")//用來指定自定義登錄界面,不使用SpringSecurity默認登錄界面 ?注意:一旦自定義登錄頁面,必須指定登錄url
? ? ? ? ? ? ? ? //loginProcessingUrl ?這個doLogin請求本身是沒有的,因為我們只需要明確告訴SpringSecurity,日后只要前端發(fā)起的是一個doLogin這樣的請求,
? ? ? ? ? ? ? ? //那SpringSecurity應(yīng)該把你username和password給捕獲到
? ? ? ? ? ? ? ? .loginProcessingUrl("/doLogin")//指定處理登錄的請求url
? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? .csrf().disable(); //禁止csrf 跨站請求保護
? ? }
}

5.1 請求參數(shù)名修改

上面的login.html用戶名必須為username,密碼必須為password,如果我們想要使用自定義的屬性名,按照如下修改

5.1.1 修改login.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/" lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <title>用戶登錄</title>
</head>
<body>
? ? <h1>用戶登錄</h1>
? ? <form th:action="@{/doLogin}" method="post">
? ? ? ? 用戶名:<input type="text" name="uname"> <br>
? ? ? ? 密碼:<input type="text" name="passwd"><br>
? ? ? ? <input type="submit" value="登錄">
? ? </form>
</body>
</html>

5.1.2 修改配置類

package com.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {


? ? @Override
? ? public void configure(HttpSecurity http) throws Exception {

? ? ? ? //【注意事項】放行資源要放在前面,認證的放在后面
? ? ? ? http.authorizeRequests()
? ? ? ? ? ? ? ? .mvcMatchers("/index").permitAll() //代表放行index的所有請求
? ? ? ? ? ? ? ? .mvcMatchers("/loginHtml").permitAll() //放行l(wèi)oginHtml請求
? ? ? ? ? ? ? ? .anyRequest().authenticated()//代表其他請求需要認證
? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? .formLogin()//表示其他需要認證的請求通過表單認證
? ? ? ? ? ? ? ? //loginPage 一旦你自定義了這個登錄頁面,那你必須要明確告訴SpringSecurity日后哪個url處理你的登錄請求
? ? ? ? ? ? ? ? .loginPage("/loginHtml")//用來指定自定義登錄界面,不使用SpringSecurity默認登錄界面 ?注意:一旦自定義登錄頁面,必須指定登錄url
? ? ? ? ? ? ? ? //loginProcessingUrl ?這個doLogin請求本身是沒有的,因為我們只需要明確告訴SpringSecurity,日后只要前端發(fā)起的是一個doLogin這樣的請求,
? ? ? ? ? ? ? ? //那SpringSecurity應(yīng)該把你username和password給捕獲到
? ? ? ? ? ? ? ? .loginProcessingUrl("/doLogin")//指定處理登錄的請求url
? ? ? ? ? ? ? ? .usernameParameter("uname") //指定登錄界面用戶名文本框的name值,如果沒有指定,默認屬性名必須為username
? ? ? ? ? ? ? ? .passwordParameter("passwd")//指定登錄界面密碼密碼框的name值,如果沒有指定,默認屬性名必須為password
? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? .csrf().disable(); //禁止csrf 跨站請求保護
? ? }
}

5.1 認證成功跳轉(zhuǎn)路徑

修改配置類successForwardUrl

package com.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {


? ? @Override
? ? public void configure(HttpSecurity http) throws Exception {

? ? ? ? //【注意事項】放行資源要放在前面,認證的放在后面
? ? ? ? http.authorizeRequests()
? ? ? ? ? ? ? ? .mvcMatchers("/index").permitAll() //代表放行index的所有請求
? ? ? ? ? ? ? ? .mvcMatchers("/loginHtml").permitAll() //放行l(wèi)oginHtml請求
? ? ? ? ? ? ? ? .anyRequest().authenticated()//代表其他請求需要認證
? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? .formLogin()//表示其他需要認證的請求通過表單認證
? ? ? ? ? ? ? ? //loginPage 一旦你自定義了這個登錄頁面,那你必須要明確告訴SpringSecurity日后哪個url處理你的登錄請求
? ? ? ? ? ? ? ? .loginPage("/loginHtml")//用來指定自定義登錄界面,不使用SpringSecurity默認登錄界面 ?注意:一旦自定義登錄頁面,必須指定登錄url
? ? ? ? ? ? ? ? //loginProcessingUrl ?這個doLogin請求本身是沒有的,因為我們只需要明確告訴SpringSecurity,日后只要前端發(fā)起的是一個doLogin這樣的請求,
? ? ? ? ? ? ? ? //那SpringSecurity應(yīng)該把你username和password給捕獲到
? ? ? ? ? ? ? ? .loginProcessingUrl("/doLogin")//指定處理登錄的請求url
? ? ? ? ? ? ? ? .usernameParameter("uname") //指定登錄界面用戶名文本框的name值,如果沒有指定,默認屬性名必須為username
? ? ? ? ? ? ? ? .passwordParameter("passwd")//指定登錄界面密碼密碼框的name值,如果沒有指定,默認屬性名必須為password
? ? ? ? ? ? ? ? .successForwardUrl("/index")//認證成功 forward 跳轉(zhuǎn)路徑
? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? .csrf().disable(); //禁止csrf 跨站請求保護
? ? }
}

修改配置類defaultSuccessUrl

package com.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {


? ? @Override
? ? public void configure(HttpSecurity http) throws Exception {

? ? ? ? //【注意事項】放行資源要放在前面,認證的放在后面
? ? ? ? http.authorizeRequests()
? ? ? ? ? ? ? ? .mvcMatchers("/index").permitAll() //代表放行index的所有請求
? ? ? ? ? ? ? ? .mvcMatchers("/loginHtml").permitAll() //放行l(wèi)oginHtml請求
? ? ? ? ? ? ? ? .anyRequest().authenticated()//代表其他請求需要認證
? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? .formLogin()//表示其他需要認證的請求通過表單認證
? ? ? ? ? ? ? ? //loginPage 一旦你自定義了這個登錄頁面,那你必須要明確告訴SpringSecurity日后哪個url處理你的登錄請求
? ? ? ? ? ? ? ? .loginPage("/loginHtml")//用來指定自定義登錄界面,不使用SpringSecurity默認登錄界面 ?注意:一旦自定義登錄頁面,必須指定登錄url
? ? ? ? ? ? ? ? //loginProcessingUrl ?這個doLogin請求本身是沒有的,因為我們只需要明確告訴SpringSecurity,日后只要前端發(fā)起的是一個doLogin這樣的請求,
? ? ? ? ? ? ? ? //那SpringSecurity應(yīng)該把你username和password給捕獲到
? ? ? ? ? ? ? ? .loginProcessingUrl("/doLogin")//指定處理登錄的請求url
? ? ? ? ? ? ? ? .usernameParameter("uname") //指定登錄界面用戶名文本框的name值,如果沒有指定,默認屬性名必須為username
? ? ? ? ? ? ? ? .passwordParameter("passwd")//指定登錄界面密碼密碼框的name值,如果沒有指定,默認屬性名必須為password
// ? ? ? ? ? ? ? ?.successForwardUrl("/index")//認證成功 forward 跳轉(zhuǎn)路徑,forward代表服務(wù)器內(nèi)部的跳轉(zhuǎn)之后,地址欄不變 始終在認證成功之后跳轉(zhuǎn)到指定請求
? ? ? ? ? ? ? ? .defaultSuccessUrl("/index")//認證成功 之后跳轉(zhuǎn),重定向 redirect 跳轉(zhuǎn)后,地址會發(fā)生改變 ?根據(jù)上一保存請求進行成功跳轉(zhuǎn)
? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? .csrf().disable(); //禁止csrf 跨站請求保護
? ? }
}

訪問http://localhost:8080/hello,認證后

發(fā)現(xiàn)并沒有到/index的情況,這是defaultSuccessUrl一特性,如果你想硬跳到/index,修改java defaultSuccessUrl("/index",true)即可

訪問http://localhost:8080/loginHtml,認證后

defaultSuccessUrl和successForwardUrl區(qū)別

1、successForwardUrl是forward跳轉(zhuǎn),defaultSuccessUrl是重定向redirect跳轉(zhuǎn)
2、successForwardUrl始終在認證成功之后跳轉(zhuǎn)到指定請求,defaultSuccessUrl根據(jù)上一保存請求進行成功跳轉(zhuǎn)

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

相關(guān)文章

  • SpringBoot整合SpringDataRedis的示例代碼

    SpringBoot整合SpringDataRedis的示例代碼

    這篇文章主要介紹了SpringBoot整合SpringDataRedis的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • SpringBoot返回統(tǒng)一的JSON標(biāo)準(zhǔn)格式實現(xiàn)步驟

    SpringBoot返回統(tǒng)一的JSON標(biāo)準(zhǔn)格式實現(xiàn)步驟

    這篇文章主要介紹了SpringBoot返回統(tǒng)一的JSON標(biāo)準(zhǔn)格式,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • springboot-controller的使用詳解

    springboot-controller的使用詳解

    本篇文章主要介紹了springboot-controller的使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • 使用Swagger實現(xiàn)接口版本號管理方式

    使用Swagger實現(xiàn)接口版本號管理方式

    這篇文章主要介紹了使用Swagger實現(xiàn)接口版本號管理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • @GrpcServise?注解的作用和使用示例詳解

    @GrpcServise?注解的作用和使用示例詳解

    @GrpcService 是一個 Spring Boot 處理器,它會查找實現(xiàn)了 grpc::BindableService 接口的類,并將其包裝成一個 Spring Bean 對象,這篇文章主要介紹了@GrpcServise?注解的作用和使用,需要的朋友可以參考下
    2023-05-05
  • java在hashmap初始化時賦初值過程解析

    java在hashmap初始化時賦初值過程解析

    這篇文章主要介紹了java在hashmap初始化時賦初值過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • commons fileupload實現(xiàn)文件上傳的實例代碼

    commons fileupload實現(xiàn)文件上傳的實例代碼

    這篇文章主要介紹了commons fileupload實現(xiàn)文件上傳的實例代碼,包括文件上傳的原理分析等相關(guān)知識點,本文給大家介紹的非常詳細,具有參考借鑒價值,感興趣的朋友一起看看吧
    2016-10-10
  • java讀寫ini文件、FileOutputStream問題

    java讀寫ini文件、FileOutputStream問題

    這篇文章主要介紹了java讀寫ini文件、FileOutputStream問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • IDEA實現(xiàn)序列化時如何自動生成serialVersionUID的步驟

    IDEA實現(xiàn)序列化時如何自動生成serialVersionUID的步驟

    這篇文章主要介紹了IDEA實現(xiàn)序列化時如何自動生成serialVersionUID的步驟,首先安裝GenerateSerialVersionUID插件,當(dāng)出現(xiàn)添加serialVersionUID選項,選中則會自動生成serialVersionUID,感興趣的朋友一起學(xué)習(xí)下吧
    2024-02-02
  • Java并發(fā)編程中的CompletableFuture使用詳解

    Java并發(fā)編程中的CompletableFuture使用詳解

    這篇文章主要介紹了Java并發(fā)編程中的CompletableFuture使用詳解,Future接口定義了操作異步任務(wù)執(zhí)行的一些方法,如獲取異步任務(wù)執(zhí)行的結(jié)果、取消任務(wù)的執(zhí)行、判斷任務(wù)是否被取消,判斷任務(wù)是否執(zhí)行完畢等,需要的朋友可以參考下
    2023-12-12

最新評論