一、使用antMatchers放行靜態資源:
package cn.zeal4j.configuration; import cn.zeal4j.handler.FarsAuthenticationFailureHandler; import cn.zeal4j.handler.FarsAuthenticationSuccessHandler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; /** * @author Administrator * @file IntelliJ IDEA Spring-Security-Tutorial * @create 2020 09 27 21:55 */ @Configuration public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Bean public PasswordEncoder getPasswordEncoder() { return new BCryptPasswordEncoder(); } @Override protected void configure(HttpSecurity httpSecurity) throws Exception { httpSecurity.formLogin(). // 設置登陸行為方式為表單登陸 // 登陸請求參數設置 usernameParameter("username"). passwordParameter("password"). loginPage("/login.html"). // 設置登陸頁面URL路徑 loginProcessingUrl("/login.action"). // 設置表單提交URL路徑 // successForwardUrl("/main.page"). // 設置認證成功跳轉URL路徑 POST請求 successHandler(new FarsAuthenticationSuccessHandler("https://www.acfun.cn/")). // 使用自定義的重定向登陸 // failureForwardUrl("/error.page"); // 設置認證失敗跳轉URL路徑 POST請求 failureHandler(new FarsAuthenticationFailureHandler("/error.html")); // 跨域處理,不需要跳轉了 httpSecurity.authorizeRequests(). antMatchers("/**/*.js", "/**/*.css", "/**/images/**/*.*").permitAll(). // 靜態資源放行 antMatchers("/login.html").permitAll(). // 登陸頁面允許任意訪問 antMatchers("/error.html").permitAll(). // 失敗跳轉后重定向的頁面也需要被允許訪問 anyRequest().authenticated(); // 其他請求均需要被授權訪問 // CSRF攻擊攔截關閉 httpSecurity.csrf().disable(); } }
一般JS和CSS這兩個很好確定,因為后綴很簡單就是js + css 所以對URL的匹配規則只要是后綴符合這兩個條件的就放行
但是如果算圖片在內的話就不行了,因為圖片的種類非常多,無法根據后綴名攔截,所以我這里折中的辦法是指定一個
目錄存放圖片。
二、使用regxMatchers放行資源:
如果這種常規的過濾設置不夠簡單,Secuirty還提供了正則表達式攔截處理:
package cn.zeal4j.configuration; import cn.zeal4j.handler.FarsAuthenticationFailureHandler; import cn.zeal4j.handler.FarsAuthenticationSuccessHandler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; /** * @author Administrator * @file IntelliJ IDEA Spring-Security-Tutorial * @create 2020 09 27 21:55 */ @Configuration public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Bean public PasswordEncoder getPasswordEncoder() { return new BCryptPasswordEncoder(); } @Override protected void configure(HttpSecurity httpSecurity) throws Exception { httpSecurity.formLogin(). // 設置登陸行為方式為表單登陸 // 登陸請求參數設置 usernameParameter("username"). passwordParameter("password"). loginPage("/login.html"). // 設置登陸頁面URL路徑 loginProcessingUrl("/login.action"). // 設置表單提交URL路徑 // successForwardUrl("/main.page"). // 設置認證成功跳轉URL路徑 POST請求 successHandler(new FarsAuthenticationSuccessHandler("https://www.acfun.cn/")). // 使用自定義的重定向登陸 // failureForwardUrl("/error.page"); // 設置認證失敗跳轉URL路徑 POST請求 failureHandler(new FarsAuthenticationFailureHandler("/error.html")); // 跨域處理,不需要跳轉了 httpSecurity.authorizeRequests(). regexMatchers(HttpMethod.POST, "正則表達式").permitAll(). // 還可以對符合正則表達式的請求方式進行要求,這個屬性使用來制定請求的方式 antMatchers("/**/*.js", "/**/*.css", "/**/images/*.*").permitAll(). // 靜態資源放行 antMatchers("/login.html").permitAll(). // 登陸頁面允許任意訪問 antMatchers("/error.html").permitAll(). // 失敗跳轉后重定向的頁面也需要被允許訪問 anyRequest().authenticated(); // 其他請求均需要被授權訪問 // CSRF攻擊攔截關閉 httpSecurity.csrf().disable(); } }
三、使用mvcMatchers訪問資源
使用該方法,適用於配置MVC-Servlet-Path的一個情況
MSP是對整個項目的地址增加前綴,也就是
http://localhost:8080/MSP前綴/你的具體的控制器接口
MVC-Servlet-Path的配置:
application.yml配置
spring:
# spring.mvc.servlet.path
mvc:
servlet:
path: /xxx
application.properties配置
spring.mvc.servlet.path = /xxx
我的疑問:
除了MVC之外,其實SpringBoot還允許設置項目根路徑:
server: servlet: context-path: /ctx-path
那么是不是應該連接起來的情況:
http://localhost:8080/ctx-path/MSP前綴/你的具體的控制器接口
mvcMatchers使用:
上述的項目URL就原封不動了,項目根路徑 + MSP路徑:
package cn.zeal4j.configuration; import cn.zeal4j.handler.FarsAuthenticationFailureHandler; import cn.zeal4j.handler.FarsAuthenticationSuccessHandler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; /** * @author Administrator * @file IntelliJ IDEA Spring-Security-Tutorial * @create 2020 09 27 21:55 */ @Configuration public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Bean public PasswordEncoder getPasswordEncoder() { return new BCryptPasswordEncoder(); } @Override protected void configure(HttpSecurity httpSecurity) throws Exception { httpSecurity.formLogin(). // 設置登陸行為方式為表單登陸 // 登陸請求參數設置 usernameParameter("username"). passwordParameter("password"). loginPage("/login.html"). // 設置登陸頁面URL路徑 loginProcessingUrl("/login.action"). // 設置表單提交URL路徑 // successForwardUrl("/main.page"). // 設置認證成功跳轉URL路徑 POST請求 successHandler(new FarsAuthenticationSuccessHandler("https://www.acfun.cn/")). // 使用自定義的重定向登陸 // failureForwardUrl("/error.page"); // 設置認證失敗跳轉URL路徑 POST請求 failureHandler(new FarsAuthenticationFailureHandler("/error.html")); // 跨域處理,不需要跳轉了 httpSecurity.authorizeRequests(). regexMatchers(HttpMethod.POST, "正則表達式").permitAll(). // 還可以對符合正則表達式的請求方式進行要求,這個屬性使用來制定請求的方式 antMatchers("/**/*.js", "/**/*.css", "/**/images/*.*").permitAll(). // 靜態資源放行 antMatchers("/login.html").permitAll(). // 登陸頁面允許任意訪問 antMatchers("/error.html").permitAll(). mvcMatchers("/main.page").servletPath("/xxx").permitAll().// 失敗跳轉后重定向的頁面也需要被允許訪問 anyRequest().authenticated(); // 其他請求均需要被授權訪問 // CSRF攻擊攔截關閉 httpSecurity.csrf().disable(); } }
訪問測試:
或者自行補上MSP前綴
package cn.zeal4j.configuration; import cn.zeal4j.handler.FarsAuthenticationFailureHandler; import cn.zeal4j.handler.FarsAuthenticationSuccessHandler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; /** * @author Administrator * @file IntelliJ IDEA Spring-Security-Tutorial * @create 2020 09 27 21:55 */ @Configuration public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Bean public PasswordEncoder getPasswordEncoder() { return new BCryptPasswordEncoder(); } @Override protected void configure(HttpSecurity httpSecurity) throws Exception { httpSecurity.formLogin(). // 設置登陸行為方式為表單登陸 // 登陸請求參數設置 usernameParameter("username"). passwordParameter("password"). loginPage("/login.html"). // 設置登陸頁面URL路徑 loginProcessingUrl("/login.action"). // 設置表單提交URL路徑 // successForwardUrl("/main.page"). // 設置認證成功跳轉URL路徑 POST請求 successHandler(new FarsAuthenticationSuccessHandler("https://www.acfun.cn/")). // 使用自定義的重定向登陸 // failureForwardUrl("/error.page"); // 設置認證失敗跳轉URL路徑 POST請求 failureHandler(new FarsAuthenticationFailureHandler("/error.html")); // 跨域處理,不需要跳轉了 httpSecurity.authorizeRequests(). regexMatchers(HttpMethod.POST, "正則表達式").permitAll(). // 還可以對符合正則表達式的請求方式進行要求,這個屬性使用來制定請求的方式 antMatchers("/**/*.js", "/**/*.css", "/**/images/*.*").permitAll(). // 靜態資源放行 antMatchers("/login.html").permitAll(). // 登陸頁面允許任意訪問 antMatchers("/error.html").permitAll(). // 失敗跳轉后重定向的頁面也需要被允許訪問 // mvcMatchers("/main.page").servletPath("/xxx").permitAll(). // mvcMatchers資源放行匹配 antMatchers("/xxx/main.page").permitAll(). // 或者多寫MSP的前綴 anyRequest().authenticated(); // 其他請求均需要被授權訪問 // CSRF攻擊攔截關閉 httpSecurity.csrf().disable(); } }