1. 程式人生 > 實用技巧 >高階-10認證服務

高階-10認證服務

一、環境準備

建立新module,作為認證中心(社交登陸、OAuth2.0、單點登入)

二、SpringMVC檢視對映

import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

public class MyWebConfigeration implements WebMvcConfigurer {

    // 檢視對映  不需要編寫控制層,直接將請求跳轉相應頁面
    // **/login 跳轉頁面 login.html
    // 路徑對映預設get
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
    }
}

三、阿里雲簡訊驗證服務

簡訊再次校驗問題

驗證碼60秒過期,60秒內同一個手機號不能重複傳送

-- 使用redis,儲存的值後面拼上時間,傳送之前首先獲取時間判斷是否60秒

介面防刷問題

四、註冊校驗

import com.example.beans.UserRegistDTO;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class TestController {

    // 註冊資料校驗介面
    // 重定向攜帶資料,利用session原理,將資料放在session中
    // 只要跳到下一個頁面且取出資料後,session中資料就會刪掉
    @PostMapping("/regist")
    public String regist (@Valid UserRegistDTO vo, BindingResult result, 
                          // 重定向方式的資料轉發
                          RedirectAttributes redirectAttributes){
        if(result.hasErrors()){
            Map<String, String> errors = result.getFieldErrors().stream()
                .collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            redirectAttributes.addFlashAttribute("errors",errors);
            // 校驗出錯,重定向到註冊頁面
            // 重定向帶上全路徑,不然會使用當前專案ip、埠地址而不經過域名閘道器等服務
            return "redirect:http://auth.gulimall.com/reg.html";
            // 轉發方式會把post請求方式轉發過去,而註冊頁面是get請求,會報錯
            // return "forward:reg";
        }
        // TODO 註冊業務...
        // 1、驗證碼對比--> 對比完刪除
        // 2、密碼加密
        // 3、資料儲存
    }
}

分散式服務下會出現session問題!!!!!!!!!

五、MD5&MD5鹽值加密

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

// 利用MD5加密的抗修改性
// 暴力破解方式,使用彩虹表,大量儲存MD5加密資料  123456->******
// 所以不能直接用MD5加密
String s = DigestUtils.md5Hex("123456");

// 鹽值加密
// 驗證:密碼鹽值(鹽值可以儲存在資料庫)加密然後和資料庫中的密碼對比
Md5Crypt.apr1Crypt("123456".getBytes(), "$s54g5s4d");

// spring的加密工具
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
// 加密
String encode = bCryptPasswordEncoder.encode("123456");
// 匹配,spring的加密資料中可以體現出加密的鹽值
boolean matches = bCryptPasswordEncoder.matches("123456","#89Hsd454$YHioh498s456dg46sd");

六、社交登陸

OAuth2.0

  • OAuth:OAuth(開放授權)是一個開放標準,允許使用者授權第三方網站訪問他們儲存
    在另外的服務提供者上的資訊,而不需要將使用者名稱和密碼提供給第三方網站或分享他們
    資料的所有內容。

  • OAuth2.0:對於使用者相關的 OpenAPI(例如獲取使用者資訊,動態同步,照片,日誌,分
    享等),為了保護使用者資料的安全和隱私,第三方網站訪問使用者資料前都需要顯式的向
    使用者徵求授權。

  • 官方版流程

七、微博開放平臺體驗

授權登入頁面--> 返回code--> 使用code換區AccessToken,code只能使用一次--> 使用AccessToken查詢開放介面