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

Springboot安全框架整合SpringSecurity實現(xiàn)方式

 更新時間:2021年09月15日 09:12:12   作者:DrLai  
這篇文章主要介紹了Spring全家桶中Springboot安全框架整合SpringSecurity的實現(xiàn)方式,有需要的朋友可以借鑒參考下,希望可以有所幫助

1.工業(yè)級安全框架介紹

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

2.建議搭建Spring Security環(huán)境         

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創(chuàng)建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創(chuàng)建簡單的html和配置相關thymeleaf的路徑

          

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,密碼則隨機生成的這串數字

 

3.進階版使用        

3.1用戶名和密碼自定義

3.2在config包下創(chuàng)建Encoder進行密碼的校驗和轉碼操作

將密碼轉成字符串形式,并通過match方法驚醒校驗。 

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的操作

以上就是Springboot安全框架整合SpringSecurity實現(xiàn)方式的詳細內容,更多關于Springboot整合SpringSecurity的資料請關注腳本之家其它相關文章!

相關文章

  • 用Spring將Service注入到Servlet中的流程步驟

    用Spring將Service注入到Servlet中的流程步驟

    在Java Web開發(fā)中,??Servlet??是一個非常重要的組件,它用于處理客戶端的請求并生成響應,而?Spring??框架則是一個廣泛使用的依賴注入框架,可以幫助開發(fā)者管理應用中的對象及其依賴關系,本文將介紹如何使用Spring框架將Service層的對象注入到Servlet中
    2025-01-01
  • ApiOperation和ApiParam注解依賴的安裝和使用以及注意事項說明

    ApiOperation和ApiParam注解依賴的安裝和使用以及注意事項說明

    這篇文章主要介紹了ApiOperation和ApiParam注解依賴的安裝和使用以及注意事項說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Java?+?Selenium?+?OpenCV解決自動化測試中的滑塊驗證問題

    Java?+?Selenium?+?OpenCV解決自動化測試中的滑塊驗證問題

    OpenCV是一個基于Apache2.0許可(開源)發(fā)行的跨平臺計算機視覺和機器學習軟件庫,可以運行在Linux、Windows、Android和Mac?OS操作系統(tǒng)上,這篇文章主要介紹了Java?+?Selenium?+?OpenCV解決自動化測試中的滑塊驗證,需要的朋友可以參考下
    2022-07-07
  • Seata?環(huán)境搭建部署過程

    Seata?環(huán)境搭建部署過程

    Seata是一個分布式事務,seata服務端也是一個微服務,需要和其他微服務一樣需要注冊中心和配置中心,這篇文章主要介紹了Seata?環(huán)境搭建,需要的朋友可以參考下
    2022-10-10
  • SpringBoot構造器注入循環(huán)依賴及解決方案

    SpringBoot構造器注入循環(huán)依賴及解決方案

    這篇文章主要介紹了SpringBoot構造器注入循環(huán)依賴及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 基于spring mvc請求controller訪問方式

    基于spring mvc請求controller訪問方式

    這篇文章主要介紹了spring mvc請求controller訪問方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java利用JSch實現(xiàn)SSH遠程操作的技術指南

    Java利用JSch實現(xiàn)SSH遠程操作的技術指南

    在日常開發(fā)中,許多應用需要通過 SSH 協(xié)議遠程連接服務器來執(zhí)行命令、上傳或下載文件,JSch是一個功能強大的 Java 庫,它提供了便捷的接口來實現(xiàn) SSH 連接和其他遠程管理功能,本文將介紹 JSch 的基本功能,并通過實際代碼示例幫助您快速上手,需要的朋友可以參考下
    2025-03-03
  • springboot項目同時啟動web服務和grpc服務的方法

    springboot項目同時啟動web服務和grpc服務的方法

    本文主要介紹了springboot項目同時啟動web服務和grpc服務的方法,通過實際代碼示例展示了實現(xiàn),對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-02-02
  • Java實現(xiàn)迅雷地址轉成普通地址實例代碼

    Java實現(xiàn)迅雷地址轉成普通地址實例代碼

    本篇文章主要介紹了Java實現(xiàn)迅雷地址轉成普通地址實例代碼,非常具有實用價值,有興趣的可以了解一下。
    2017-03-03
  • java基礎之Integer與int類型輸出示例解析

    java基礎之Integer與int類型輸出示例解析

    這篇文章主要為大家介紹了java基礎之Integer與int類型輸出示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06

最新評論

军事| 北票市| 达孜县| 天峻县| 囊谦县| 交城县| 响水县| 沾化县| 玉山县| 横峰县| 六安市| 福州市| 揭西县| 郑州市| 松阳县| 新巴尔虎右旗| 新泰市| 荆门市| 桐乡市| 青川县| 宜城市| 威海市| 康保县| 瑞丽市| 卢氏县| 涟水县| 屯门区| 桃源县| 建平县| 梁河县| 长岛县| 北安市| 尼勒克县| 运城市| 揭东县| 襄垣县| 荥经县| 平顶山市| 新民市| 当阳市| 秀山|