欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于SpringSecurity的基本使用示例

 更新時間:2023年05月06日 09:34:02   作者:BoldExplorer  
這篇文章主要介紹了關(guān)于SpringSecurity的基本使用示例,SpringSecurity 本質(zhì)是一個過濾器鏈SpringSecurity 采用的是責(zé)任鏈的設(shè)計模式,它有一條很長的過濾器鏈,需要的朋友可以參考下

Spring Security核心功能 關(guān)于安全方面的兩個主要區(qū)域是認證和授權(quán)(或者訪問控制),這兩點也是Spring Security核心功能 用戶認證:驗證某個用戶是否為系統(tǒng)中的合法主體,也就是說用戶能否訪問該系統(tǒng)。用戶認證一般要求用戶提供用戶名和密碼。系統(tǒng)通過校驗用戶名和密碼來完成認證過程。 用戶授權(quán):驗證謀而用戶是否有權(quán)限執(zhí)行某一操作。在一個系統(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 啟動項目隨機訪問controller的一個接口

賬號為user,密碼為控制臺輸出的密碼:

2、結(jié)合數(shù)據(jù)庫使用

2.1 編寫類實現(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;
    /**
     * 需新建配置類注冊一個指定的加密方式Bean,或在下一步Security配置類中注冊指定
     */
    @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(),
                // 因為數(shù)據(jù)庫是明文,所以這里需加密密碼
                passwordEncoder.encode(userInfo.getPassword()),
                authorities
        );
    }
}

2.2 編寫類繼承WebSecurityConfigurerAdapter實現(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ù)庫讀取的用戶進行身份認證
                .userDetailsService(userDatailService)
                .passwordEncoder(passwordEncoder());
    }
}

2.3瀏覽器訪問controller層隨意接口

到此這篇關(guān)于關(guān)于SpringSecurity的基本使用示例的文章就介紹到這了,更多相關(guān)SpringSecurity基礎(chǔ)詳解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot在 POM 中引入本地 JAR 包的方法

    SpringBoot在 POM 中引入本地 JAR 包的方法

    在開發(fā) Spring Boot 應(yīng)用程序時,您可能需要使用本地 JAR 包來添加自定義庫或功能,本文將介紹在 Spring Boot 項目的 POM 文件中如何引入本地 JAR 包,感興趣的朋友跟隨小編一起看看吧
    2023-08-08
  • Java中@ConditionalOnProperty注解使用

    Java中@ConditionalOnProperty注解使用

    在Spring?Boot中,@ConditionalOnProperty注解是一種方便的工具,用于根據(jù)應(yīng)用程序配置文件中的屬性值來控制Bean的創(chuàng)建和加載,本文就來介紹一下Java中@ConditionalOnProperty注解使用,感興趣的可以了解一下
    2023-11-11
  • Spring?Boot自定義?Starter并推送到遠端公服的詳細代碼

    Spring?Boot自定義?Starter并推送到遠端公服的詳細代碼

    這篇文章主要介紹了Spring?Boot自定義?Starter并推送到遠端公服,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-09-09
  • Java線程池中的工作線程Worker類源碼解析

    Java線程池中的工作線程Worker類源碼解析

    這篇文章主要介紹了Java線程池中的工作線程Worker類源碼解析,線程池中的工作線程是通過內(nèi)部類Worker表示的,Worker繼承自AbstractQueueSynchronizer,可以實現(xiàn)同步器的功能,需要的朋友可以參考下
    2023-12-12
  • 從架構(gòu)思維角度分析高并發(fā)下冪等性解決方案

    從架構(gòu)思維角度分析高并發(fā)下冪等性解決方案

    冪等(idempotent、idempotence)是一個數(shù)學(xué)與計算機學(xué)概念,常見于抽象代數(shù)中。?在編程中.一個冪等操作的特點是其任意多次執(zhí)行所產(chǎn)生的影響均與一次執(zhí)行的影響相同。冪等函數(shù),或冪等方法,是指可以使用相同參數(shù)重復(fù)執(zhí)行,并能獲得相同結(jié)果的函數(shù)
    2022-01-01
  • 聊一聊Java反射

    聊一聊Java反射

    工作中哪些地方比較容易用到反射,這篇文章就為大家介紹了工作中常用到的Java反射,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Action訪問Servlet的API的簡單實例

    Action訪問Servlet的API的簡單實例

    下面小編就為大家?guī)硪黄狝ction訪問Servlet的API的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • SpringBoot項目從18.18M瘦身到0.18M的實現(xiàn)

    SpringBoot項目從18.18M瘦身到0.18M的實現(xiàn)

    本文主要介紹了SpringBoot項目從18.18M瘦身到0.18M的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Java ArrayList擴容問題實例詳解

    Java ArrayList擴容問題實例詳解

    這篇文章主要介紹了Java ArrayList擴容問題實例詳解,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • Spark SQL關(guān)于性能調(diào)優(yōu)選項詳解

    Spark SQL關(guān)于性能調(diào)優(yōu)選項詳解

    這篇文章將為大家詳細講解有關(guān)Spark SQL性能調(diào)優(yōu)選項,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲
    2023-02-02

最新評論