日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術(shù)文章
文章詳情頁

Spring Security的簡單使用

瀏覽:12日期:2023-07-16 09:25:04
什么是Spring Security Spring Security是一個(gè)功能強(qiáng)大且高度可定制的身份驗(yàn)證和訪問控制框架。它實(shí)際上是保護(hù)基于spring的應(yīng)用程序的標(biāo)準(zhǔn)。 Spring Security是一個(gè)框架,側(cè)重于為Java應(yīng)用程序提供身份驗(yàn)證和授權(quán)。與所有Spring項(xiàng)目一樣,Spring安全性的真正強(qiáng)大之處在于它可以輕松地?cái)U(kuò)展以滿足定制需求 在用戶認(rèn)證方面,Spring Security 框架支持主流的認(rèn)證方式,包括 HTTP 基本認(rèn)證、HTTP 表單驗(yàn)證、HTTP 摘要認(rèn)證、OpenID 和 LDAP 等。在用戶授權(quán)方面,Spring Security 提供了基于角色的訪問控制和訪問控制列表(Access Control List,ACL),可以對(duì)應(yīng)用中的領(lǐng)域?qū)ο筮M(jìn)行細(xì)粒度的控制。 Spring Security測(cè)試

前期準(zhǔn)備

新建一個(gè)springboot項(xiàng)目,導(dǎo)入web模板和thymeleaf模板 導(dǎo)入靜態(tài)資源

Spring Security的簡單使用

關(guān)閉thymeleaf緩存spring.thymeleaf.cache=false 先創(chuàng)建一個(gè)TestController來測(cè)試一下項(xiàng)目是否搭建成功

package com.example.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@Controllerpublic class TestController { @RequestMapping('/') public String index(){return 'index'; } @RequestMapping('/toLogin') public String toLogin(){return 'views/login'; } @RequestMapping('/level1/{id}') public String level1(@PathVariable('id') int id){return 'views/level1/'+id; } @RequestMapping('/level2/{id}') public String level2(@PathVariable('id') int id){return 'views/level2/'+id; } @RequestMapping('/level3/{id}') public String level3(@PathVariable('id') int id){return 'views/level3/'+id; }}SpringSecurity的使用

引入spring-boot-starter-security模塊

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId></dependency>

認(rèn)識(shí)SpringSecurity的幾個(gè)重要的類

WebSecurityConfigurerAdapter:自定義Security策略 AuthenticationManagerBuilder:自定義認(rèn)證策略 @EnableWebSecurity:開啟WebSecurity模式

SpringSecurity---授權(quán)(認(rèn)真看代碼和注釋)

//授權(quán) @Override protected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers('/').permitAll() //首頁所有人可以訪問.antMatchers('/level1/**').hasRole('vip1') //level1下的頁面,VIP1可以訪問.antMatchers('/level2/**').hasRole('vip2').antMatchers('/level3/**').hasRole('vip3');//開啟自動(dòng)配置的登錄功能,//即,沒有登錄的時(shí)候,除了首頁,其他頁面都訪問不了,這時(shí)候,你訪問其他頁面的時(shí)候,//它直接跳轉(zhuǎn)到它內(nèi)置的登陸頁面,讓你登錄 http.formLogin();http.formLogin().loginPage('/toLogin');//自定義登錄頁,將自定義的登錄頁替換掉內(nèi)置的登錄頁//用來處理用戶登錄提交的表單http.formLogin().usernameParameter('username').passwordParameter('password').loginPage('/toLogin').loginProcessingUrl('/login');//開啟自動(dòng)配置的注銷的功能// /logout 注銷請(qǐng)求 http.logout();http.csrf().disable();//關(guān)閉csrf功能:跨站請(qǐng)求偽造,默認(rèn)只能通過post方式提交logout請(qǐng)求http.logout().logoutSuccessUrl('/');//注銷成功就返回首頁//開啟記住我功能 http.rememberMe();http.rememberMe().rememberMeParameter('remember');//在自定義登錄頁添加 記住我 }

SpringSecurity---認(rèn)證(認(rèn)真看代碼和注釋)

