最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot淺析安全管理之高級配置

 更新時間:2022年08月12日 15:33:57   作者:一只小熊貓呀  
安全管理是軟件系統(tǒng)必不可少的的功能。根據(jù)經(jīng)典的“墨菲定律”——凡是可能,總會發(fā)生。如果系統(tǒng)存在安全隱患,最終必然會出現(xiàn)問題,這篇文章主要介紹了SpringBoot安全管理之高級配置

角色繼承

SpringBoot淺析安全管理之基于數(shù)據(jù)庫認(rèn)證中定義了三種角色,但是這三種角色之間不具備任何關(guān)系,一般來說角色之間是有關(guān)系的,例如 ROLE_admin 一般既具有 admin 權(quán)限,又具有 user 權(quán)限。那么如何配置這種角色繼承關(guān)系呢?只需要開發(fā)者在 Spring Security 的配置類中提供一個 RoleHierarchy 即可

@Bean
RoleHierarchy roleHierarchy() {
    RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
    String hierarchy = "ROLE_dba > ROLE_admin ROLE_admin > ROLE_user";
    roleHierarchy.setHierarchy(hierarchy);
    return roleHierarchy;
}

配置完 RoleHierarchy 之后,具有 ROLE_dba 角色的用戶就可以訪問 所有資源了,具有 ROLE_admin 角色的用戶也可以訪問具有 ROLE_user 角色才能訪問的資源。

動態(tài)權(quán)限配置

使用 HttpSecurity 配置的認(rèn)證授權(quán)規(guī)則還是不夠靈活,無法實現(xiàn)資源和角色之間的動態(tài)調(diào)整,要實現(xiàn)動態(tài)配置 URL 權(quán)限,需要開發(fā)者自定義權(quán)限配置,配置步驟如下傳送門

1. 數(shù)據(jù)庫設(shè)計

在 10.2節(jié) 數(shù)據(jù)庫的基礎(chǔ)上再增加一張資源表和資源角色關(guān)聯(lián)表,資源表中定義了用戶能夠訪問的 URL 模式,資源角色表則定義了訪問該模式的 URL 需要什么樣的角色

建表語句

