成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Spring Security權限控制系列(二)

開發 架構
默認項目中引入Spring Security后會攔截所有的請求,這其中包括了靜態資源,這肯定不是我們希望的,接下來我們看如何進行資源自定義的攔截。

本篇主要內容:請求攔截及自定義登錄頁面。

上一篇:《??Spring Security權限控制系列(一)??》

自定義攔截請求

默認項目中引入Spring Security后會攔截所有的請求,這其中包括了靜態資源,這肯定不是我們希望的,接下來我們看如何進行資源自定義的攔截。

  • 新建如下靜態資源

  • 配置靜態資源訪問路徑

由于靜態資源默認訪問路徑是/**,這里為了區分靜態資源與Controller給靜態資源加一個前綴。

spring:  mvc:    static-path-pattern: /resources/**
  • 訪問靜態資源

先將Spring Security從項目中移除,然后進行訪問。分別訪問index.js和index.html。

都能正常訪問,接下來將Spring Security加到項目中后,再進行訪問會發現之前還能訪問的現在直接跳轉到了登錄頁面。

  • 靜態資源放行

自定義配置設置路徑方向規則。

@Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter {  @Override  protected void configure(HttpSecurity http) throws Exception {    http      .authorizeRequests() // 獲取基于SpEL表達式的基于URL的授權對象      .antMatchers("/resources/**") // 設定URI路徑規則以/resoures開頭的任意請求      .permitAll() ; // 只要是基于上面/resources開頭的請求都進行放行    http.formLogin() ; // 如果請求不是上面配置的訪問uri前綴則進行登錄  }}

再次訪問靜態資源,這時候就能正常訪問了,沒有跳轉到登錄頁面,在訪問Controller接口 GET /demos/home運行結果。

發現靜態資源能訪問,同時我們的Controller也能訪問。

  • 修改配置只放行指定的資源
protected void configure(HttpSecurity http) throws Exception {  http    .authorizeRequests()    .antMatchers("/resources/**")    .permitAll() ; // 方向/resource請求的資源   ①  http    .authorizeRequests()    .anyRequest() // 任意請求    .authenticated() ; // 必須進行登錄認證授權 ②   http.formLogin() ;}

以上配置后以/resources前綴的請求都會方向,其它任意的請求都會進行攔截跳轉到登錄頁面。

注意:上面的 ① ② 如果順序進行顛倒后服務啟動會報錯。報錯信息如下。

Caused by: java.lang.IllegalStateException: Can't configure antMatchers after anyRequestat org.springframework.util.Assert.state(Assert.java:76) ~[spring-core-5.3.12.jar:5.3.12]

不能在anyRequest之后配置antMatchers。

  • 為請求配置角色

定義2個Controller。

@RestController@RequestMapping("/demos")public class DemoController {  @GetMapping("home")  public Object home() {    return "demos home" ;  }}@RestController@RequestMapping("/api")public class ApiController {  @GetMapping("/{id}")  public Object get(@PathVariable("id") Integer id) {    return "獲取 - " + id + " - 數據" ;  }}

我們期望/demos/**接口訪問必須擁有USERS權限,/api/**接口訪問必須擁有ADMIN權限, 配置如下:

@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {  // 配置guest用戶,該用戶擁有ADMIN角色  auth.inMemoryAuthentication().passwordEncoder(NoOpPasswordEncoder.getInstance()).withUser("guest").password("123456").roles("ADMIN") ;}@Overrideprotected void configure(HttpSecurity http) throws Exception {  http.csrf().disable() ;  http.authorizeRequests().antMatchers("/resources/**").permitAll() ;  // 這里無需使用ROLE_前綴,系統會自動插入該前綴  http.authorizeRequests().antMatchers("/demos/**").hasRole("USERS") ; // /demos/**必須具備USERS角色  http.authorizeRequests().antMatchers("/api/**").hasRole("ADMIN") ; // /api/**必須具備ADMIN角色  http.authorizeRequests().anyRequest().authenticated() ;  http.formLogin() ;}

分別訪問/demos/home 和 /api/1接口。

通過guest/123456登錄后,該接口之間返回了403的狀態錯誤(讀取403.html)。

我的項目中在static/error下新建了403.html錯誤頁面

/api/**接口訪問正常,接下來我們在配置一個用于USERS權限的用戶。

protected void configure(AuthenticationManagerBuilder auth) throws Exception {  auth.inMemoryAuthentication()    .passwordEncoder(NoOpPasswordEncoder.getInstance())    .withUser("guest").password("123456").roles("ADMIN")    .and()    .withUser("test").password("666666").roles("USERS") ;}

通過test用戶訪問/demos/home接口登錄后能正常訪問。

  • 配置多權限

在很多情況下我們期望只要用戶用于任意其中一個權限就認定可以訪問該資源,如何配置?

http.authorizeRequests().antMatchers("/demos/**").hasAnyRole("USERS", "AKKF", "BLLE") ;http.authorizeRequests().antMatchers("/api/**").hasAnyRole("ADMIN", "MGR", "SYSTEM") ;

通過上面的配置即可滿足只要擁有任意一個權限就可以放行。

  • 其它配置

多個URI具有相同的權限。

http.authorizeRequests().antMatchers("/demos/**", "/api/**").hasAnyAuthority("ROLE_USERS", "ROLE_ADMIN") ;

對請求的Method控制。

http.authorizeRequests().antMatchers(HttpMethod.GET).permitAll() ;

自定義登錄頁面

  • 引入依賴
<dependency>  <groupId>org.thymeleaf</groupId>  <artifactId>thymeleaf-spring5</artifactId></dependency><dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
  • thymeleaf配置
spring:  thymeleaf:    prefix: classpath:/templates/
  • 登錄頁面

在/resources/templates/下新建login.html頁面。

這里省去無關緊要的東西。

<div class="loginContainer">  <div class="pageTitle">    <h3>認證登錄</h3>  </div>  <div class="loginPanel">    <div class="loginTitle">安全登錄</div>      <div class="loginContent">        <form method="post" action="login">          <div class="c-row">            <label>安全帳號</label>            <input type="text" class="input-control" id="username" name="username" placeholder="帳號">          </div>          <div class="c-row">            <label>安全密碼</label>            <input type="password" class="input-control" id="password" name="password" placeholder="密碼">          </div>          <div class="c-row" style="height: auto;">            <input type="checkbox" class="checkbox-control" id="remember-me" name="remember-me"/><label for="remember-me">記住我</label>          </div>          <div class="c-row" style="margin-top: 20px;">            <button type="submit" class="btn btn-sm btn-primary" style="padding: 10px 15px;width: 60%;border-radius: 20px;">安全登錄</button>          </div>        </form>        <div class="c-row">          <div th:if="${param.error}" th:text="${session.SPRING_SECURITY_LAST_EXCEPTION?.message }" class="alert alert-danger" style="padding:5px; margin-bottom:5px;"></div>        </div>      </div>    </div></div>
  • Controller定義login頁面
@Controllerpublic class LoginController {  @GetMapping("/custom/login")  public String login() {    return "login" ;  }}
  • 自定義配置登錄頁
protected void configure(HttpSecurity http) throws Exception {  http.csrf().disable() ;  http.authorizeRequests().antMatchers("/resources/**").permitAll() ;  http.authorizeRequests().antMatchers("/demos/**").hasRole("USERS") ;  http.authorizeRequests().antMatchers("/api/**").hasRole("ADMIN") ;  // 登錄頁面指向上面配置的Controller即可  http.formLogin().loginPage("/custom/login") ;}

測試:

總結

  1. Spring Security如何配置攔截請求。
  2. 資源訪問必須具備權限的配置。
  3. 自定義登錄頁面。
責任編輯:姜華 來源: 今日頭條
相關推薦

2022-08-30 08:36:13

Spring權限控制

2022-08-15 08:42:46

權限控制Spring

2022-08-30 08:55:49

Spring權限控制

2022-08-30 08:43:11

Spring權限控制

2022-08-30 08:50:07

Spring權限控制

2024-02-18 12:44:22

2020-06-17 08:31:10

權限控制Spring Secu

2021-07-27 10:49:10

SpringSecurity權限

2023-01-13 08:11:24

2022-06-16 10:38:24

URL權限源代碼

2020-09-16 08:07:54

權限粒度Spring Secu

2025-06-30 01:33:00

2023-05-26 01:05:10

2022-05-05 10:40:36

Spring權限對象

2017-04-25 10:46:57

Spring BootRESRful API權限

2022-06-27 14:21:09

Spring語言

2022-01-07 07:29:08

Rbac權限模型

2021-08-29 18:36:57

項目

2021-04-23 07:33:10

SpringSecurity單元

2019-11-22 09:40:40

SpringJava編程語言
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲综合色丁香婷婷六月图片 | 激情 一区 | 日韩影音 | 国产精品久久久久久久久 | 亚洲精品一区二区三区 | 国产成人精品在线 | 一区二区三区免费在线观看 | 国产网站在线播放 | 毛片网在线观看 | 欧美精品综合在线 | 久久夜视频| 91综合网 | 欧美日在线| 日韩一级免费 | a欧美 | 久热爱| 色综合久久久久 | 色婷婷av777 av免费网站在线 | 亚洲精品在线免费播放 | 欧美xxxⅹ性欧美大片 | 亚洲一区导航 | 精品国产18久久久久久二百 | 日韩欧美综合 | 久久丝袜| 蜜臀久久99精品久久久久野外 | 国产日韩欧美在线 | 欧美日韩淫片 | 精品视频一区二区 | 精品国产精品国产偷麻豆 | 久久久久久网站 | 中文字幕国产高清 | 成人久草 | 中文字幕一区二区三区乱码图片 | 97人人超碰 | 欧美日韩中文在线 | 亚洲国产精品久久久久久 | 国产不卡在线观看 | 成人黄色电影在线播放 | av在线一区二区三区 | 99pao成人国产永久免费视频 | www性色|