1. 程式人生 > 程式設計 >SpringBoot Security前後端分離登入驗證的實現

SpringBoot Security前後端分離登入驗證的實現

最近一段時間在研究OAuth2的使用,想整個單點登入,從網上找了很多demo都沒有實施成功,也許是因為壓根就不懂OAuth2的原理導致。有那麼幾天,越來越沒有頭緒,又不能放棄,轉過頭來一想,OAuth2是在Security的基礎上擴充套件的,對於Security自己都是一無所知,乾脆就先研究一下Security吧,先把Security搭建起來,找找感覺。

說幹就幹,在現成的SpringBoot 2.1.4.RELEASE環境下,進行Security的使用。
簡單的Security的使用就不說了,目前的專案是前後端分離的,登入成功或者失敗返回的資料格式必須JSON形式的,未登入時也需要返回JSON格式的提示資訊 ,退出時一樣需要返回JSON格式的資料。授權先不管,先返回JSON格式的資料,這一個搞定,也研究了好幾天,翻看了很多別人的經驗,別人的經驗有的看得懂,有的看不懂,關鍵時刻還需要自己研究呀。

下面,上程式碼:

第一步,在pom.xml中引入Security配置檔案

<dependency>
 <groupId>org.springframework.boot</groupId>  
 <artifactId>spring-boot-starter-security</artifactId>  
</dependency>

第二步,增加Configuration配置檔案

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
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.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 參考網址:
 * https://blog.csdn.net/XlxfyzsFdblj/article/details/82083443
 * https://blog.csdn.net/lizc_lizc/article/details/84059004
 * https://blog.csdn.net/XlxfyzsFdblj/article/details/82084183
 * https://blog.csdn.net/weixin_36451151/article/details/83868891
 * 查找了很多檔案,有用的還有有的,感謝他們的辛勤付出
 * Security配置檔案,專案啟動時就載入了
 * @author 程就人生
 *
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {  
 
 @Autowired
 private MyPasswordEncoder myPasswordEncoder;
 
 @Autowired
 private UserDetailsService myCustomUserService;
 
 @Autowired
 private ObjectMapper objectMapper;

 @Override
 protected void configure(HttpSecurity http) throws Exception {
 
  http
  .authenticationProvider(authenticationProvider())
  .httpBasic()
  //未登入時,進行json格式的提示,很喜歡這種寫法,不用單獨寫一個又一個的類
   .authenticationEntryPoint((request,response,authException) -> {
    response.setContentType("application/json;charset=utf-8");
    response.setStatus(HttpServletResponse.SC_FORBIDDEN);
    PrintWriter out = response.getWriter();
    Map<String,Object> map = new HashMap<String,Object>();
    map.put("code",403);
    map.put("message","未登入");
    out.write(objectMapper.writeValueAsString(map));
    out.flush();
    out.close();
   })
   
   .and()
   .authorizeRequests()
   .anyRequest().authenticated() //必須授權才能範圍
   
   .and()
   .formLogin() //使用自帶的登入
   .permitAll()
   //登入失敗,返回json
   .failureHandler((request,ex) -> {
    response.setContentType("application/json;charset=utf-8");
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    PrintWriter out = response.getWriter();
    Map<String,401);
    if (ex instanceof UsernameNotFoundException || ex instanceof BadCredentialsException) {
     map.put("message","使用者名稱或密碼錯誤");
    } else if (ex instanceof DisabledException) {
     map.put("message","賬戶被禁用");
    } else {
     map.put("message","登入失敗!");
    }
    out.write(objectMapper.writeValueAsString(map));
    out.flush();
    out.close();
   })
   //登入成功,返回json
   .successHandler((request,authentication) -> {
    Map<String,200);
    map.put("message","登入成功");
    map.put("data",authentication);
    response.setContentType("application/json;charset=utf-8");
    PrintWriter out = response.getWriter();
    out.write(objectMapper.writeValueAsString(map));
    out.flush();
    out.close();
   })
   .and()
   .exceptionHandling()
   //沒有許可權,返回json
   .accessDeniedHandler((request,ex) -> {
    response.setContentType("application/json;charset=utf-8");
    response.setStatus(HttpServletResponse.SC_FORBIDDEN);
    PrintWriter out = response.getWriter();
    Map<String,"許可權不足");
    out.write(objectMapper.writeValueAsString(map));
    out.flush();
    out.close();
   })
   .and()
   .logout()
   //退出成功,返回json
   .logoutSuccessHandler((request,"退出成功");
    map.put("data",authentication);
    response.setContentType("application/json;charset=utf-8");
    PrintWriter out = response.getWriter();
    out.write(objectMapper.writeValueAsString(map));
    out.flush();
    out.close();
   })
   .permitAll();
   //開啟跨域訪問
   http.cors().disable();
   //開啟模擬請求,比如API POST測試工具的測試,不開啟時,API POST為報403錯誤
   http.csrf().disable();
 }
 
 @Override
 public void configure(WebSecurity web) {
  //對於在header裡面增加token等類似情況,放行所有OPTIONS請求。
  web.ignoring().antMatchers(HttpMethod.OPTIONS,"/**");
 }

 @Bean
 public AuthenticationProvider authenticationProvider() {
  DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
  //對預設的UserDetailsService進行覆蓋
  authenticationProvider.setUserDetailsService(myCustomUserService);
  authenticationProvider.setPasswordEncoder(myPasswordEncoder);
  return authenticationProvider;
 }
 
}

