提示:學習本文需要具備springboot spring 前端頁面 和Spring Security基礎相關知識
登入頁面回顯提示資訊和動态配置認證相關URL
- 一、登入頁面回顯提示資訊
-
- 1.頁面中使用thymeleaf進行回顯
- 2.修改預設讀取錯誤檔案的位址
- 3. 測試
- 二、動态配置認證相關URL
-
- 1.引入application.yml 配置處理器包
- 2.配置yml檔案
- 3.建立讀取配置類和javaBean
- 4.将配置類注入安全配置類并動态讀取
- 5.測試
- 喜歡的小夥伴可以點贊、關注、收藏哦
- -- 技術源于追求,技術改變生活 --
一、登入頁面回顯提示資訊
-
當送出登入表單資料認證失敗後,通過 http://localhost/login/page?error 重定向回登入頁,此時位址帶有一
個 error 參數,辨別認證失敗。
并且當使用者名或密碼錯誤時,背景會響應提示資訊 Bad credentials ,我們要将提示資訊在登入頁上回顯。
- 預設情況下,提示資訊預設都是英文的,其實是可配置成中文資訊。
1.頁面中使用thymeleaf進行回顯
http://localhost/login/page?error 有 error 參數就是認證失敗 th:if=" p a r a m . e r r o r " 2. l o g i n . h t m l 頁 面 渲 染 提 示 信 息 t h : t e x t = " {param.error}" 2. login.html 頁面渲染提示資訊th:text=" param.error"2.login.html頁面渲染提示資訊th:text="{session.SPRING_SECURITY_LAST_EXCEPTION?.message}
<div th:if="${param.error}">
<span th:text="${session.SPRING_SECURITY_LAST_EXCEPTION?.message}" style="color:red">使用者名或密碼錯誤</span>
</div>
2.修改預設讀取錯誤檔案的位址
預設 ReloadableResourceBundleMessageSource 是加載了 messages.properties 英文配置檔案,我們要改成messages_zh_CN.properties
/**
* @author LIJW
* @date 2021/3/27 6:18 下午
* 加載認證資訊配置類 把提示資訊修改為中文
*/
@Configuration
public class ReloadMessageConfig {
//預設 ReloadableResourceBundleMessageSource 是加載了 messages.properties 英文配置檔案;
@Bean
public ReloadableResourceBundleMessageSource messageSource(){
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:org/springframework/security/messages_zh_CN");
return messageSource;
}
}
3. 測試
二、動态配置認證相關URL
1.引入application.yml 配置處理器包
<!-- application.yml 配置處理器 在實作認證相關url可配置化時使用-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
2.配置yml檔案
server:
port: 80
spring:
thymeleaf:
cache: false #關閉thymeleaf緩存
puge:
security:
authentication:
loginPage: /login/page #響應認證(登入)頁面URL 修改後必須得使用方法進行比對
loginProcessingUrl: /login/form #登入表單送出處理Url
usernameParameter: name #登入表單使用者名的屬性名
passwordParameter: pwd #登入表單密碼的屬性名
staticPaths: #靜态資源"/dist/**","modules/**","/plugins/**"
- /dist/**
- /modules/**
- /plugins/**
3.建立讀取配置類和javaBean
/**
* @author LIJW
* @date 2021/3/28 5:16 下午
* 對應配置檔案中的參數名 配置檔案javaBean
*/
@Data
public class AuthenticationProperties {
// application.yml 沒配置取預設值
private String loginPage = "/login/page";
private String loginProcessingUrl = "/login/form";
private String usernameParameter = "name";
private String passwordParameter = "pwd";
private String[] staticPaths = {"/dist/**", "/modules/**", "/plugins/**"};
}
要注意讀取類中要封裝的屬性名一定要和yml中配置的最後一層相同
/**
* @author LIJW
* @date 2021/3/28 5:14 下午
* 讀取yml配置中的配置資訊 實作動态配置認證相關路徑
*/
@Component
@ConfigurationProperties(prefix = "puge.security")
public class securityProperties {
//本屬性名authentication要和yml配置中最後一層相同
AuthenticationProperties authentication;
/**
* 重寫get set為了友善擷取authentication 隻要将本類交給spring Ioc即可
* @return
*/
public AuthenticationProperties getAuthentication() {
return authentication;
}
public void setAuthentication(AuthenticationProperties authentication) {
this.authentication = authentication;
}
}
4.将配置類注入安全配置類并動态讀取
/**
* @author LIJW
* @date 2021/3/27 9:38 上午
* security 安全配置類
* 繼承于webSecurityConfigurerAdapter抽象類
*/
@Configuration
@EnableWebSecurity //啟動 SpringSecurity 過濾器鍊功能
@Slf4j
public class springSecurity extends WebSecurityConfigurerAdapter {
//注入擷取yml的配置類
@Autowired
private securityProperties securityProperties;
/**
* configure(HttpSecurity http) 資源權限配置(過濾器鍊)
* 1.攔截的哪一些資源
* 2.資源所對應的角色權限
* 3.定制登入頁面、登入請求位址、錯誤處理方式
* 4.自定義 spring security 過濾器等
* 5.定義認證方式:httpBasic httpForm
* @param http
* @throws Exception
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
//HttpBasic和HttpForm2種認證方式
http.formLogin()//表單認證
//注意不管是自定義登入頁面還是請求位址開頭都要加 /
.loginPage(securityProperties.getAuthentication().getLoginPage())//交給/login/page響應認證(登陸)頁面
.loginProcessingUrl(securityProperties.getAuthentication().getLoginProcessingUrl())//登入表單處理url,預設是login
.usernameParameter(securityProperties.getAuthentication().getUsernameParameter()) //修改預設的username 注:要和頁面送出的字段名字一樣
.passwordParameter(securityProperties.getAuthentication().getPasswordParameter()) //修改預設的password
.and()//連結符号
.authorizeRequests() //認證請求
.antMatchers(securityProperties.getAuthentication().getLoginPage()).permitAll()//放行跳轉認證請求
.anyRequest().authenticated() //所有進入應用的HTTP請求都要進行認證
;
}
/**
* 釋放靜态資源
* @param web
* @throws Exception
*/
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(securityProperties.getAuthentication().getStaticPaths());
}
}