//認(rèn)證 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception {/* auth.inMemoryAuthentication().withUser('xiaomi').password('123').roles('vip1') 這樣寫是不行的,會(huì)出現(xiàn)500錯(cuò)誤。原因:密碼沒有加密 Spring security 5.0中新增了多種加密方式,也改變了密碼的格式。 要想我們的項(xiàng)目還能夠正常登陸,需要修改一下configure中的代碼。我們要將前端傳過來的密碼進(jìn)行某種方式加密 spring security 官方推薦的是使用bcrypt加密方式。 這里通過 passwordEncoder(new BCryptPasswordEncoder()) 的方式進(jìn)行加密 */// 在內(nèi)存中定義認(rèn)證的規(guī)則auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser('xiaolong').password(new BCryptPasswordEncoder().encode('123456')).roles('vip1').and().withUser('xiaomi').password(new BCryptPasswordEncoder().encode('123456')).roles('vip1','vip2').and().withUser('xiaohu').password(new BCryptPasswordEncoder().encode('123456')).roles('vip1','vip2','vip3');//在jdbc中定義認(rèn)證的規(guī)則//auth.jdbcAuthentication() }

啟動(dòng)測(cè)試

靜態(tài)資源

login.html

<!DOCTYPE html><html lang='en' xmlns:th='http://www.thymeleaf.org'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1'> <title>登錄</title> <!--semantic-ui--> <link rel='external nofollow' rel='external nofollow' rel='stylesheet'></head><body><!--主容器--><div class='ui container'> <div class='ui segment'><div style='text-align: center'> <h1 class='header'>登錄</h1></div><div class='ui placeholder segment'> <div class='ui column very relaxed stackable grid'><div class='column'> <div class='ui form'><form th:action='@{/login}' method='post'> <div class='field'><label>Username</label><div class='ui left icon input'> <input type='text' placeholder='Username' name='username'> <i class='user icon'></i></div> </div> <div class='field'><label>Password</label><div class='ui left icon input'> <input type='password' name='password'> <i class='lock icon'></i></div> </div> <div class='field'><input type='checkbox' name='remember'>記住我 </div> <input type='submit' /></form> </div></div> </div></div><div style='text-align: center'> <div class='ui label'></i>注冊(cè) </div> <br><br></div><div style='text-align: center'> <h3>Spring Security</h3></div> </div></div><script th:src='http://www.b3g6.com/bcjs/@{/js/jquery-3.1.1.min.js}'></script><script th:src='http://www.b3g6.com/bcjs/@{/js/semantic.min.js}'></script></body></html>

index.html