CREATE TABLE `menu` (
  `id` int(11) NOT NULL,
  `pattern` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `menu_role` (
  `id` int(11) NOT NULL,
  `mid` int(11) DEFAULT NULL,
  `rid` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

初始化數(shù)據(jù)

INSERT INTO `menu`(`id`, `pattern`) VALUES (1, '/db/**');
INSERT INTO `menu`(`id`, `pattern`) VALUES (2, '/admin/**');
INSERT INTO `menu`(`id`, `pattern`) VALUES (3, '/user/**');
INSERT INTO `menu_role`(`id`, `mid`, `rid`) VALUES (1, 1, 1);
INSERT INTO `menu_role`(`id`, `mid`, `rid`) VALUES (2, 2, 2);
INSERT INTO `menu_role`(`id`, `mid`, `rid`) VALUES (3, 3, 3);

Menu 實體類

public class Menu {
    private Integer id;
    private String pattern;
    private List<Role> roles;
    public List<Role> getRoles() {
        return roles;
    }
    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getPattern() {
        return pattern;
    }
    public void setPattern(String pattern) {
        this.pattern = pattern;
    }
}

MenuMapper

@Mapper
public interface MenuMapper {
    List<Menu> getAllMenus();
}

MenuMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.sang.mapper.MenuMapper">
    <resultMap id="BaseResultMap" type="org.sang.model.Menu">
        <id property="id" column="id"/>
        <result property="pattern" column="pattern"/>
        <collection property="roles" ofType="org.sang.model.Role">
            <id property="id" column="rid"/>
            <result property="name" column="rname"/>
            <result property="nameZh" column="rnameZh"/>
        </collection>
    </resultMap>
    <select id="getAllMenus" resultMap="BaseResultMap">
        SELECT m.*,r.id AS rid,r.name AS rname,r.nameZh AS rnameZh FROM menu m LEFT JOIN menu_role mr ON m.`id`=mr.`mid` LEFT JOIN role r ON mr.`rid`=r.`id`
    </select>
</mapper>

2. 自定義FilterInvocationSecurityMetadataSource

Spring Security 中通過 FilterInvocationSecurityMetadataSource 接口中的 getAttributes 方法來確定一個請求需要哪些角色,F(xiàn)ilterInvocationSecurityMetadataSource 接口默認(rèn)實現(xiàn)類是 DefaultFilterInvocationSecurityMetadataSource ,參考 DefaultFilterInvocationSecurityMetadataSource 的實現(xiàn),開發(fā)者可以定義自己的 FilterInvocationSecurityMetadataSource ,如下:

@Component
public class CustomFilterInvocationSecurityMetadataSource
        implements FilterInvocationSecurityMetadataSource {
    AntPathMatcher antPathMatcher = new AntPathMatcher();
    @Autowired
    MenuMapper menuMapper;
    @Override
    public Collection<ConfigAttribute> getAttributes(Object object)
            throws IllegalArgumentException {
        String requestUrl = ((FilterInvocation) object).getRequestUrl();
        List<Menu> allMenus = menuMapper.getAllMenus();
        for (Menu menu : allMenus) {
            if (antPathMatcher.match(menu.getPattern(), requestUrl)) {
                List<Role> roles = menu.getRoles();
                String[] roleArr = new String[roles.size()];
                for (int i = 0; i < roleArr.length; i++) {
                    roleArr[i] = roles.get(i).getName();
                }
                return SecurityConfig.createList(roleArr);
            }
        }
        return SecurityConfig.createList("ROLE_LOGIN");
    }
    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }
    @Override
    public boolean supports(Class<?> clazz) {
        return FilterInvocation.class.isAssignableFrom(clazz);
    }
}

代碼解釋:

  • 開發(fā)者自定義 FilterInvocationSecurityMetadataSource 主要實現(xiàn)接口中的 getAttributes 方法,該方法的參數(shù)是一個 FilterInvocation ,開發(fā)者可以從 FilterInvocation 中提取當(dāng)前請求的 URL ,返回值是 Collection,表示當(dāng)前請求 URL 所需角色
  • 創(chuàng)建一個 AntPathMatcher 主要用來實現(xiàn) ant 風(fēng)格的 URL 匹配
  • ((FilterInvocation) object).getRequestUrl(); 從參數(shù)中提取當(dāng)前請求的 URL
  • menuMapper.getAllMenus(); 從數(shù)據(jù)庫中獲取所有的資源信息,即 menu 表以及 menu 所對應(yīng)的 role,在真實項目環(huán)境中,開發(fā)者可以將資源信息緩存在 Redis 或者其他緩存數(shù)據(jù)庫中
  • 然后遍歷資源信息,遍歷過程中獲取當(dāng)前請求的 URL 所需要的角色信息并返回。如果當(dāng)前請求的 URL 在資源表中不存在相應(yīng)的模式,就假設(shè)該請求登錄后即可訪問,即直接返回 ROLE_LOGIN
  • getAllConfigAttributes 方法用來返回所有定義好的權(quán)限資源,Spring Security 在啟動時會校驗相關(guān)配置是否正確,如果不需要校驗,那么該方法直接返回 null 即可
  • supports 方法返回 類對象是否支持校驗

3. 自定義 AccessDecisionManager

當(dāng)一個請求走完 FilterInvocationSecurityMetadataSource 中的 getAttributes 方法后,接下來就會來到 AccessDecisionManager 類中進(jìn)行角色信息的比對,自定義 AccessDecisionManager 如下:

@Component
public class CustomAccessDecisionManager implements AccessDecisionManager {
    @Override
    public void decide(Authentication auth,
                       Object object,
                       Collection<ConfigAttribute> ca) {
        Collection<? extends GrantedAuthority> auths = auth.getAuthorities();
        for (ConfigAttribute configAttribute : ca) {
            if ("ROLE_LOGIN".equals(configAttribute.getAttribute())
                    && auth instanceof UsernamePasswordAuthenticationToken) {
                return;
            }
            for (GrantedAuthority authority : auths) {
                if (configAttribute.getAttribute().equals(authority.getAuthority())) {
                    return;
                }
            }
        }
        throw new AccessDeniedException("權(quán)限不足");
    }
    @Override
    public boolean supports(ConfigAttribute attribute) {
        return true;
    }
    @Override
    public boolean supports(Class<?> clazz) {
        return true;
    }
}

代碼解釋:

  • 自定義 AccessDecisionManager 并重寫 decide 方法,在該方法中判斷當(dāng)前登錄的用戶是否具備當(dāng)前請求 URL 所需要的角色信息,如果不具備,就拋出 AccessDeniedException 異常,否則不做任何事情
  • decide 有三個參數(shù),第一個參數(shù)包含當(dāng)前登錄用戶的信息;第二個參數(shù)則是一個 FilterInvocation 對象,可以獲取當(dāng)前請求對象等;第三個參數(shù)就是 UsernamePasswordAuthenticationToken 的實例,說明當(dāng)前用戶已登錄,該方法到此結(jié)束,否則進(jìn)入正常的判斷流程,如果當(dāng)前用戶具備當(dāng)前請求需要的角色,那么方法結(jié)束

4. 配置

最后,在 Spring Security 中配置上邊的兩個自定義類,代碼如下:

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    UserService userService;
    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService);
    }
    @Bean
    RoleHierarchy roleHierarchy() {
        RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
        String hierarchy = "ROLE_dba > ROLE_admin ROLE_admin > ROLE_user";
        roleHierarchy.setHierarchy(hierarchy);
        return roleHierarchy;
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
                    @Override
                    public <O extends FilterSecurityInterceptor> O postProcess(O object) {
                        object.setSecurityMetadataSource(cfisms());
                        object.setAccessDecisionManager(cadm());
                        return object;
                    }
                })
                .and()
                .formLogin()
                .loginProcessingUrl("/login").permitAll()
                .and()
                .csrf().disable();
    }
    @Bean
    CustomFilterInvocationSecurityMetadataSource cfisms() {
        return new CustomFilterInvocationSecurityMetadataSource();
    }
    @Bean
    CustomAccessDecisionManager cadm() {
        return new CustomAccessDecisionManager();
    }
}

