Spring Security(Spring安全框架)學習筆記(一)簡介、自定義登錄頁面、放過靜態資源
Spring Security(Spring安全框架)學習筆記(二)登錄接口,登錄參數,登錄回調,註銷登錄
Spring Security(Spring安全框架)學習筆記(三)返回json格式數據,適用前後端分離場景
一、配置類 SecurityConfig.java
注意:在寫路徑的時候要加上"/",否則訪問會出錯404!
package com.hx.security;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
// 密碼加密實例
PasswordEncoder passwordEncoder() {
return NoOpPasswordEncoder.getInstance(); // 採用不加密方式
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception { // 配置用戶名,密碼
//這裏的配置會覆蓋properties配置文件中配置的賬號密碼
// 配置多個使用and連接,一個就不用加and()
auth.inMemoryAuthentication()
.withUser("huathy")
.password("a")
.roles("admin")
.and()
.withUser("worthy")
.password("a")
.roles("user");
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/js/**", "/css/**", "images/**"); //放過靜態資源下的js,css,img資源
}
@Override
protected void configure(HttpSecurity http) throws Exception { //http安全配置
//authorizeRequests開啓配置
http.authorizeRequests()
.anyRequest() //anyRequest所有請求都攔截
.authenticated()
.and()
.formLogin() //formLogin表單配置
.loginPage("/login.html") //loginPage指定登錄頁面(登錄接口)
.loginProcessingUrl("/doLogin") //指定登錄請求接口,若不配置則與指定的loginPage相同
.usernameParameter("uname") //指定請求用戶名的name屬性
.passwordParameter("pwd") //指定請求密碼的name屬性
// .successForwardUrl("/success") //成功回調,服務端跳轉(地址欄不跳轉)轉發
.defaultSuccessUrl("/success",false) //默認成功跳轉頁面(地址欄跳轉)重定向,
//會記錄重定向地址,即從哪個頁面跳轉來的,登錄成功時就返回哪個頁面。參數alwaysUse爲true時,與successForwardUrl無異。
// .failureForwardUrl("/fail") //服務端跳轉,轉發
// .failureUrl("/fail") //客戶端跳轉,重定向
.permitAll() //permitAll放過相關頁面
.and()
.logout()
// .logoutUrl("/logout") //配置退出登錄地址
.logoutRequestMatcher(new AntPathRequestMatcher("/logout","POST")) //配置post請求方式的退出登錄方法
.logoutSuccessUrl("/login.html") //配置退出登錄成功的跳轉頁面
.invalidateHttpSession(true) //清除session信息,不配置默認true
.clearAuthentication(true) //清除認證信息,不配置默認true
.and()
.csrf().disable(); //關閉csrf
}
}
二、控制層 HelloController.java
package com.hx.security;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "<h1>HELLO</h1>";
}
@RequestMapping("/success")
public String success(){
return "<h1>SUCCESS</h1>";
}
@RequestMapping("/fail")
public String fail() {
return "<h1>LOGIN FAIL</h1>";
}
}
三、html頁面 login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/doLogin" method="post"> <!-- 必須指定爲post請求,地址爲login.html -->
用戶名:<input name="uname"> <br> <!-- 指定名稱username,遵循規範 -->
密碼:<input name="pwd"> <br> <!-- 指定名稱password,遵循規範 -->
<button type="submit">提交</button>
</form>
</body>
</html>
四、退出登錄logout測試
由於已經將logout更改爲post請求方式,故需要發送post請求才可退出登錄!