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

您的位置:首頁技術文章
文章詳情頁

Spring Security 多過濾鏈的使用詳解

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

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

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

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

方案二

在同一個服務中編寫。不同的請求使用不同的SecurityFilterChain來實現。

經過考慮,此處采用方案二來實現,因為方案一簡單,使用方案二實現,也可以記錄下在同一個項目中 通過使用多條過濾器鏈,因為并不是所有的時候,都是可以分成多個項目的。

擴展:

1、Spring Security SecurityFilterChain 的結構

Spring Security 多過濾鏈的使用詳解

2、控制 SecurityFilterChain 的執行順序

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

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

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

四、實現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 的數據格式返回給前端 */ @Bean @Order(1) public SecurityFilterChain appSecurityFilterChain(HttpSecurity http) throws Exception {// 只處理 /api 開頭的請求return http.antMatcher('/api/**').authorizeRequests()// 所有以 /api 開頭的請求都需要 ADMIN 的權限 .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:':'您無權訪問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:':'您無權訪問02'}'); }) .and()// 用戶認證.addFilterBefore((request, response, chain) -> { // 此處可以模擬從 token 中解析出用戶名、權限等 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、網站端 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;/** * 給 網站 應用的安全配置 * * @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);// 創建用戶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()// 所有請求都必須要認證才可以訪問 .anyRequest() .hasRole('ADMIN') .and()// 禁用csrf.csrf() .disable()// 啟用表單登錄.formLogin() .permitAll() .and()// 捕獲成功認證后無權限訪問異常,直接跳轉到 百度.exceptionHandling() .accessDeniedHandler((request, response, exception) -> {response.sendRedirect('http://www.baidu.com'); }) .and().build(); } /** * 忽略靜態資源 */ @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>五、實現效果1、app 有權限訪問 api

Spring Security 多過濾鏈的使用詳解

2、app 無權限訪問 api

Spring Security 多過濾鏈的使用詳解

3、admin 用戶有權限訪問 網站 api

Spring Security 多過濾鏈的使用詳解

4、dev 用戶無權限訪問 網站 api

Spring Security 多過濾鏈的使用詳解

訪問無權限的API直接跳轉到 百度 首頁。

六、完整代碼

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

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

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
视频一区欧美日韩| 91精品国产自产在线观看永久∴| 九一国产精品| 欧美91福利在线观看| 99国产精品免费视频观看| 婷婷激情一区| 亚洲调教视频在线观看| 欧美日韩在线观看视频小说| 亚洲国产专区| 在线精品视频在线观看高清| 国产手机视频一区二区| 亚洲一区有码| 日韩av一区二区在线影视| 午夜性色一区二区三区免费视频| 日韩一区二区三区精品| 国产欧美一区| 日韩精品a在线观看91| 日本欧美一区二区在线观看| 国产日韩视频| 激情综合五月| 久久久久亚洲| 亚洲欧美日韩国产| 日韩亚洲精品在线观看| 91精品国产自产精品男人的天堂 | 首页欧美精品中文字幕| 高清一区二区三区| 日韩一区二区三区在线免费观看| 久久久久久美女精品| 国产亚洲福利| 日韩av中文字幕一区二区三区| 久久亚州av| 亚洲高清成人| 亚洲精品在线国产| 麻豆一区在线| 欧美日韩精品一区二区视频| 国产精品日本欧美一区二区三区| 亚洲精品裸体| 国产一区丝袜| 99riav国产精品| 国产亚洲欧美日韩精品一区二区三区| 日本激情一区| 久久国产精品99国产| 日韩国产精品久久久久久亚洲| 久久久精品区| 欧美一级精品| 日韩成人av影视| 亚洲成人一区在线观看| 综合激情网...| 成人综合一区| 蜜桃久久av一区| 美女久久久久久 | 性色一区二区| 另类欧美日韩国产在线| 亚洲涩涩在线| 蜜桃久久精品一区二区| 精品中文字幕一区二区三区四区| 最新日韩欧美| 欧美黑人巨大videos精品| 极品裸体白嫩激情啪啪国产精品| 久久精品 人人爱| 日韩欧美一区二区三区在线观看| 中文字幕一区二区精品区| 亚洲国产福利| 奇米亚洲欧美| 久久三级视频| 国产精品亚洲综合色区韩国| 婷婷色综合网| 国内不卡的一区二区三区中文字幕| 日韩午夜在线| 久久久久久婷| 欧美日韩一区二区国产| 久久久亚洲一区| 牛牛精品成人免费视频| 丝袜脚交一区二区| 日韩欧美一区二区三区免费看| 欧美一级一区| 亚洲涩涩av| 久久精品免费一区二区三区| 国产精品一区二区三区四区在线观看 | 美女少妇全过程你懂的久久| 国产免费av国片精品草莓男男| 91精品二区| 精品亚洲精品| 日韩欧美在线精品| 中文精品视频| 日韩另类视频| 久久成人高清| 亚洲3区在线| 午夜精品免费| av日韩中文| 国产亚洲字幕| 中文字幕一区二区三区日韩精品 | 国产日韩欧美三级| 久久成人精品| 波多野结衣一区| 天堂√中文最新版在线| 国产剧情在线观看一区| 亚洲欧美日韩视频二区| 国产亚洲一区二区手机在线观看| 麻豆免费精品视频| 国产亚洲欧美日韩精品一区二区三区| 亚洲在线观看| 999国产精品永久免费视频app| 精品网站999| 久久精品 人人爱| 色8久久久久| 亚洲一区二区三区高清| 欧美1级日本1级| 99久久九九| 午夜精品成人av| 97国产成人高清在线观看| 国产激情一区| 久久国产精品色av免费看| 日韩精品导航| 日韩精品一区二区三区免费视频| 亚洲精一区二区三区| 亚洲在线久久| 亚洲+小说+欧美+激情+另类| 中文字幕日本一区二区| 亚洲日本在线观看视频| 日韩精品一二三区| 久热精品在线| 在线精品视频一区| 日韩中文字幕麻豆| 蜜臀av性久久久久蜜臀aⅴ四虎 | 99久久夜色精品国产亚洲狼| 中文字幕在线视频网站| 不卡福利视频| 香蕉视频亚洲一级| 亚洲第一精品影视| 好吊日精品视频| 亚洲欧美日韩综合国产aⅴ| 免费视频久久| 一区二区精彩视频| 日韩高清成人在线| 国产精品日韩精品在线播放| 国产精品xxx| 日韩av在线中文字幕| 中文字幕在线看片| 欧美日韩在线观看视频小说| 伊人成人在线视频| av不卡在线看| 亚洲精品乱码日韩| 国产亚洲人成a在线v网站| 欧美精品不卡| 国产福利片在线观看| 欧美精选视频一区二区| 欧美 日韩 国产精品免费观看| 亚洲成人一区| 综合欧美精品| 国产精品xxxav免费视频| 激情黄产视频在线免费观看| 快播电影网址老女人久久| 性欧美69xoxoxoxo| 亚洲精品美女91| 国产精品porn| 国产成人在线中文字幕| 欧美1级日本1级| 亚洲精品第一| 精品国产亚洲一区二区在线观看| 日韩一区电影| 久久国产99| 国产精品一区二区三区四区在线观看 | 中文在线免费视频| 91久久视频| 清纯唯美亚洲综合一区| 欧美交a欧美精品喷水| 亚洲精品国产嫩草在线观看| 蜜桃一区二区三区在线| 美女久久精品| 欧美日韩国产免费观看 | 99久久亚洲精品蜜臀| 亚洲久久在线| 国产精品99一区二区三区| 悠悠资源网久久精品| 欧美在线看片| 丝袜美腿诱惑一区二区三区 | 精品视频在线观看网站| 欧美日韩国产在线一区| 国产精品一区二区三区四区在线观看| 日韩免费在线| 婷婷综合国产| 97在线精品| 蜜桃久久av一区| 成人黄色av| 亚洲一区二区小说| а√在线中文在线新版| 一区二区国产精品| 免费观看亚洲| 日韩免费精品| 91精品综合| 97久久亚洲| 九九综合九九| 久久免费影院| 美女久久网站| 国产传媒在线| 欧美偷窥清纯综合图区| 欧美二区视频| 欧美xxxx性| 在线免费观看亚洲|