代碼解釋:

  • 此處 WebSecurityConfig 類的定義是對 10.2節(jié) 中 WebSecurityConfig 定義的補充,主要修改了 configure(HttpSecurity http) 方法的實現(xiàn)并添加了兩個 Bean
  • object.setSecurityMetadataSource(cfisms()); object.setAccessDecisionManager(cadm());將定義的兩個實例設(shè)置進(jìn)去

到此這篇關(guān)于SpringBoot淺析安全管理之高級配置的文章就介紹到這了,更多相關(guān)SpringBoot高級配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring Cloud 優(yōu)雅下線以及灰度發(fā)布實現(xiàn)

    Spring Cloud 優(yōu)雅下線以及灰度發(fā)布實現(xiàn)

    這篇文章主要介紹了Spring Cloud 優(yōu)雅下線以及灰度發(fā)布實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • spring boot 靜態(tài)資源處理方法

    spring boot 靜態(tài)資源處理方法

    本篇文章主要介紹了spring boot 靜態(tài)資源處理方法。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • spring?boot+mybatis-plus配置讀寫分離的操作

    spring?boot+mybatis-plus配置讀寫分離的操作

    這篇文章主要介紹了spring?boot+mybatis-plus配置讀寫分離的操作,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-01-01
  • SpringBoot接口返回的數(shù)據(jù)時間與實際相差8小時問題排查方式

    SpringBoot接口返回的數(shù)據(jù)時間與實際相差8小時問題排查方式

    文章描述了在部署SpringBoot應(yīng)用到容器中時遇到請求接口返回時間與實際相差8小時的問題,并詳細(xì)分析了可能的原因及具體的排查步驟和解決方案,總結(jié)指出,環(huán)境初始時區(qū)未配置是根本原因,建議在應(yīng)用部署前配置好時區(qū)
    2025-02-02
  • springboot項目數(shù)據(jù)庫密碼如何加密

    springboot項目數(shù)據(jù)庫密碼如何加密

    在我們?nèi)粘i_發(fā)中,我們可能很隨意把數(shù)據(jù)庫密碼直接明文暴露在配置文件中,今天就來聊聊在springboot項目中如何對數(shù)據(jù)庫密碼進(jìn)行加密,感興趣的可以了解一下
    2021-07-07
  • Java實現(xiàn)簡單的抽牌游戲

    Java實現(xiàn)簡單的抽牌游戲

    這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)簡單的抽牌游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • 學(xué)習(xí)Java之如何正確地向上轉(zhuǎn)型與向下轉(zhuǎn)型

    學(xué)習(xí)Java之如何正確地向上轉(zhuǎn)型與向下轉(zhuǎn)型

    面向?qū)ο蟮牡谌齻€特征是多態(tài),實現(xiàn)多態(tài)有三個必要條件:繼承、方法重寫和向上轉(zhuǎn)型,在學(xué)習(xí)多態(tài)之前,我們還要先學(xué)習(xí)Java的類型轉(zhuǎn)換,本篇文章就來帶大家認(rèn)識什么是類型轉(zhuǎn)換,看看類型轉(zhuǎn)換都有哪幾種情況,以及如何避免類型轉(zhuǎn)換時出現(xiàn)異常
    2023-05-05
  • SWT(JFace)體驗之ViewForm的使用

    SWT(JFace)體驗之ViewForm的使用

    SWT(JFace)體驗之ViewForm的使用
    2009-06-06
  • Spring定時任務(wù)使用及如何使用郵件監(jiān)控服務(wù)器

    Spring定時任務(wù)使用及如何使用郵件監(jiān)控服務(wù)器

    這篇文章主要介紹了Spring定時任務(wù)使用及如何使用郵件監(jiān)控服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • Java線程通信及線程虛假喚醒知識總結(jié)

    Java線程通信及線程虛假喚醒知識總結(jié)

    今天給大家?guī)淼氖顷P(guān)于Java線程的相關(guān)知識,文章圍繞著Java線程通信及線程虛假喚醒的知識展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06

最新評論

蛟河市| 大悟县| 玛纳斯县| 延边| 望城县| 榆林市| 阳谷县| 普陀区| 贵阳市| 双鸭山市| 当雄县| 定西市| 星子县| 当涂县| 灵山县| 衡东县| 博白县| 红安县| 穆棱市| 社旗县| 普陀区| 乌鲁木齐县| 隆安县| 庆云县| 法库县| 墨脱县| 汨罗市| 库伦旗| 武山县| 扎赉特旗| 宁城县| 黄龙县| 腾冲县| 汝阳县| 榆社县| 察哈| 敦煌市| 淳安县| 探索| 西乡县| 临清市|