微信小程序多少錢做一個(gè)博客程序seo
文章目錄
- 什么是 SpringSesurity ?
- 細(xì)節(jié)
- 使用方法
什么是 SpringSesurity ?
在我們的開發(fā)中,安全還是有些必要的
用 攔截器 和 過濾器 寫代碼還是比較麻煩。
SpringSecurity
是SpringBoot
的底層安全默認(rèn)選型。一般我們需要認(rèn)證和授權(quán),這也是它的核心功能。我們僅僅需要導(dǎo)入spring-boot-starter-security
模塊,進(jìn)行少量的配置,即可實(shí)現(xiàn)強(qiáng)大的安全管理
認(rèn)證:驗(yàn)證當(dāng)前訪問系統(tǒng)的用戶 是不是本系統(tǒng)的用戶,并且要具體哪一個(gè)用戶
授權(quán):經(jīng)過認(rèn)證后判斷當(dāng)前用戶是否有權(quán)限進(jìn)行某個(gè)操作
細(xì)節(jié)
登錄過程
負(fù)責(zé)我們?cè)诘卿涰撁嫣顚懙挠脩艉兔艽a登錄的請(qǐng)求,入門案例主要由他負(fù)責(zé)。
使用方法
總結(jié)到代碼之中。
// 開啟 web 安全
@EnableWebSecurity
public class springSecurityConfig extends WebSecurityConfigurerAdapter { // 然后我們繼承 web安全配置 適配器// 鏈?zhǔn)骄幊?#64;Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests(). // authorize 授權(quán)antMatchers("/").permitAll() // ant ==> 螞蟻 matcher ==> 匹配器 permitAll ==> 允許所有.antMatchers("/test1/**").hasRole("vip1") // role角色.antMatchers("/test2/**").hasRole("vip2").antMatchers("/test3/**").hasRole("vip3");// 如果沒有用戶的情況下,則會(huì)跳轉(zhuǎn)到 login 頁面 // 登錄加工的urlhttp.formLogin().loginPage("/toLogin").usernameParameter("name").passwordParameter("pwd").loginProcessingUrl("/Login");// http.formLogin()
// .loginPage("/toLogin") // 設(shè)置登錄頁面URL
// .loginProcessingUrl("/Login") // 設(shè)置表單提交的URL 個(gè)人感覺:應(yīng)該是需要認(rèn)證的模塊
// .failureUrl("/login-error") // 登錄失敗后的重定向URL
// .defaultSuccessUrl("/home", true) // 登錄成功后的默認(rèn)重定向URL
// .and()
// .logout()
// .logoutSuccessUrl("/"); // 注銷成功后的重定向URL// 開啟記住我功能http.rememberMe().rememberMeParameter("jiZhuWo");http.csrf().disable(); // 關(guān)閉腳本跨站攻擊// 開啟注銷功能http.logout().logoutSuccessUrl("/index"); // 注銷成功后,回到 “/index” 之中。}// 認(rèn)證@Override // 最新版本需要密碼加密protected void configure(AuthenticationManagerBuilder auth) throws Exception { // Authentication 認(rèn)證 Manager 管理者auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) // Memory 記憶.withUser("xj").password(new BCryptPasswordEncoder().encode("abc")).roles("vip1","vip2") // memory ==> 記憶.and().withUser("test").password(new BCryptPasswordEncoder().encode("cba")).roles("vip3");}
}
我們可以看到,擁有這個(gè)功能,不僅提供了權(quán)限、安全等,還另外 省去了寫登錄后臺(tái)、過濾器等大量繁雜的代碼,十分的方便