Spring Security學(xué)習(xí)筆記(一)
介紹
這里學(xué)習(xí)SpringSecurity,對(duì)SpringSecurity進(jìn)行學(xué)習(xí)。
基本用法
添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
添加接口
package com.example.demo.web; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class Test { @RequestMapping("/test") public String test(){ return "test"; } }
啟動(dòng)項(xiàng)目
可以看到日志中,已經(jīng)有了密碼
訪問接口,此時(shí)已經(jīng)有了登錄頁面
輸入用戶名和密碼
用戶名: user
密碼 984cccf2-ba82-468e-a404-7d32123d0f9c
此時(shí)已經(jīng)登錄成功
配置用戶名和密碼
在配置文件中,進(jìn)行配置
spring:
security:
user:
name: ming
password: 123456
roles: admin
輸入用戶名和密碼,可以正常登錄
基于內(nèi)存的認(rèn)證
需要自定義類繼承 WebSecurityConfigurerAdapter
實(shí)現(xiàn)自定義的配置
這里基于內(nèi)存的配置,如下
package com.example.demo.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.password.NoOpPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter { @Bean PasswordEncoder passwordEncoder(){ return NoOpPasswordEncoder.getInstance(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("admin").password("123").roles("admin"); } }
這里基于內(nèi)存的配置
HttpSecurity
這里對(duì)某些方法進(jìn)行攔截
package com.ming.demo.interceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 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; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { //基于內(nèi)存的用戶存儲(chǔ) @Override public void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("itguang").password("123456").roles("USER").and() .withUser("admin").password("{noop}" + "123456").roles("ADMIN"); } //請(qǐng)求攔截 @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().permitAll() .and() .formLogin() .permitAll() .and() .logout() .permitAll(); } }
這里成功完成了post請(qǐng)求進(jìn)行登錄驗(yàn)證。
以上就是Spring Security學(xué)習(xí)筆記(一)的詳細(xì)內(nèi)容,更多關(guān)于Spring Security的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 詳解springSecurity之java配置篇
- Java開發(fā)之spring security實(shí)現(xiàn)基于MongoDB的認(rèn)證功能
- java中Spring Security的實(shí)例詳解
- Java中SpringSecurity密碼錯(cuò)誤5次鎖定用戶的實(shí)現(xiàn)方法
- java中自定義Spring Security權(quán)限控制管理示例(實(shí)戰(zhàn)篇)
- JavaWeb開發(fā)之Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基礎(chǔ)框架
- Spring Security+Spring Data Jpa如何進(jìn)行安全管理
- Spring Security 將用戶數(shù)據(jù)存入數(shù)據(jù)庫
- Spring Security OAuth2 授權(quán)碼模式的實(shí)現(xiàn)
- Spring Security基于數(shù)據(jù)庫實(shí)現(xiàn)認(rèn)證過程解析
- SpringBoot整合Spring Security的詳細(xì)教程
- 手把手帶你入門 Spring Security的具體流程
相關(guān)文章
Java lambda表達(dá)式實(shí)現(xiàn)Flink WordCount過程解析
這篇文章主要介紹了Java lambda表達(dá)式實(shí)現(xiàn)Flink WordCount過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02springBoot如何動(dòng)態(tài)加載資源文件
這篇文章主要介紹了springBoot如何動(dòng)態(tài)加載資源文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Java數(shù)據(jù)結(jié)構(gòu)之線段樹的原理與實(shí)現(xiàn)
線段樹是一種二叉搜索樹,是用來維護(hù)區(qū)間信息的數(shù)據(jù)結(jié)構(gòu)。本文將利用示例詳細(xì)講講Java數(shù)據(jù)結(jié)構(gòu)中線段樹的原理與實(shí)現(xiàn),需要的可以參考一下2022-06-06SpringCloud Gateway自定義filter獲取body中的數(shù)據(jù)為空的問題
這篇文章主要介紹了SpringCloud Gateway自定義filter獲取body中的數(shù)據(jù)為空,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10Spring Boot Redis客戶端遠(yuǎn)程操作實(shí)現(xiàn)過程解析
這篇文章主要介紹了Spring Boot Redis客戶端遠(yuǎn)程操作實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04java調(diào)用webservice接口,并解析返回參數(shù)問題
這篇文章主要介紹了java調(diào)用webservice接口,并解析返回參數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07Java遍歷輸出指定目錄、樹形結(jié)構(gòu)所有文件包括子目錄下的文件
這篇文章主要介紹了Java遍歷輸出指定目錄、樹形結(jié)構(gòu)下的所有文件包括子目錄中的文件,需要的朋友可以參考下2015-07-07Java開發(fā)環(huán)境配置教程(win7 64bit)
這篇文章主要為大家詳細(xì)介紹了win7 64bit下Java開發(fā)環(huán)境的配置教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08