關(guān)于SpringSecurity的基本使用示例
Spring Security核心功能 關(guān)于安全方面的兩個(gè)主要區(qū)域是認(rèn)證和授權(quán)(或者訪問(wèn)控制),這兩點(diǎn)也是Spring Security核心功能 用戶(hù)認(rèn)證:驗(yàn)證某個(gè)用戶(hù)是否為系統(tǒng)中的合法主體,也就是說(shuō)用戶(hù)能否訪問(wèn)該系統(tǒng)。用戶(hù)認(rèn)證一般要求用戶(hù)提供用戶(hù)名和密碼。系統(tǒng)通過(guò)校驗(yàn)用戶(hù)名和密碼來(lái)完成認(rèn)證過(guò)程。 用戶(hù)授權(quán):驗(yàn)證謀而用戶(hù)是否有權(quán)限執(zhí)行某一操作。在一個(gè)系統(tǒng)中,不同用戶(hù)所具有的權(quán)限是不同的。
本質(zhì):就是一堆過(guò)濾器鏈
一、使用
1、直接引入依賴(lài)使用(不連接數(shù)據(jù)庫(kù))
1.1 引入依賴(lài)
<!--springsecurity-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>1.2 啟動(dòng)項(xiàng)目隨機(jī)訪問(wèn)controller的一個(gè)接口

賬號(hào)為user,密碼為控制臺(tái)輸出的密碼:

2、結(jié)合數(shù)據(jù)庫(kù)使用
2.1 編寫(xiě)類(lèi)實(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;
/**
* 需新建配置類(lèi)注冊(cè)一個(gè)指定的加密方式Bean,或在下一步Security配置類(lèi)中注冊(cè)指定
*/
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// 通過(guò)用戶(hù)名從數(shù)據(jù)庫(kù)獲取用戶(hù)信息
com.zzh.activiti.entity.User userInfo = userInfoService.getUserInfo(username);
if (userInfo == null) {
throw new UsernameNotFoundException("用戶(hù)不存在");
}
// 得到用戶(hù)角色
String role = userInfo.getRole();
// 角色集合
List<GrantedAuthority> authorities = new ArrayList<>();
// 角色必須以`ROLE_`開(kāi)頭,數(shù)據(jù)庫(kù)中沒(méi)有,則在這里加
authorities.add(new SimpleGrantedAuthority("ROLE_" + role));
return new User(
userInfo.getName(),
// 因?yàn)閿?shù)據(jù)庫(kù)是明文,所以這里需加密密碼
passwordEncoder.encode(userInfo.getPassword()),
authorities
);
}
}2.2 編寫(xiě)類(lèi)繼承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;
// 開(kāi)啟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ù)庫(kù)讀取的用戶(hù)進(jìn)行身份認(rèn)證
.userDetailsService(userDatailService)
.passwordEncoder(passwordEncoder());
}
}2.3瀏覽器訪問(wèn)controller層隨意接口

到此這篇關(guān)于關(guān)于SpringSecurity的基本使用示例的文章就介紹到這了,更多相關(guān)SpringSecurity基礎(chǔ)詳解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot在 POM 中引入本地 JAR 包的方法
在開(kāi)發(fā) Spring Boot 應(yīng)用程序時(shí),您可能需要使用本地 JAR 包來(lái)添加自定義庫(kù)或功能,本文將介紹在 Spring Boot 項(xiàng)目的 POM 文件中如何引入本地 JAR 包,感興趣的朋友跟隨小編一起看看吧2023-08-08
Java中@ConditionalOnProperty注解使用
在Spring?Boot中,@ConditionalOnProperty注解是一種方便的工具,用于根據(jù)應(yīng)用程序配置文件中的屬性值來(lái)控制Bean的創(chuàng)建和加載,本文就來(lái)介紹一下Java中@ConditionalOnProperty注解使用,感興趣的可以了解一下2023-11-11
Spring?Boot自定義?Starter并推送到遠(yuǎn)端公服的詳細(xì)代碼
這篇文章主要介紹了Spring?Boot自定義?Starter并推送到遠(yuǎn)端公服,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
Action訪問(wèn)Servlet的API的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇Action訪問(wèn)Servlet的API的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
SpringBoot項(xiàng)目從18.18M瘦身到0.18M的實(shí)現(xiàn)
本文主要介紹了SpringBoot項(xiàng)目從18.18M瘦身到0.18M的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Java ArrayList擴(kuò)容問(wèn)題實(shí)例詳解
這篇文章主要介紹了Java ArrayList擴(kuò)容問(wèn)題實(shí)例詳解,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
Spark SQL關(guān)于性能調(diào)優(yōu)選項(xiàng)詳解
這篇文章將為大家詳細(xì)講解有關(guān)Spark SQL性能調(diào)優(yōu)選項(xiàng),小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲2023-02-02

