springboot+security整合(1)
說明 springboot 版本 2.0.3
源碼地址:點(diǎn)擊跳轉(zhuǎn)
系列
- springboot+security 整合(1)
- springboot+security 整合(2)
- springboot+security 整合(3)
一、 介紹
??Spring Security 是一個(gè)能夠?yàn)榛?Spring 的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組可以在 Spring 應(yīng)用上下文中配置的 Bean,充分利用了 Spring IoC,DI(控制反轉(zhuǎn) Inversion of Control ,DI:Dependency Injection 依賴注入)和 AOP(面向切面編程)功能,為應(yīng)用系統(tǒng)提供聲明式的安全訪問控制功能,減少了為企業(yè)系統(tǒng)安全控制編寫大量重復(fù)代碼的工作。
二、 環(huán)境搭建
??建立 springboot2 項(xiàng)目,加入 security 依賴,mybatis 依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version> </dependency> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope> </dependency>數(shù)據(jù)庫為傳統(tǒng)的用戶--角色--權(quán)限,權(quán)限表記錄了 url 和 method,springboot 配置文件如下:
mybatis:type-aliases-package: com.example.demo.entity server:port: 8081 spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=trueusername: rootpassword: 123456http:encoding:charset: utf-8enabled: truespringboot 啟動(dòng)類中加入如下代碼,設(shè)置路由匹配規(guī)則。
@Override protected void configurePathMatch(PathMatchConfigurer configurer) {configurer.setUseSuffixPatternMatch(false) //設(shè)置路由是否后綴匹配,譬如/user能夠匹配/user.,/user.aa.setUseTrailingSlashMatch(false); //設(shè)置是否后綴路徑匹配,比如/user能夠匹配/user,/user/ }三、 security 配置
??默認(rèn)情況下 security 是無需任何自定義配置就可使用的,我們不考慮這種方式,直接講如何個(gè)性化登錄過程。
1、 建立 security 配置文件,目前配置文件中還沒有任何配置。
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { }2、 個(gè)性化登錄,security 中的登錄如下:
- security 需要一個(gè) user 的實(shí)體類實(shí)現(xiàn)UserDetails接口,該實(shí)體類最后與系統(tǒng)中用戶的實(shí)體類分開,代碼如下:
- 編寫了實(shí)體類還需要編寫一個(gè)服務(wù)類 SecurityService 實(shí)現(xiàn)UserDetailsService接口,重寫 loadByUsername 方法,通過這個(gè)方法根據(jù)用戶名獲取用戶信息,代碼如下:
- 通常我們會(huì)對密碼進(jìn)行加密,所有還要編寫一個(gè) passwordencode 類,實(shí)現(xiàn) PasswordEncoder 接口,代碼如下:
3、 編輯配置文件
- 編寫 config Bean 以使用上面定義的驗(yàn)證邏輯,securityUserService、myPasswordEncoder 通過@Autowired 引入。
- 然后編寫 configure Bean(和上一個(gè)不一樣,參數(shù)不同),實(shí)現(xiàn) security 驗(yàn)證邏輯,代碼如下:
到這里便可實(shí)現(xiàn) security 與 springboot 的基本整合。
四、實(shí)現(xiàn)記住我功能
1、 建表
??記住我功能需要數(shù)據(jù)庫配合實(shí)現(xiàn),首先要在數(shù)據(jù)庫建一張表用戶保存 cookie 和用戶名,數(shù)據(jù)庫建表語句如下:不能做修改
CREATE TABLE `persistent_logins` (`username` varchar(64) NOT NULL,`series` varchar(64) NOT NULL,`token` varchar(64) NOT NULL,`last_used` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`series`) )2、 編寫 rememberMeservice Bean
??代碼如下:
@Beanpublic RememberMeServices rememberMeServices(){JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();jdbcTokenRepository.setDataSource(dataSource);PersistentTokenBasedRememberMeServices rememberMeServices =new PersistentTokenBasedRememberMeServices("INTERNAL_SECRET_KEY",securityUserService,jdbcTokenRepository);//還可設(shè)置許多其他屬性rememberMeServices.setCookieName("kkkkk"); //客戶端cookie名return rememberMeServices;}dataSource 為@Autowired 引入
3、 配置文件設(shè)置 remember
??在 config(HttpSecurity http)中加入記住我功能
.rememberMe().rememberMeServices(rememberMeServices()).key("INTERNAL_SECRET_KEY")在登錄表單中設(shè)置 remember-me 即可實(shí)現(xiàn)記住我功能。
本文原創(chuàng)發(fā)布于:https://www.tapme.top/blog/detail/2018-08-20-10-38
轉(zhuǎn)載于:https://www.cnblogs.com/wuyoucao/p/10863419.html
總結(jié)
以上是生活随笔為你收集整理的springboot+security整合(1)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android(3_2)-----模仿微
- 下一篇: git删除远程服务的文件夹