溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用Java SpringBoot安全框架去整合Spring Security

發布時間:2021-09-14 11:28:54 來源:億速云 閱讀:217 作者:柒染 欄目:開發技術

如何使用Java SpringBoot安全框架去整合Spring Security,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。

    1.工業級安全框架介紹

    Spring Security基于Spring開發,項目中如果使用Spring作為基礎,配合Spring Security做權限更加方便,而Shiro需要和Spring進行整合開發。因此作為spring全家桶中的Spring Security在java領域很常用。

    2.建議搭建Spring Security環境

    2.1在pom.xml中添加相關依賴

    <?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">
        <modelVersion>4.0.0</modelVersion>
        <groupId>org.example</groupId>
        <artifactId>springsecurityReview</artifactId>
        <version>1.0-SNAPSHOT</version>
        <parent>
            <artifactId>spring-boot-dependencies</artifactId>
            <groupId>org.springframework.boot</groupId>
            <version>2.5.4</version>
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
        </dependencies>
    </project>

    2.2創建Handler類

    package com.example.controller;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    @Controller
    public class Handler {
        @GetMapping("/index")
        public String index(){
            return "index";
        }
    }

    2.3創建簡單的html和配置相關thymeleaf的路徑

    如何使用Java SpringBoot安全框架去整合Spring Security

    2.4最后再加個啟動類,那么我們的整合測試就完成勒

    package com.example;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class,args);
        }
    }

    2.5成果展示 用戶名默認user,密碼則隨機生成的這串數字

    如何使用Java SpringBoot安全框架去整合Spring Security

    如何使用Java SpringBoot安全框架去整合Spring Security

    3.進階版使用

    3.1用戶名和密碼自定義

    如何使用Java SpringBoot安全框架去整合Spring Security

    3.2在config包下創建Encoder

    進行密碼的校驗和轉碼操作,將密碼轉成字符串形式,并通過match方法驚醒校驗。

    如何使用Java SpringBoot安全框架去整合Spring Security

    3.3賦予賬號角色權限

    package com.example.config;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        //角色和資源的關系
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().antMatchers("/admin").hasRole("ADMIN")
            .antMatchers("/index").access("hasRole('ADMIN') or hasRole('USER') ")
                    .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
            .logout()
            .permitAll()
            .and()
            .csrf()
            .disable();
        }
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
           auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder())
           .withUser("user").password(new MyPasswordEncoder()
           .encode("000")).roles("USER")
           .and()
           .withUser("admin").password(new MyPasswordEncoder()
           .encode("123")).roles("ADMIN","USER");
        }
    }

    最后達到admin賬號能訪問admin.html和index.html

    user只能訪問index.html的操作

    看完上述內容,你們掌握如何使用Java SpringBoot安全框架去整合Spring Security的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

    向AI問一下細節

    免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

    AI

    亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女