Spring Security權(quán)限控制系列(二)

本篇主要內(nèi)容:請(qǐng)求攔截及自定義登錄頁(yè)面。
上一篇:《??Spring Security權(quán)限控制系列(一)??》
自定義攔截請(qǐng)求
默認(rèn)項(xiàng)目中引入Spring Security后會(huì)攔截所有的請(qǐng)求,這其中包括了靜態(tài)資源,這肯定不是我們希望的,接下來(lái)我們看如何進(jìn)行資源自定義的攔截。
- 新建如下靜態(tài)資源
 

- 配置靜態(tài)資源訪問(wèn)路徑
 
由于靜態(tài)資源默認(rèn)訪問(wèn)路徑是/**,這里為了區(qū)分靜態(tài)資源與Controller給靜態(tài)資源加一個(gè)前綴。
spring: mvc: static-path-pattern: /resources/**
- 訪問(wèn)靜態(tài)資源
 
先將Spring Security從項(xiàng)目中移除,然后進(jìn)行訪問(wèn)。分別訪問(wèn)index.js和index.html。

都能正常訪問(wèn),接下來(lái)將Spring Security加到項(xiàng)目中后,再進(jìn)行訪問(wèn)會(huì)發(fā)現(xiàn)之前還能訪問(wèn)的現(xiàn)在直接跳轉(zhuǎn)到了登錄頁(yè)面。

- 靜態(tài)資源放行
 
自定義配置設(shè)置路徑方向規(guī)則。
@Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter {  @Override  protected void configure(HttpSecurity http) throws Exception {    http      .authorizeRequests() // 獲取基于SpEL表達(dá)式的基于URL的授權(quán)對(duì)象      .antMatchers("/resources/**") // 設(shè)定URI路徑規(guī)則以/resoures開頭的任意請(qǐng)求      .permitAll() ; // 只要是基于上面/resources開頭的請(qǐng)求都進(jìn)行放行    http.formLogin() ; // 如果請(qǐng)求不是上面配置的訪問(wèn)uri前綴則進(jìn)行登錄  }}再次訪問(wèn)靜態(tài)資源,這時(shí)候就能正常訪問(wèn)了,沒(méi)有跳轉(zhuǎn)到登錄頁(yè)面,在訪問(wèn)Controller接口 GET /demos/home運(yùn)行結(jié)果。

發(fā)現(xiàn)靜態(tài)資源能訪問(wèn),同時(shí)我們的Controller也能訪問(wèn)。

- 修改配置只放行指定的資源
 
protected void configure(HttpSecurity http) throws Exception {  http    .authorizeRequests()    .antMatchers("/resources/**")    .permitAll() ; // 方向/resource請(qǐng)求的資源   ①  http    .authorizeRequests()    .anyRequest() // 任意請(qǐng)求    .authenticated() ; // 必須進(jìn)行登錄認(rèn)證授權(quán) ②   http.formLogin() ;}以上配置后以/resources前綴的請(qǐng)求都會(huì)方向,其它任意的請(qǐng)求都會(huì)進(jìn)行攔截跳轉(zhuǎn)到登錄頁(yè)面。
注意:上面的 ① ② 如果順序進(jìn)行顛倒后服務(wù)啟動(dòng)會(huì)報(bào)錯(cuò)。報(bào)錯(cuò)信息如下。
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。
- 為請(qǐng)求配置角色
 
定義2個(gè)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 + " - 數(shù)據(jù)" ;  }}我們期望/demos/**接口訪問(wèn)必須擁有USERS權(quán)限,/api/**接口訪問(wèn)必須擁有ADMIN權(quán)限, 配置如下:
@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() ;  // 這里無(wú)需使用ROLE_前綴,系統(tǒng)會(huì)自動(dòng)插入該前綴  http.authorizeRequests().antMatchers("/demos/**").hasRole("USERS") ; // /demos/**必須具備USERS角色  http.authorizeRequests().antMatchers("/api/**").hasRole("ADMIN") ; // /api/**必須具備ADMIN角色  http.authorizeRequests().anyRequest().authenticated() ;  http.formLogin() ;}分別訪問(wèn)/demos/home 和 /api/1接口。
通過(guò)guest/123456登錄后,該接口之間返回了403的狀態(tài)錯(cuò)誤(讀取403.html)。

我的項(xiàng)目中在static/error下新建了403.html錯(cuò)誤頁(yè)面

/api/**接口訪問(wèn)正常,接下來(lái)我們?cè)谂渲靡粋€(gè)用于USERS權(quán)限的用戶。
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") ;}通過(guò)test用戶訪問(wèn)/demos/home接口登錄后能正常訪問(wèn)。
- 配置多權(quán)限
 
在很多情況下我們期望只要用戶用于任意其中一個(gè)權(quán)限就認(rèn)定可以訪問(wèn)該資源,如何配置?
http.authorizeRequests().antMatchers("/demos/**").hasAnyRole("USERS", "AKKF", "BLLE") ;http.authorizeRequests().antMatchers("/api/**").hasAnyRole("ADMIN", "MGR", "SYSTEM") ;通過(guò)上面的配置即可滿足只要擁有任意一個(gè)權(quán)限就可以放行。
- 其它配置
 
多個(gè)URI具有相同的權(quán)限。
http.authorizeRequests().antMatchers("/demos/**", "/api/**").hasAnyAuthority("ROLE_USERS", "ROLE_ADMIN") ;對(duì)請(qǐng)求的Method控制。
http.authorizeRequests().antMatchers(HttpMethod.GET).permitAll() ;
自定義登錄頁(yè)面
- 引入依賴
 
<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/
- 登錄頁(yè)面
 
在/resources/templates/下新建login.html頁(yè)面。
這里省去無(wú)關(guān)緊要的東西。
<div class="loginContainer">  <div class="pageTitle">    <h3>認(rèn)證登錄</h3>  </div>  <div class="loginPanel">    <div class="loginTitle">安全登錄</div>      <div class="loginContent">        <form method="post" action="login">          <div class="c-row">            <label>安全帳號(hào)</label>            <input type="text" class="input-control" id="username" name="username" placeholder="帳號(hào)">          </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頁(yè)面
 
@Controllerpublic class LoginController {  @GetMapping("/custom/login")  public String login() {    return "login" ;  }}- 自定義配置登錄頁(yè)
 
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") ;  // 登錄頁(yè)面指向上面配置的Controller即可  http.formLogin().loginPage("/custom/login") ;}測(cè)試:

總結(jié):
- Spring Security如何配置攔截請(qǐng)求。
 - 資源訪問(wèn)必須具備權(quán)限的配置。
 - 自定義登錄頁(yè)面。
 















 
 
 













 
 
 
 