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

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

Spring Security 多過濾鏈的使用詳解

瀏覽:22日期:2023-07-03 09:40:59
目錄一、背景二、需求1、給客戶端使用的api2、給網(wǎng)站使用的api三、實(shí)現(xiàn)方案方案一:方案二四、實(shí)現(xiàn)1、app 端 Spring Security 的配置五、實(shí)現(xiàn)效果1、app 有權(quán)限訪問 api2、app 無權(quán)限訪問 api3、admin 用戶有權(quán)限訪問 網(wǎng)站 api4、dev 用戶無權(quán)限訪問 網(wǎng)站 api六、完整代碼一、背景

在我們實(shí)際的開發(fā)過程中,有些時候可能存在這么一些情況,某些api 比如: /api/** 這些是給App端使用的,數(shù)據(jù)的返回都是以JSON的格式返回,且這些API的認(rèn)證方式都是使用的TOKEN進(jìn)行認(rèn)證。而除了 /api/** 這些API之外,都是給網(wǎng)頁端使用的,需要使用表單認(rèn)證,給前端返回的都是某個頁面。

二、需求1、給客戶端使用的api 攔截 /api/**所有的請求。 /api/**的所有請求都需要ROLE_ADMIN的角色。 從請求頭中獲取 token,只要獲取到token的值,就認(rèn)為認(rèn)證成功,并賦予ROLE_ADMIN到角色。 如果沒有權(quán)限,則給前端返回JSON對象 {message:'您無權(quán)限訪問'} 訪問 /api/userInfo端點(diǎn) 請求頭攜帶 token 可以訪問。請求頭不攜帶token不可以訪問。2、給網(wǎng)站使用的api 攔截 所有的請求,但是不處理/api/**開頭的請求。 所有的請求需要ROLE_ADMIN的權(quán)限。 沒有權(quán)限,需要使用表單登錄。 登錄成功后,訪問了無權(quán)限的請求,直接跳轉(zhuǎn)到百度去。 構(gòu)建2個內(nèi)建的用戶 用戶一: admin/admin 擁有 ROLE_ADMIN 角色用戶二:dev/dev 擁有 ROLE_DEV 角色 訪問 /index 端點(diǎn) admin 用戶訪問,可以訪問。dev 用戶訪問,不可以訪問,權(quán)限不夠。三、實(shí)現(xiàn)方案方案一:

直接拆成多個服務(wù),其中 /api/** 的成為一個服務(wù)。非/api/**的拆成另外一個服務(wù)。各個服務(wù)使用自己的配置,互不影響。

方案二

在同一個服務(wù)中編寫。不同的請求使用不同的SecurityFilterChain來實(shí)現(xiàn)。

經(jīng)過考慮,此處采用方案二來實(shí)現(xiàn),因?yàn)榉桨敢缓唵危褂梅桨付?shí)現(xiàn),也可以記錄下在同一個項(xiàng)目中 通過使用多條過濾器鏈,因?yàn)椴⒉皇撬械臅r候,都是可以分成多個項(xiàng)目的。

擴(kuò)展:

1、Spring Security SecurityFilterChain 的結(jié)構(gòu)

Spring Security 多過濾鏈的使用詳解

2、控制 SecurityFilterChain 的執(zhí)行順序

使用 org.springframework.core.annotation.Order 注解。

3、查看是怎樣選擇那個 SecurityFilterChain 的

查看 org.springframework.web.filter.DelegatingFilterProxy#doFilter方法

四、實(shí)現(xiàn)1、app 端 Spring Security 的配置

package com.huan.study.security.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.annotation.Order;import org.springframework.http.HttpStatus;import org.springframework.http.MediaType;import org.springframework.security.authentication.TestingAuthenticationToken;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.core.Authentication;import org.springframework.security.core.authority.AuthorityUtils;import org.springframework.security.core.context.SecurityContextHolder;import org.springframework.security.web.SecurityFilterChain;import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;import org.springframework.util.StringUtils;import javax.servlet.http.HttpServletRequest;import java.nio.charset.StandardCharsets;/** * 給 app 端用的 Security 配置 * * @author huan.fu 2021/7/13 - 下午9:06 */@Configurationpublic class AppSecurityConfig { /** * 處理 給 app(前后端分離) 端使用的過濾鏈 * 以 json 的數(shù)據(jù)格式返回給前端 */ @Bean @Order(1) public SecurityFilterChain appSecurityFilterChain(HttpSecurity http) throws Exception {// 只處理 /api 開頭的請求return http.antMatcher('/api/**').authorizeRequests()// 所有以 /api 開頭的請求都需要 ADMIN 的權(quán)限 .antMatchers('/api/**') .hasRole('ADMIN') .and()// 捕獲到異常,直接給前端返回 json 串.exceptionHandling() .authenticationEntryPoint((request, response, authException) -> {response.setStatus(HttpStatus.UNAUTHORIZED.value());response.setCharacterEncoding(StandardCharsets.UTF_8.name());response.setContentType(MediaType.APPLICATION_JSON.toString());response.getWriter().write('{'message:':'您無權(quán)訪問01'}'); }) .accessDeniedHandler((request, response, accessDeniedException) -> {response.setStatus(HttpStatus.UNAUTHORIZED.value());response.setCharacterEncoding(StandardCharsets.UTF_8.name());response.setContentType(MediaType.APPLICATION_JSON.toString());response.getWriter().write('{'message:':'您無權(quán)訪問02'}'); }) .and()// 用戶認(rèn)證.addFilterBefore((request, response, chain) -> { // 此處可以模擬從 token 中解析出用戶名、權(quán)限等 String token = ((HttpServletRequest) request).getHeader('token'); if (!StringUtils.hasText(token)) {chain.doFilter(request, response);return; } Authentication authentication = new TestingAuthenticationToken(token, null, AuthorityUtils.createAuthorityList('ROLE_ADMIN')); SecurityContextHolder.getContext().setAuthentication(authentication); chain.doFilter(request, response);}, UsernamePasswordAuthenticationFilter.class).build(); }}

2、網(wǎng)站端 Spring Secuirty 的配置

package com.huan.study.security.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.annotation.Order;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.WebSecurityCustomizer;import org.springframework.security.core.authority.AuthorityUtils;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;import org.springframework.security.web.SecurityFilterChain;/** * 給 網(wǎng)站 應(yīng)用的安全配置 * * @author huan.fu 2021/7/14 - 上午9:09 */@Configurationpublic class WebSiteSecurityFilterChainConfig { /** * 處理 給 webSite(非前后端分離) 端使用的過濾鏈 * 以 頁面 的格式返回給前端 */ @Bean @Order(2) public SecurityFilterChain webSiteSecurityFilterChain(HttpSecurity http) throws Exception {AuthenticationManagerBuilder authenticationManagerBuilder = http.getSharedObject(AuthenticationManagerBuilder.class);// 創(chuàng)建用戶authenticationManagerBuilder.inMemoryAuthentication().withUser('admin') .password(new BCryptPasswordEncoder().encode('admin')) .authorities(AuthorityUtils.commaSeparatedStringToAuthorityList('ROLE_ADMIN')) .and().withUser('dev') .password(new BCryptPasswordEncoder().encode('dev')) .authorities(AuthorityUtils.commaSeparatedStringToAuthorityList('ROLE_DEV')) .and().passwordEncoder(new BCryptPasswordEncoder());// 只處理 所有 開頭的請求return http.antMatcher('/**').authorizeRequests()// 所有請求都必須要認(rèn)證才可以訪問 .anyRequest() .hasRole('ADMIN') .and()// 禁用csrf.csrf() .disable()// 啟用表單登錄.formLogin() .permitAll() .and()// 捕獲成功認(rèn)證后無權(quán)限訪問異常,直接跳轉(zhuǎn)到 百度.exceptionHandling() .accessDeniedHandler((request, response, exception) -> {response.sendRedirect('http://www.baidu.com'); }) .and().build(); } /** * 忽略靜態(tài)資源 */ @Bean public WebSecurityCustomizer webSecurityCustomizer( ){return web -> web.ignoring().antMatchers('/**/js/**').antMatchers('/**/css/**'); }}

3、控制器寫法

/** * 資源控制器 * * @author huan.fu 2021/7/13 - 下午9:33 */@Controllerpublic class ResourceController { /** * 返回用戶信息 */ @GetMapping('/api/userInfo') @ResponseBody public Authentication showUserInfoApi() {return SecurityContextHolder.getContext().getAuthentication(); } @GetMapping('/index') public String index(Model model){model.addAttribute('username','張三');return 'index'; }}

4、引入jar包

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>五、實(shí)現(xiàn)效果1、app 有權(quán)限訪問 api

Spring Security 多過濾鏈的使用詳解

2、app 無權(quán)限訪問 api

Spring Security 多過濾鏈的使用詳解

3、admin 用戶有權(quán)限訪問 網(wǎng)站 api

Spring Security 多過濾鏈的使用詳解

4、dev 用戶無權(quán)限訪問 網(wǎng)站 api

Spring Security 多過濾鏈的使用詳解

訪問無權(quán)限的API直接跳轉(zhuǎn)到 百度 首頁。

六、完整代碼

https://gitee.com/huan1993/Spring-Security/tree/master/multi-security-filter-chain

到此這篇關(guān)于Spring Security 多過濾鏈的使用詳解的文章就介紹到這了,更多相關(guān)Spring Security 多過濾鏈 內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲免费激情| 色偷偷偷在线视频播放| 亚洲精品网址| 精品在线播放| 91久久在线| 日韩午夜av在线| 久久亚洲欧洲| 天堂俺去俺来也www久久婷婷| 亚洲资源网站| 欧美专区一区| 嫩草伊人久久精品少妇av杨幂 | 视频小说一区二区| 欧美日韩在线二区| 黄色成人在线网址| 欧美日韩国产免费观看视频| 国产在线成人| 久久一二三区| 国产精品亚洲片在线播放| 你懂的亚洲视频| 成人国产综合| 国产一区日韩欧美| 男女激情视频一区| 日韩黄色在线观看| 国产精品xxx在线观看| 国产一区二区三区天码| 久久激情一区| 久久aⅴ国产紧身牛仔裤| 在线观看视频免费一区二区三区| 亚洲精品日本| 少妇精品久久久一区二区| 欧美精品国产一区| 吉吉日韩欧美| 免费在线看一区| 国产精品www994| 日韩在线第七页| 亚洲女人av| 国产另类在线| 久久久噜噜噜| 四虎精品一区二区免费| 国产精品高潮呻吟久久久久| 久久影院午夜精品| 亚洲在线网站| 国产精久久一区二区| av高清一区| 亚洲另类av| 久久精品国产在热久久| 91成人精品| 国产九九精品| 亚洲手机在线| 国产精品主播在线观看| 99久久99久久精品国产片果冰| 亚洲精品精选| 在线看片福利| 深夜日韩欧美| 麻豆国产精品777777在线| 91精品一区二区三区综合| 在线精品国产亚洲| 日产午夜精品一线二线三线| 亚洲专区一区| 麻豆中文一区二区| 亚洲欧洲一区| 麻豆成人91精品二区三区| 国产精品日本| 国产一区丝袜| 亚洲精品影视| 91精品国产91久久久久久黑人| 亚洲精品自拍| 久久久久蜜桃| 麻豆国产精品777777在线| 国产一区二区高清| 91亚洲一区| 青草综合视频| 亚洲资源av| 色爱综合网欧美| 日韩中文字幕亚洲一区二区va在线| 国语对白精品一区二区| 亚洲精品在线国产| 亚洲国产专区校园欧美| 国产一区国产二区国产三区| 在线免费观看亚洲| 日本国产精品| 欧美激情五月| 日产欧产美韩系列久久99| 亚洲国产影院| 中文在线中文资源| 国产精品三级| 日韩在线视频一区二区三区| 1024精品久久久久久久久| 91免费精品| 国产探花一区二区| 日韩专区欧美专区| 国产91精品对白在线播放| 精品亚洲精品| 国产三级一区| 日本不卡一区二区三区| 国产一区二区精品| 欧美亚洲国产激情| 国产v综合v| 精品国产亚洲日本| 国产欧美日韩一级| 91麻豆精品激情在线观看最新| 亚洲作爱视频| 亚洲国产不卡| 蜜臀av免费一区二区三区| 国产aⅴ精品一区二区三区久久| 人人爱人人干婷婷丁香亚洲| 免费观看日韩电影| 在线综合亚洲| 尤物网精品视频| 午夜精品一区二区三区国产| 亚洲四虎影院| 日韩不卡视频在线观看| 免费福利视频一区二区三区| 精品资源在线| 国产精品igao视频网网址不卡日韩| 91精品麻豆| 国产精品一区二区三区四区在线观看| 日韩欧美中文在线观看| 日韩一区精品视频| 久久午夜视频| 视频一区欧美精品| 亚洲一区中文| 亚洲一级淫片| 深夜日韩欧美| 欧美中文高清| 久久的色偷偷| 日韩av有码| 欧美日韩精品免费观看视欧美高清免费大片| 91视频久久| 久久久久国产一区二区| 久久婷婷亚洲| 亚洲激情中文| 在线视频亚洲欧美中文| 综合亚洲视频| 日韩精品一卡二卡三卡四卡无卡| 自拍自偷一区二区三区| 久久国产生活片100| 麻豆中文一区二区| 日韩欧美一区免费| 欧美精选一区二区三区| 免费观看在线综合| 日本在线成人| 国产精品久久免费视频| 97精品国产福利一区二区三区| 成人看片网站| 噜噜噜久久亚洲精品国产品小说| 深夜福利亚洲| 国产精品va| 91精品推荐| 99日韩精品| 日韩国产在线一| 国内精品伊人| 好吊视频一区二区三区四区| 亚洲三级观看| 麻豆成人综合网| 欧美日韩一二| 亚洲狼人精品一区二区三区| 国产精品xxx在线观看| 五月激情久久| 免费在线看一区| 里番精品3d一二三区| 91精品国产乱码久久久久久久| 亚洲欧美日韩国产| 国产精品99精品一区二区三区∴| 福利片在线一区二区| 国产精品99一区二区| 五月国产精品| 精品国产aⅴ| 亚洲手机在线| 欧美一区免费| 亚洲电影有码| 亚洲资源网站| 久久久久久婷| 久久高清国产| 99精品视频在线| 日韩一区二区三区免费视频| 国内一区二区三区| 国产精品日韩| 国产精品美女久久久久久不卡| 欧洲av一区二区| 国产精品日韩| 精品国产一区二区三区av片| 亚洲激情偷拍| 里番精品3d一二三区| 欧美美女一区| 久久激五月天综合精品| 今天的高清视频免费播放成人| 日本不卡不码高清免费观看 | 特黄特色欧美大片| 亚洲久久在线| 中文在线中文资源| 日韩国产欧美在线播放| 天堂av在线| 日韩av字幕| 九九色在线视频| 日韩黄色av| 五月综合激情| 国产一区二区三区探花| 四虎成人精品一区二区免费网站| 日韩在线中文|