<!DOCTYPE html><html lang='en' xmlns:th='http://www.thymeleaf.org' xmlns:sec='http://www.thymeleaf.org/extras/spring-security'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1'> <title>首頁</title> <!--semantic-ui--> <link rel='external nofollow' rel='external nofollow' rel='stylesheet'> <link th:href='http://www.b3g6.com/bcjs/@{/css/mystyle.css}' rel='external nofollow' rel='stylesheet'></head><body><!--主容器--><div class='ui container'> <div th:fragment='nav-menu'><div class='ui secondary menu'> <a th:href='http://www.b3g6.com/bcjs/@{/index}' rel='external nofollow' >首頁</a> <!--登錄注銷--> <div class='right menu'><!--未登錄--><!--sec:authorize='!isAuthenticated() 未認(rèn)證即未登錄--><div sec:authorize='!isAuthenticated()'> <a th:href='http://www.b3g6.com/bcjs/@{/toLogin}' rel='external nofollow' ><i class='address card icon'></i> 登錄 </a></div><!--已登錄--><!--sec:authorize='!isAuthenticated() 已認(rèn)證即已經(jīng)有用戶登錄--><div sec:authorize='isAuthenticated()'> <a class='item'><i class='address card icon'></i>用戶名:<span sec:authentication='principal.username'></span>角色:<span sec:authentication='principal.authorities'></span> </a></div><!--注銷--><div sec:authorize='isAuthenticated()'> <a th:href='http://www.b3g6.com/bcjs/@{/logout}' rel='external nofollow' ><i class='sign-out icon'></i> 注銷 </a></div> </div></div> </div> <div style='text-align: center'><h3>Spring Security</h3> </div> <div><br><div class='ui three column stackable grid'> <!-- sec:authorize='hasRole(’vip1’) 永擁有vip1權(quán)限的人才能看到 --> <div sec:authorize='hasRole(’vip1’)'><div class='ui raised segment'> <div class='ui'><div class='content'> <h5 class='content'>Level 1</h5> <hr> <div><a th:href='http://www.b3g6.com/bcjs/@{/level1/1}' rel='external nofollow' ><i class='bullhorn icon'></i> Level-1-1</a></div> <div><a th:href='http://www.b3g6.com/bcjs/@{/level1/2}' rel='external nofollow' ><i class='bullhorn icon'></i> Level-1-2</a></div> <div><a th:href='http://www.b3g6.com/bcjs/@{/level1/3}' rel='external nofollow' ><i class='bullhorn icon'></i> Level-1-3</a></div></div> </div></div> </div> <!-- sec:authorize='hasRole(’vip2’) 永擁有vip2權(quán)限的人才能看到 --> <div sec:authorize='hasRole(’vip2’)'><div class='ui raised segment'> <div class='ui'><div class='content'> <h5 class='content'>Level 2</h5> <hr> <div><a th:href='http://www.b3g6.com/bcjs/@{/level2/1}' rel='external nofollow' ><i class='bullhorn icon'></i> Level-2-1</a></div> <div><a th:href='http://www.b3g6.com/bcjs/@{/level2/2}' rel='external nofollow' ><i class='bullhorn icon'></i> Level-2-2</a></div> <div><a th:href='http://www.b3g6.com/bcjs/@{/level2/3}' rel='external nofollow' ><i class='bullhorn icon'></i> Level-2-3</a></div></div> </div></div> </div> <!-- sec:authorize='hasRole(’vip3’) 永擁有vip3權(quán)限的人才能看到 --> <div sec:authorize='hasRole(’vip3’)'><div class='ui raised segment'> <div class='ui'><div class='content'> <h5 class='content'>Level 3</h5> <hr> <div><a th:href='http://www.b3g6.com/bcjs/@{/level3/1}' rel='external nofollow' ><i class='bullhorn icon'></i> Level-3-1</a></div> <div><a th:href='http://www.b3g6.com/bcjs/@{/level3/2}' rel='external nofollow' ><i class='bullhorn icon'></i> Level-3-2</a></div> <div><a th:href='http://www.b3g6.com/bcjs/@{/level3/3}' rel='external nofollow' ><i class='bullhorn icon'></i> Level-3-3</a></div></div> </div></div> </div></div> </div> </div><script th:src='http://www.b3g6.com/bcjs/@{/js/jquery-3.1.1.min.js}'></script><script th:src='http://www.b3g6.com/bcjs/@{/js/semantic.min.js}'></script></body></html>一些其他的小東西 如果你在測(cè)試TestController之前已經(jīng)提前把spring-boot-starter-security這個(gè)依賴導(dǎo)入,那么你請(qǐng)求首頁的時(shí)候,程序會(huì)自動(dòng)跳轉(zhuǎn)到登錄頁面,任何請(qǐng)求都會(huì)被攔截,停留在登錄頁面 如果用戶還沒有登錄,你只能看到首頁,你點(diǎn)擊首頁的任何界面的請(qǐng)求都會(huì)跳轉(zhuǎn)到默認(rèn)的登錄頁面。然后,你通過你認(rèn)證過的用戶進(jìn)行登錄,登錄成功后會(huì)返回你之前點(diǎn)擊的那個(gè)界面的請(qǐng)求。也就是說,本來界面有一個(gè)/level1/1.html你點(diǎn)擊它,沒登錄,會(huì)直接跳轉(zhuǎn)到默認(rèn)的登錄界面,登陸成功后,會(huì)返回/level1/1.html,而不是返回首頁,這是默認(rèn)的 分析一下自定義登錄頁的實(shí)現(xiàn)

Spring Security的簡單使用

