日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SpingSecurity配置

發(fā)布時間:2024/3/26 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpingSecurity配置 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

SpingSecurity

SpringSecurity是一個框架,它關注于為Java應用程序提供身份驗證和授權。與所有Spring項目一樣,Spring安全性的真正威力在于它可以多么容易地擴展以滿足定制需求

Spring Security是一個功能強大且高度可定制的身份驗證和訪問控制框架。它是保護基于Spring的應用程序的事實標準。

Spring Security 是針對Spring項目的安全框架,也是Spring Boot底層安全模塊默認的技術選型,他可以實現(xiàn)強大的Web安全控制,對于安全控制,我們僅需要引入 spring-boot-starter-security 模塊,進行少量的配置,即可實現(xiàn)強大的安全管理!

記住幾個類:

  • WebSecurityConfigurerAdapter:自定義Security策略

  • AuthenticationManagerBuilder:自定義認證策略

  • @EnableWebSecurity:開啟WebSecurity模式

Spring Security的兩個主要目標是 “認證” 和 “授權”(訪問控制)。

“認證”(Authentication)

身份驗證是關于驗證您的憑據(jù),如用戶名/用戶ID和密碼,以驗證您的身份。

身份驗證通常通過用戶名和密碼完成,有時與身份驗證因素結合使用。

“授權” (Authorization)

授權發(fā)生在系統(tǒng)成功驗證您的身份后,最終會授予您訪問資源(如信息,文件,數(shù)據(jù)庫,資金,位置,幾乎任何內(nèi)容)的完全權限。

認證和授權

常見的http碼

405:請求方式不一致,post,get,濫用415:不支持媒體類型,類型不匹配403:權限不允許500:后臺代碼錯誤404:請求路徑不存在400:請求發(fā)送的參數(shù)是非法的,不正確的

Spring Security是針對Spring項目的安全框架,也是Spring Boot底層安全模塊默認的技術選型,他可以實現(xiàn)強大的Web安全控制,對于安全控制,我們僅需要引入spring-boot-starter-security模塊,進行少量的配置,即可實現(xiàn)強大的安全管理


SecurityConfig.java

package com.chen.config;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.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;@EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {//授權@Overrideprotected void configure(HttpSecurity http) throws Exception {//首頁所有人可以訪問,功能頁只有對應有全向的人才能訪問//鏈式編程 http認證請求 添加一個地址 選擇所有人//請求授權的規(guī)則 //只有vip*下的才可以訪問http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/level1/**").hasRole("vip1").antMatchers("/level2/**").hasRole("vip2").antMatchers("/level3/**").hasRole("vip3");//沒有權限默認會跳到登錄頁面 定制跳轉頁面 自定義參數(shù)http.formLogin().loginPage("/toLogin").usernameParameter("user").passwordParameter("pwd").loginProcessingUrl("/login");//開啟注銷功能 跳轉到首頁 參數(shù)介紹 移除所有的cookie 清空所有的session(會話)// http.logout().deleteCookies("remove").invalidateHttpSession(true);//默認注銷去指定頁面http.logout().logoutSuccessUrl("/");//關閉跨站腳本請求攻擊的功能http.csrf().disable();//開啟記住我功能 自定義接受前端參數(shù) cookie默認保存2周http.rememberMe().rememberMeParameter("remember");}//認證//密碼未加密 反編譯為class文件@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {//內(nèi)存中認證auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("chenchao").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3").and().withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3").and().withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");} }

需要導入的包
pom.xml

<!--springsecurit與thymeleaf整合包--><dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-springsecurity5</artifactId><version>3.0.4.RELEASE</version></dependency><!--web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--springsecurity--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><!--thymeleaf--><dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring5</artifactId></dependency><dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-java8time</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency> 官方文檔:https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#using-boot-starter

總結

以上是生活随笔為你收集整理的SpingSecurity配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。