關(guān)于SpringSecurity的基本使用示例
Spring Security核心功能 關(guān)于安全方面的兩個(gè)主要區(qū)域是認(rèn)證和授權(quán)(或者訪問控制),這兩點(diǎn)也是Spring Security核心功能 用戶認(rèn)證:驗(yàn)證某個(gè)用戶是否為系統(tǒng)中的合法主體,也就是說用戶能否訪問該系統(tǒng)。用戶認(rèn)證一般要求用戶提供用戶名和密碼。系統(tǒng)通過校驗(yàn)用戶名和密碼來完成認(rèn)證過程。 用戶授權(quán):驗(yàn)證謀而用戶是否有權(quán)限執(zhí)行某一操作。在一個(gè)系統(tǒng)中,不同用戶所具有的權(quán)限是不同的。
本質(zhì):就是一堆過濾器鏈
一、使用
1、直接引入依賴使用(不連接數(shù)據(jù)庫)
1.1 引入依賴
<!--springsecurity--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
1.2 啟動(dòng)項(xiàng)目隨機(jī)訪問controller的一個(gè)接口
賬號(hào)為user,密碼為控制臺(tái)輸出的密碼:
2、結(jié)合數(shù)據(jù)庫使用
2.1 編寫類實(shí)現(xiàn)UserDetailsService接口
package com.zzh.activiti.config.securityservice; import com.zzh.activiti.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @Component public class UserDetailService implements UserDetailsService { @Autowired private UserService userInfoService; /** * 需新建配置類注冊(cè)一個(gè)指定的加密方式Bean,或在下一步Security配置類中注冊(cè)指定 */ @Autowired private PasswordEncoder passwordEncoder; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { // 通過用戶名從數(shù)據(jù)庫獲取用戶信息 com.zzh.activiti.entity.User userInfo = userInfoService.getUserInfo(username); if (userInfo == null) { throw new UsernameNotFoundException("用戶不存在"); } // 得到用戶角色 String role = userInfo.getRole(); // 角色集合 List<GrantedAuthority> authorities = new ArrayList<>(); // 角色必須以`ROLE_`開頭,數(shù)據(jù)庫中沒有,則在這里加 authorities.add(new SimpleGrantedAuthority("ROLE_" + role)); return new User( userInfo.getName(), // 因?yàn)閿?shù)據(jù)庫是明文,所以這里需加密密碼 passwordEncoder.encode(userInfo.getPassword()), authorities ); } }
2.2 編寫類繼承WebSecurityConfigurerAdapter實(shí)現(xiàn)securiy配置
package com.zzh.activiti.config; import com.zzh.activiti.config.securityservice.UserDetailService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 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; import org.springframework.stereotype.Component; // 開啟Security配置 @EnableWebSecurity @Configuration @Slf4j public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailService userDatailService; /** * 指定加密方式 */ @Bean public PasswordEncoder passwordEncoder(){ // 使用BCrypt加密密碼 return new BCryptPasswordEncoder(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { log.error("aaa"); auth // 從數(shù)據(jù)庫讀取的用戶進(jìn)行身份認(rèn)證 .userDetailsService(userDatailService) .passwordEncoder(passwordEncoder()); } }
2.3瀏覽器訪問controller層隨意接口
到此這篇關(guān)于關(guān)于SpringSecurity的基本使用示例的文章就介紹到這了,更多相關(guān)SpringSecurity基礎(chǔ)詳解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot在 POM 中引入本地 JAR 包的方法
在開發(fā) Spring Boot 應(yīng)用程序時(shí),您可能需要使用本地 JAR 包來添加自定義庫或功能,本文將介紹在 Spring Boot 項(xiàng)目的 POM 文件中如何引入本地 JAR 包,感興趣的朋友跟隨小編一起看看吧2023-08-08Java中@ConditionalOnProperty注解使用
在Spring?Boot中,@ConditionalOnProperty注解是一種方便的工具,用于根據(jù)應(yīng)用程序配置文件中的屬性值來控制Bean的創(chuàng)建和加載,本文就來介紹一下Java中@ConditionalOnProperty注解使用,感興趣的可以了解一下2023-11-11Spring?Boot自定義?Starter并推送到遠(yuǎn)端公服的詳細(xì)代碼
這篇文章主要介紹了Spring?Boot自定義?Starter并推送到遠(yuǎn)端公服,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09Action訪問Servlet的API的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄狝ction訪問Servlet的API的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06SpringBoot項(xiàng)目從18.18M瘦身到0.18M的實(shí)現(xiàn)
本文主要介紹了SpringBoot項(xiàng)目從18.18M瘦身到0.18M的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Java ArrayList擴(kuò)容問題實(shí)例詳解
這篇文章主要介紹了Java ArrayList擴(kuò)容問題實(shí)例詳解,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02Spark SQL關(guān)于性能調(diào)優(yōu)選項(xiàng)詳解
這篇文章將為大家詳細(xì)講解有關(guān)Spark SQL性能調(diào)優(yōu)選項(xiàng),小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲2023-02-02