第三步,實現UserDetailsService介面

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
/**
 * 登入專用類
 * 自定義類,實現了UserDetailsService介面,使用者登入時呼叫的第一類
 * @author 程就人生
 *
 */
@Component
public class MyCustomUserService implements UserDetailsService {

 /**
  * 登陸驗證時,通過username獲取使用者的所有許可權資訊
  * 並返回UserDetails放到spring的全域性快取SecurityContextHolder中,以供授權器使用
  */
 @Override
 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  //在這裡可以自己呼叫資料庫,對username進行查詢,看看在資料庫中是否存在
  MyUserDetails myUserDetail = new MyUserDetails();
  myUserDetail.setUsername(username);
  myUserDetail.setPassword("123456");
  return myUserDetail;
 }
}

說明:這個類,主要是用來接收登入傳遞過來的使用者名稱,然後可以在這裡擴充套件,查詢該使用者名稱在資料庫中是否存在,不存在時,可以丟擲異常。本測試為了演示,把資料寫死了。

第四步,實現PasswordEncoder介面

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
/**
 * 自定義的密碼加密方法,實現了PasswordEncoder介面
 * @author 程就人生
 *
 */
@Component
public class MyPasswordEncoder implements PasswordEncoder {

 @Override
 public String encode(CharSequence charSequence) {
  //加密方法可以根據自己的需要修改
  return charSequence.toString();
 }

 @Override
 public boolean matches(CharSequence charSequence,String s) {
  return encode(charSequence).equals(s);
 }
}

說明:這個類主要是對密碼加密的處理,以及使用者傳遞過來的密碼和資料庫密碼(UserDetailsService中的密碼)進行比對。

第五步,實現UserDetails介面

import java.util.Collection;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

/**
 * 實現了UserDetails介面,只留必需的屬性,也可新增自己需要的屬性
 * @author 程就人生
 *
 */
@Component
public class MyUserDetails implements UserDetails {

 /**
  * 
  */
 private static final long serialVersionUID = 1L;

 //登入使用者名稱
 private String username;
 //登入密碼
 private String password;

 private Collection<? extends GrantedAuthority> authorities;

 public void setUsername(String username) {
  this.username = username;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
  this.authorities = authorities;
 }

 @Override
 public Collection<? extends GrantedAuthority> getAuthorities() {
  return this.authorities;
 }

 @Override
 public String getPassword() {
  return this.password;
 }

 @Override
 public String getUsername() {
  return this.username;
 }

 @Override
 public boolean isAccountNonExpired() {
  return true;
 }

 @Override
 public boolean isAccountNonLocked() {
  return true;
 }

 @Override
 public boolean isCredentialsNonExpired() {
  return true;
 }

 @Override
 public boolean isEnabled() {
  return true;
 }
}

說明:這個類是用來儲存登入成功後的使用者資料,登入成功後,可以使用下列程式碼獲取:

MyUserDetails myUserDetails= (MyUserDetails) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();

程式碼寫完了,接下來需要測試一下,經過測試才能證明程式碼的有效性,先用瀏覽器吧。

第一步測試,未登入前訪問index,頁面直接重定向到預設的login頁面了,測試介面OK。

SpringBoot Security前後端分離登入驗證的實現

圖-1

第二步測試,登入login後,返回了json資料,測試結果OK。

SpringBoot Security前後端分離登入驗證的實現

圖-2

第三步測試,訪問index,返回輸出的登入資料,測試結果OK。

SpringBoot Security前後端分離登入驗證的實現

圖-3

第四步,訪問logout,返回json資料,測試介面OK。

SpringBoot Security前後端分離登入驗證的實現

圖-4

第五步,用API POST測試,用這個工具模擬ajax請求,看請求結果如何,首先訪問index,這個必須登入後才能訪問。測試結果ok,返回了我們需要的JSON格式資料。

SpringBoot Security前後端分離登入驗證的實現

圖-5

第六步,在登入模擬對話方塊,設定環境變數,以保持登入狀態。

SpringBoot Security前後端分離登入驗證的實現

圖-6

**第七步,登入測試,返回JSON格式的資料,測試結果OK。

SpringBoot Security前後端分離登入驗證的實現

圖-7

第八步,在返回到index測試視窗,傳送請求,返回當前使用者JSON格式的資訊,測試結果OK。

SpringBoot Security前後端分離登入驗證的實現

圖-8

第九步,測試退出,返回JSON格式資料,測試結果OK

SpringBoot Security前後端分離登入驗證的實現

圖-9

第十步,退出後,再訪問index,出現問題,登入資訊還在,LOOK!

SpringBoot Security前後端分離登入驗證的實現

圖-10

把頭部的header前面的勾去掉,也就是去掉cookie,這時正常了,原因很簡單,在退出時,沒有清除cookie,這個只能到正式的環境上去測了。API POST再怎麼模擬還是和正式環境有區別的。

如果在API POST測試報403錯誤,那就需要把configuration配置檔案裡的

//開啟跨域訪問
http.cors().disable();
//開啟模擬請求,比如API POST測試工具的測試,不開啟時,API POST為報403錯誤
http.csrf().disable();

到此這篇關於SpringBoot Security前後端分離登入驗證的實現的文章就介紹到這了,更多相關SpringBoot Security登入驗證內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!