spring security 认证与权限控制
目錄
- 1. 使用授權(quán)和認(rèn)證的必要性
- 2. spring security 與 shiro 與 過濾器,攔截器
- 3. 具體配置使用
項(xiàng)目地址:
https://github.com/sevenyoungairye/spring-boot-study/tree/main/springboot-security-06
1. 使用授權(quán)和認(rèn)證的必要性
安全框架,是為了保護(hù)web應(yīng)用,保護(hù)用戶的隱私,哪些東西能給別人看,而哪些不能給別人看。給別人看,需要達(dá)到哪些條件,比如需要登錄,會員等級(充錢)。
spring security 是針對spring項(xiàng)目的安全框架,也是spring boot底層安全模塊默認(rèn)的技術(shù)選型,他可以實(shí)現(xiàn)強(qiáng)大的web安全控制,對于安全控制,只需要引入spring-starter-security模塊,進(jìn)行少量的配置,即可實(shí)現(xiàn)強(qiáng)大的安全管理。
幾個(gè)類:
-
WebSecurityConfigurerAdapter: 自定義Security策略
-
AuthenticationManagerBuilder:自定義認(rèn)證策略
-
@EnableWebSecurity: 開啟WebSecurity模式,@EnableXXXX開啟某個(gè)功能
spring security 的兩個(gè)主要目標(biāo)是“認(rèn)證”Authentication和“授權(quán)“Authorization(訪問控制)
這兩個(gè)概念相同,不只是在spring security中存在。
- 功能權(quán)限(理解為管理員和普通成員對應(yīng)的功能)
- 訪問權(quán)限(理解為需要授權(quán),登錄)
- 菜單權(quán)限 (理解為分角色)
2. spring security 與 shiro 與 過濾器,攔截器
只在使用方面比較:
spring security 簡單易用,spring家族天然集成。
shiro屬于阿帕奇,使用較為復(fù)雜,配置類較多,但功能強(qiáng)大。
過濾器,攔截器… 需要大量配置,大量判斷,代碼顯得冗余。
3. 具體配置使用
一個(gè)配置類搞定
package cn.bitqian.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;/*** @description spring security test* @author echo lovely* @date 2020/10/25 19:46*/@EnableWebSecurity // 啟用安全框架 public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {// 授權(quán)@Overrideprotected void configure(HttpSecurity http) throws Exception {// 鏈?zhǔn)骄幊?/span>http.authorizeRequests().antMatchers("/").permitAll(). // 首頁允許所有人訪問antMatchers("/level1/**").hasRole("vip1"). // vip1才能訪問level1..antMatchers("/level2/**").hasRole("vip2").antMatchers("/level3/**").hasRole("vip3");// 沒有權(quán)限默認(rèn)會到登錄頁面,需要開啟的登錄頁面// 賬號 密碼 與 表單的name要一致http.formLogin().loginPage("/login").loginProcessingUrl("/login"). // 指定一個(gè)特殊的頁面登錄!usernameParameter("username").passwordParameter("password");// 支持post請求http.csrf().disable();// 注銷 退出登錄到login 頁面http.logout().logoutSuccessUrl("/login");// 記住我 cookiehttp.rememberMe().rememberMeParameter("remember"); //記住我表單}// 認(rèn)證方式@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {// 內(nèi)存數(shù)據(jù)auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("jack").password(new BCryptPasswordEncoder().encode("123")).roles("vip1", "vip2").and().withUser("root").password(new BCryptPasswordEncoder().encode("123")).roles("vip1", "vip2", "vip3").and().withUser("guest").password(new BCryptPasswordEncoder().encode("123")).roles("vip1");}}更多請?jiān)L問git。
總結(jié)
以上是生活随笔為你收集整理的spring security 认证与权限控制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: docker 覆盖 entrypoint
- 下一篇: openfeign 负载均衡调用服务