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

內(nèi)置訪問(wèn)控制方法
permitAll()表示所匹配的 URL 任何人都允許訪問(wèn)。authenticated()表示所匹配的 URL 都需要被認(rèn)證才能訪問(wèn)。anonymous()表示可以匿名訪問(wèn)匹配的 URL 。和 permitAll() 效果類似,只是設(shè)置為 anonymous() 的 url 會(huì)執(zhí)行 filter 鏈中denyAll()表示所匹配的 URL 都不允許被訪問(wèn)。rememberMe()被“remember me”的用戶允許訪問(wèn) 這個(gè)有點(diǎn)類似于很多網(wǎng)站的十天內(nèi)免登錄,登陸一次即可記住你,然后未來(lái)一段時(shí)間不用登錄。fullyAuthenticated()如果用戶不是被 remember me 的,才可以訪問(wèn)。也就是必須一步一步按部就班的登錄才行。
角色權(quán)限判斷
hasAuthority(String)判斷用戶是否具有特定的權(quán)限,用戶的權(quán)限是在自定義登錄邏輯hasAnyAuthority(String ...)如果用戶具備給定權(quán)限中某一個(gè),就允許訪問(wèn)hasRole(String)如果用戶具備給定角色就允許訪問(wèn)。否則出現(xiàn) 403hasAnyRole(String ...)如果用戶具備給定角色的任意一個(gè),就允許被訪問(wèn)hasIpAddress(String)如果請(qǐng)求是指定的 IP 就運(yùn)行訪問(wèn)。可以通過(guò) 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)用中往往需要自定義配置,因此接下來(lái)對(duì)Spring Security進(jìn)行自定義配置。
配置 Spring Security (入門(mén))
在內(nèi)存中(簡(jiǎn)化環(huán)節(jié),了解邏輯)配置兩個(gè)用戶角色(admin和user),設(shè)置不同密碼;
同時(shí)設(shè)置角色訪問(wèn)權(quán)限,其中admin可以訪問(wèn)所有路徑(即/*),user只能訪問(wèn)/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ù)庫(kù)、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()
//配置攔截的路徑、配置哪類角色可以訪問(wèn)該路徑
.antMatchers("/user").hasAnyRole("USER")
.antMatchers("/*").hasAnyRole("ADMIN")
//配置登錄界面,可以添加自定義界面, 沒(méi)添加則用系統(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)不同頁(yè)面
- SpringBoot整合SpringSecurity實(shí)現(xiàn)圖形驗(yàn)證碼功能
- SpringSecurity集成圖片驗(yàn)證碼的詳細(xì)過(guò)程
- 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ù)庫(kù)中獲取用戶信息進(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)文章
java基于JDBC連接Oracle 11g Release2實(shí)例分析
這篇文章主要介紹了java基于JDBC連接Oracle 11g Release2的方法,實(shí)例分析了JDBC連接Oracle 11g Release2容易出現(xiàn)的異常與解決方法,需要的朋友可以參考下2015-06-06
java學(xué)習(xí)筆記之DBUtils工具包詳解
下面小編就為大家分享一篇java學(xué)習(xí)筆記之DBUtils工具包詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Java設(shè)計(jì)模式之建造者模式實(shí)例詳解
這篇文章主要介紹了Java設(shè)計(jì)模式之建造者模式,結(jié)合具體實(shí)例形式分析了建造者模式的概念、原理、實(shí)現(xiàn)方法與相關(guān)使用注意事項(xiàng),需要的朋友可以參考下2017-09-09
Spring Security中successHandler無(wú)效問(wèn)題及解決
這篇文章主要介紹了Spring Security中successHandler無(wú)效問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

