springSecurity用戶認(rèn)證和授權(quán)的實(shí)現(xiàn)
一,框架介紹
Spring 是一個(gè)非常流行和成功的 Java 應(yīng)用開發(fā)框架。Spring Security 基于 Spring 框架,提供了一套 Web 應(yīng)用安全性的完整解決方案。一般來說,Web 應(yīng)用的安全性包括用戶認(rèn)證(Authentication)和用戶授權(quán)(Authorization)兩個(gè)部分。
(1)用戶認(rèn)證指的是:驗(yàn)證某個(gè)用戶是否為系統(tǒng)中的合法主體,也就是說用戶能否訪問該系統(tǒng)。用戶認(rèn)證一般要求用戶提供用戶名和密碼。系統(tǒng)通過校驗(yàn)用戶名和密碼來完成認(rèn)證過程。
(2)用戶授權(quán)指的是驗(yàn)證某個(gè)用戶是否有權(quán)限執(zhí)行某個(gè)操作。在一個(gè)系統(tǒng)中,不同用戶所具有的權(quán)限是不同的。比如對(duì)一個(gè)文件來說,有的用戶只能進(jìn)行讀取,而有的用戶可以進(jìn)行修改。一般來說,系統(tǒng)會(huì)為不同的用戶分配不同的角色,而每個(gè)角色則對(duì)應(yīng)一系列的權(quán)限。
Spring Security其實(shí)就是用filter,多請(qǐng)求的路徑進(jìn)行過濾。
(1)如果是基于Session,那么Spring-security會(huì)對(duì)cookie里的sessionid進(jìn)行解析,找到服務(wù)器存儲(chǔ)的sesion信息,然后判斷當(dāng)前用戶是否符合請(qǐng)求的要求。
(2)如果是token,則是解析出token,然后將當(dāng)前請(qǐng)求加入到Spring-security管理的權(quán)限信息中去
認(rèn)證與授權(quán)實(shí)現(xiàn)思路
二 ,如何實(shí)現(xiàn)
1 ,導(dǎo)入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2,代碼
package com.demo.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.WebSecurityConfiguration; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UsernameNotFoundException; @EnableWebSecurity public class Security extends WebSecurityConfiguration { protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/public/**").permitAll() // 允許訪問公開路徑 .antMatchers("/public/index").hasRole("vip") // 需要指定用戶在可以使用 .antMatchers("/admin/**").hasRole("admin") // 需要指定用戶在可以使用 .anyRequest().authenticated() // 其他請(qǐng)求需要認(rèn)證 .and() .formLogin() // 使用表單登錄 .loginPage("/login") // 指定登錄頁面 .permitAll() // 允許所有用戶訪問登錄頁面 .and() .logout() // 配置注銷 .logoutUrl("/logout") // 注銷路徑 .logoutSuccessUrl("/login?logout") // 注銷成功后跳轉(zhuǎn)的頁面 .permitAll(); } //緩存中 protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() //添加 名字為 admin 密碼 為 admin123 的用戶 基于權(quán)限 ADMIN .withUser("admin").password("admin123").roles("ADMIN") .and() .withUser("user").password("user123").roles("USER"); } //從數(shù)據(jù)庫中 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { // 從數(shù)據(jù)庫或其他數(shù)據(jù)源加載用戶信息 // 這里簡(jiǎn)單起見,直接使用內(nèi)存中的用戶信息 if ("admin".equals(username)) { return org.springframework.security.core.userdetails.User.builder() .username("admin") .password("{noop}admin") // {noop}表示不加密,實(shí)際項(xiàng)目中應(yīng)該使用加密的密碼 .roles("ADMIN") .build(); } else if ("user".equals(username)) { return org.springframework.security.core.userdetails.User.builder() .username("user") .password("{noop}user") .roles("USER") .build(); } else { throw new UsernameNotFoundException("User not found"); } } }
import org.springframework.security.access.annotation.Secured; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class MyController { @GetMapping("/user") @Secured("ROLE_USER") // 要求用戶具有ROLE_USER角色才能訪問 public String userPage() { return "user"; } }
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class MyController { @GetMapping("/admin") @PreAuthorize("hasRole('ADMIN')") // 要求用戶具有ROLE_ADMIN角色才能訪問 public String adminPage() { return "admin"; } @GetMapping("/user") @PreAuthorize("hasRole('USER')") // 要求用戶具有ROLE_USER角色才能訪問 public String userPage() { return "user"; } }
到此這篇關(guān)于springSecurity用戶認(rèn)證和授權(quán)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springSecurity 認(rèn)證和授權(quán)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringSecurity實(shí)現(xiàn)權(quán)限認(rèn)證與授權(quán)的使用示例
- SpringSecurity進(jìn)行認(rèn)證與授權(quán)的示例代碼
- SpringBoot整合SpringSecurity認(rèn)證與授權(quán)
- 深入淺析springsecurity入門登錄授權(quán)
- SpringSecurityOAuth2實(shí)現(xiàn)微信授權(quán)登錄
- SpringBoot+SpringSecurity實(shí)現(xiàn)基于真實(shí)數(shù)據(jù)的授權(quán)認(rèn)證
- springsecurity第三方授權(quán)認(rèn)證的項(xiàng)目實(shí)踐
- SpringSecurity數(shù)據(jù)庫進(jìn)行認(rèn)證和授權(quán)的使用
- SpringSecurity授權(quán)機(jī)制的實(shí)現(xiàn)(AccessDecisionManager與投票決策)
相關(guān)文章
Spring?Boot?4.0對(duì)于Java開發(fā)的影響和前景
探索Spring?Boot?4.0如何徹底革新Java開發(fā),提升效率并開拓未來可能性!別錯(cuò)過這篇緊湊的指南,它帶你領(lǐng)略Spring?Boot的強(qiáng)大魅力和潛力,準(zhǔn)備好了嗎?2024-02-02Java實(shí)現(xiàn)字符串倒序輸出的四種方法匯總
這篇文章主要介紹了Java實(shí)現(xiàn)字符串倒序輸出的四種方法匯總,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06詳解MyBatis resultType與resultMap中的幾種返回類型
本文主要介紹了MyBatis resultType與resultMap中的幾種返回類型,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Spring boot調(diào)用Oracle存儲(chǔ)過程的兩種方式及完整代碼
這篇文章主要給大家介紹了關(guān)于Spring boot調(diào)用Oracle存儲(chǔ)過程的兩種方式及完整代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08javaWeb實(shí)現(xiàn)簡(jiǎn)單文件上傳
這篇文章主要為大家詳細(xì)介紹了JAVAWeb實(shí)現(xiàn)簡(jiǎn)單文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06詳解Spring Boot 使用Spring security 集成CAS
本篇文章主要介紹了詳解Spring Boot 使用Spring security 集成CAS,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05