SpringBoot?SpringSecurity?詳細介紹(基于內(nèi)存的驗證)
SpringBoot 集成 SpringSecurity + MySQL + JWT 附源碼,廢話不多直接盤
SpringBoot已經(jīng)為用戶采用默認配置,只需要引入pom依賴就能快速啟動Spring Security。
目的:驗證請求用戶的身份,提供安全訪問
優(yōu)勢:基于Spring,配置方便,減少大量代碼
內(nèi)置訪問控制方法
permitAll()
表示所匹配的 URL 任何人都允許訪問。authenticated()
表示所匹配的 URL 都需要被認證才能訪問。anonymous()
表示可以匿名訪問匹配的 URL 。和 permitAll() 效果類似,只是設置為 anonymous() 的 url 會執(zhí)行 filter 鏈中denyAll()
表示所匹配的 URL 都不允許被訪問。rememberMe()
被“remember me”的用戶允許訪問 這個有點類似于很多網(wǎng)站的十天內(nèi)免登錄,登陸一次即可記住你,然后未來一段時間不用登錄。fullyAuthenticated()
如果用戶不是被 remember me 的,才可以訪問。也就是必須一步一步按部就班的登錄才行。
角色權(quán)限判斷
hasAuthority(String)
判斷用戶是否具有特定的權(quán)限,用戶的權(quán)限是在自定義登錄邏輯hasAnyAuthority(String ...)
如果用戶具備給定權(quán)限中某一個,就允許訪問hasRole(String)
如果用戶具備給定角色就允許訪問。否則出現(xiàn) 403hasAnyRole(String ...)
如果用戶具備給定角色的任意一個,就允許被訪問hasIpAddress(String)
如果請求是指定的 IP 就運行訪問??梢酝ㄟ^ 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>
運行后會自動生成 password 默認用戶名為: user
默認配置每次都啟動項目都會重新生成密碼,同時用戶名和攔截請求也不能自定義,在實際應用中往往需要自定義配置,因此接下來對Spring Security進行自定義配置。
配置 Spring Security (入門)
在內(nèi)存中(簡化環(huán)節(jié),了解邏輯)配置兩個用戶角色(admin和user),設置不同密碼;
同時設置角色訪問權(quán)限,其中admin可以訪問所有路徑(即/*),user只能訪問/user下的所有路徑。
自定義配置類,實現(xiàn)WebSecurityConfigurerAdapter
接口,WebSecurityConfigurerAdapter
接口中有兩個用到的 configure()方法,其中一個配置用戶身份,另一個配置用戶權(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(); //簡化操作,將用戶名和密碼存在內(nèi)存中,后期會存放在數(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)默認的界面 .and().formLogin(); } }
添加接口測試用
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)存的驗證)的文章就介紹到這了,更多相關(guān)SpringBoot SpringSecurity內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springsecurity實現(xiàn)登錄驗證以及根據(jù)用戶身份跳轉(zhuǎn)不同頁面
- SpringBoot整合SpringSecurity實現(xiàn)圖形驗證碼功能
- SpringSecurity集成圖片驗證碼的詳細過程
- SpringSecurity添加圖形驗證碼認證實現(xiàn)
- SpringBoot+SpringSecurity+jwt實現(xiàn)驗證
- Springboot+SpringSecurity實現(xiàn)圖片驗證碼登錄的示例
- SpringSecurity從數(shù)據(jù)庫中獲取用戶信息進行驗證的案例詳解
- SpringSecurity實現(xiàn)圖形驗證碼功能的實例代碼
- SpringBoot + SpringSecurity 短信驗證碼登錄功能實現(xiàn)
- SpringSecurity實現(xiàn)多種身份驗證方式
相關(guān)文章
java基于JDBC連接Oracle 11g Release2實例分析
這篇文章主要介紹了java基于JDBC連接Oracle 11g Release2的方法,實例分析了JDBC連接Oracle 11g Release2容易出現(xiàn)的異常與解決方法,需要的朋友可以參考下2015-06-06Spring Security中successHandler無效問題及解決
這篇文章主要介紹了Spring Security中successHandler無效問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08