SpringBoot?SpringSecurity?詳細(xì)介紹(基于內(nèi)存的驗(yàn)證)
SpringBoot 集成 SpringSecurity + MySQL + JWT 附源碼,廢話不多直接盤
SpringBoot已經(jīng)為用戶采用默認(rèn)配置,只需要引入pom依賴就能快速啟動(dòng)Spring Security。
目的:驗(yàn)證請(qǐng)求用戶的身份,提供安全訪問
優(yōu)勢(shì):基于Spring,配置方便,減少大量代碼

內(nèi)置訪問控制方法
permitAll()表示所匹配的 URL 任何人都允許訪問。authenticated()表示所匹配的 URL 都需要被認(rèn)證才能訪問。anonymous()表示可以匿名訪問匹配的 URL 。和 permitAll() 效果類似,只是設(shè)置為 anonymous() 的 url 會(huì)執(zhí)行 filter 鏈中denyAll()表示所匹配的 URL 都不允許被訪問。rememberMe()被“remember me”的用戶允許訪問 這個(gè)有點(diǎn)類似于很多網(wǎng)站的十天內(nèi)免登錄,登陸一次即可記住你,然后未來一段時(shí)間不用登錄。fullyAuthenticated()如果用戶不是被 remember me 的,才可以訪問。也就是必須一步一步按部就班的登錄才行。
角色權(quán)限判斷
hasAuthority(String)判斷用戶是否具有特定的權(quán)限,用戶的權(quán)限是在自定義登錄邏輯hasAnyAuthority(String ...)如果用戶具備給定權(quán)限中某一個(gè),就允許訪問hasRole(String)如果用戶具備給定角色就允許訪問。否則出現(xiàn) 403hasAnyRole(String ...)如果用戶具備給定角色的任意一個(gè),就允許被訪問hasIpAddress(String)如果請(qǐng)求是指定的 IP 就運(yùn)行訪問??梢酝ㄟ^ request.getRemoteAddr() 獲取 ip 地址
引用 Spring Security
Pom 文件中添加
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>vipsoft-parent</artifactId>
<groupId>com.vipsoft.boot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>vipsoft-security</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.7</version>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>運(yùn)行后會(huì)自動(dòng)生成 password 默認(rèn)用戶名為: user

默認(rèn)配置每次都啟動(dòng)項(xiàng)目都會(huì)重新生成密碼,同時(shí)用戶名和攔截請(qǐng)求也不能自定義,在實(shí)際應(yīng)用中往往需要自定義配置,因此接下來對(duì)Spring Security進(jìn)行自定義配置。
配置 Spring Security (入門)
在內(nèi)存中(簡(jiǎn)化環(huán)節(jié),了解邏輯)配置兩個(gè)用戶角色(admin和user),設(shè)置不同密碼;
同時(shí)設(shè)置角色訪問權(quán)限,其中admin可以訪問所有路徑(即/*),user只能訪問/user下的所有路徑。
自定義配置類,實(shí)現(xiàn)WebSecurityConfigurerAdapter接口,WebSecurityConfigurerAdapter接口中有兩個(gè)用到的 configure()方法,其中一個(gè)配置用戶身份,另一個(gè)配置用戶權(quán)限:
配置用戶身份的configure()方法:
SecurityConfig
package com.vipsoft.web.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/**
* 配置用戶身份的configure()方法
*
* @param auth
* @throws Exception
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
//簡(jiǎn)化操作,將用戶名和密碼存在內(nèi)存中,后期會(huì)存放在數(shù)據(jù)庫、Redis中
auth.inMemoryAuthentication()
.passwordEncoder(passwordEncoder)
.withUser("admin")
.password(passwordEncoder.encode("888"))
.roles("ADMIN")
.and()
.withUser("user")
.password(passwordEncoder.encode("666"))
.roles("USER");
}
/**
* 配置用戶權(quán)限的configure()方法
* @param http
* @throws Exception
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
//配置攔截的路徑、配置哪類角色可以訪問該路徑
.antMatchers("/user").hasAnyRole("USER")
.antMatchers("/*").hasAnyRole("ADMIN")
//配置登錄界面,可以添加自定義界面, 沒添加則用系統(tǒng)默認(rèn)的界面
.and().formLogin();
}
}添加接口測(cè)試用
package com.vipsoft.web.controller;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DefaultController {
@GetMapping("/")
@PreAuthorize("hasRole('ADMIN')")
public String demo() {
return "Welcome";
}
@GetMapping("/user/list")
@PreAuthorize("hasAnyRole('ADMIN','USER')")
public String getUserList() {
return "User List";
}
@GetMapping("/article/list")
@PreAuthorize("hasRole('ADMIN')")
public String getArticleList() {
return "Article List";
}
} 
到此這篇關(guān)于SpringBoot SpringSecurity 介紹(基于內(nèi)存的驗(yàn)證)的文章就介紹到這了,更多相關(guān)SpringBoot SpringSecurity內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springsecurity實(shí)現(xiàn)登錄驗(yàn)證以及根據(jù)用戶身份跳轉(zhuǎn)不同頁面
- SpringBoot整合SpringSecurity實(shí)現(xiàn)圖形驗(yàn)證碼功能
- SpringSecurity集成圖片驗(yàn)證碼的詳細(xì)過程
- SpringSecurity添加圖形驗(yàn)證碼認(rèn)證實(shí)現(xiàn)
- SpringBoot+SpringSecurity+jwt實(shí)現(xiàn)驗(yàn)證
- Springboot+SpringSecurity實(shí)現(xiàn)圖片驗(yàn)證碼登錄的示例
- SpringSecurity從數(shù)據(jù)庫中獲取用戶信息進(jìn)行驗(yàn)證的案例詳解
- SpringSecurity實(shí)現(xiàn)圖形驗(yàn)證碼功能的實(shí)例代碼
- SpringBoot + SpringSecurity 短信驗(yàn)證碼登錄功能實(shí)現(xiàn)
- SpringSecurity實(shí)現(xiàn)多種身份驗(yàn)證方式
相關(guān)文章
詳解mybatis collection標(biāo)簽一對(duì)多的使用
這篇文章主要介紹了mybatis collection標(biāo)簽一對(duì)多的使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Spring Boot JPA Repository之existsBy查詢方法失效的解決
這篇文章主要介紹了Spring Boot JPA Repository之existsBy查詢方法失效的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Spring Boot整合Spring Security簡(jiǎn)單實(shí)現(xiàn)登入登出從零搭建教程
這篇文章主要給大家介紹了關(guān)于Spring Boot整合Spring Security簡(jiǎn)單實(shí)現(xiàn)登入登出從零搭建的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2018-09-09
java字符串日期類Date和Calendar相互轉(zhuǎn)化及相關(guān)常用方法
Java語言的Calendar(日歷),Date(日期),和DateFormat(日期格式)組成了Java標(biāo)準(zhǔn)的一個(gè)基本但是非常重要的部分,下面這篇文章主要給大家介紹了關(guān)于java字符串日期類Date和Calendar相互轉(zhuǎn)化及相關(guān)常用方法的相關(guān)資料,需要的朋友可以參考下2023-12-12
簡(jiǎn)單實(shí)現(xiàn)java上傳圖片文件功能
這篇文章主要教大家如何簡(jiǎn)單實(shí)現(xiàn)java上傳圖片文件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
java發(fā)送javax.mail郵件實(shí)例講解
這篇文章主要為大家介紹了java發(fā)送javax.mail郵件實(shí)例講解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01