以上就是Spring Security的簡單使用的詳細(xì)內(nèi)容,更多關(guān)于Spring Security的使用的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
久久蜜桃av| 激情五月色综合国产精品| 日韩av午夜在线观看| 精品精品99| 免费久久精品| 久久男人av| 蜜桃av一区二区| 麻豆久久一区| 麻豆91精品| 国产中文欧美日韩在线| 一区二区三区四区日韩| 国产精品黑丝在线播放| 亚洲日本三级| 欧美色图国产精品| 精品亚洲a∨| 日韩av一级片| 一区二区三区网站| 黄色亚洲在线| se01亚洲视频 | 精品国产精品久久一区免费式| 欧美手机在线| 国产一区二区三区四区大秀| 日本一区福利在线| 中文久久精品| 亚洲一本视频| 国产 日韩 欧美一区| 国产精品tv| 欧美片第1页综合| 亚洲日本免费电影| 蜜桃一区二区三区在线观看| 欧美久久精品一级c片| 在线一区视频观看| av综合电影网站| 日韩大片在线观看| 国内精品亚洲| 荡女精品导航| 亚洲欧洲高清| 久久国产亚洲| 激情视频一区二区三区| 欧美日韩一二三四| 欧美在线影院| 日韩av在线免费观看不卡| 久久一区精品| 麻豆成人在线观看| 国产麻豆久久| 日本一二区不卡| 国产网站在线| 精品捆绑调教一区二区三区| 久久久久免费av| 黄页网站一区| 黄色日韩精品| 国产日韩一区二区三区在线 | 亚洲综合婷婷| 久久国产乱子精品免费女| 国产精品v日韩精品v欧美精品网站 | 你懂的国产精品| 桃色一区二区| 嫩草伊人久久精品少妇av杨幂| 久久gogo国模啪啪裸体| 亚洲综合电影| 亚洲精品少妇| 国产精久久一区二区| 精品亚洲美女网站| 日本免费新一区视频| 精品久久中文| 三级在线观看一区二区| 欧美国产日韩电影| 国产精品日本| 日本久久黄色| 日韩欧美久久| 久久国产精品成人免费观看的软件| 一区二区三区四区在线观看国产日韩| 国产精品22p| 麻豆9191精品国产| 日韩国产欧美| 欧美日韩a区| 色88888久久久久久影院| 免费视频亚洲| 成人黄色av| 91麻豆精品激情在线观看最新| 成人亚洲欧美| 免费日韩一区二区三区| 亚洲一区二区三区高清| 日韩av二区| 国产亚洲第一伦理第一区| 国产精品毛片| 欧美日韩中文一区二区| 国产伦精品一区二区三区在线播放 | 韩国一区二区三区视频| 爽好久久久欧美精品| 日韩av福利| 美女视频黄 久久| 日韩高清在线一区| 亚洲狼人精品一区二区三区| 国产精品av一区二区| 日韩成人三级| 日本а中文在线天堂| 久久精品国产免费| 国产精品一页| 国产色噜噜噜91在线精品| 亚洲v天堂v手机在线| 蜜桃久久av| 综合一区av| 视频一区在线视频| 视频在线观看91| 视频在线观看一区| 日韩中文字幕一区二区三区| 99re国产精品| 国产精品试看| 综合激情视频| 丝袜亚洲另类欧美| 综合欧美亚洲| 国产精品欧美大片| 国产精品网站在线看| 国产欧美大片| 国产a久久精品一区二区三区| 精品一区二区三区亚洲| 国产一区福利| 亚洲高清成人| 久久久国产精品一区二区中文| 国产一区亚洲| 亚洲字幕久久| 国产欧美一区二区三区精品观看| 国产欧美高清视频在线| 成人在线视频区| 中文久久精品| 欧美国产日本| 国产欧美丝祙| 麻豆极品一区二区三区| 国产极品模特精品一二| 国产精品17p| 精品一区不卡| 日韩在线卡一卡二| 国产欧美日韩免费观看| 日韩国产综合| 亚洲精品系列| 免费精品视频在线| 亚洲综合五月| 日本成人中文字幕| 国产精品毛片aⅴ一区二区三区| 国产乱人伦丫前精品视频| 国产欧美一区| 久久精品国产网站| 丝袜诱惑一区二区| 欧美成人综合| 婷婷亚洲成人| 国际精品欧美精品| 日韩午夜一区| 婷婷精品在线观看| 国产麻豆精品| 精品成人免费一区二区在线播放| 久久精品亚洲欧美日韩精品中文字幕| 不卡专区在线| 99视频精品免费观看| 日韩福利在线观看| 欧美精品高清| 亚洲欧美日韩精品一区二区| 五月亚洲婷婷 | 99成人超碰| 久久女人天堂| 国产精品香蕉| 91欧美日韩在线| 免费高清在线一区| 在线视频精品| 欧美aa在线视频| 欧美激情视频一区二区三区在线播放| 免费日韩视频| 久久xxxx精品视频| 亚洲免费中文| 日韩中文欧美| 免费国产亚洲视频| 激情国产在线| 亚洲激情二区| 亚洲精品影视| 中文字幕日韩高清在线| 欧美日韩国产在线观看网站 | 亚洲专区视频| 亚洲免费一区三区| 亚洲精品日本| 视频精品一区二区| 免费观看日韩电影| 日韩欧乱色一区二区三区在线| 蜜臀91精品一区二区三区| 蜜桃91丨九色丨蝌蚪91桃色| 视频一区二区国产| 日本不卡高清视频| 欧美偷窥清纯综合图区| 国产精品宾馆| 国语精品一区| 欧美.日韩.国产.一区.二区| 亚洲免费婷婷| 日韩精品1区2区3区| 国产精品777777在线播放| 国产在线不卡一区二区三区| 日本欧美国产| 午夜精品亚洲| 国产探花在线精品一区二区| 精品欠久久久中文字幕加勒比| 日韩欧美自拍| 久久xxxx精品视频|