OAuth认证服务
组件编码
hzero-oauth
简介
hzero-oauth 服务是基于 Spring Security、Spring OAuth2、JWT 实现的统一认证服务中心,登录基于 spring security 的标准登录流程,客户端授权支持 oauth2.0 的四种授权模式:授权码模式、简化模式、密码模式、客户端模式,授权流程跟标准的 oauth2 流程一致。web 端采用简化模式(implicit)登录系统,移动端可使用密码模式(password)登录系统 。并支持基于 Spring Social 的三方账号登录方式(如微信)。
组件坐标
<dependency>
    <groupId>org.hzero</groupId>
    <artifactId>hzero-oauth</artifactId>
    <version>${hzero.service.version}</version>
</dependency>
主要功能
- 统一登录界面
 - 账户、手机、邮箱登录
 - 短信登录
 - 第三方登录功能
 - 可客制化登录模板
 
服务配置
OAuth 服务的参数配置使用场景可具体参考 OAuth 服务下的其它文档。
hzero:
  send-message:
    # 手机登录发送验证码模板代码
    mobile-login: HOTH.MOBILE_LOGIN
    # 修改密码发送验证码模板代码
    modify-login-password: HOTH.MODIFY_PASSWORD
  oauth:
    # 认证服务器 自定义资源匹配器
    # 可实现 ResourceMatcher 接口,自定义 OAuth ResourceServer 对哪些API认证
    custom-resource-matcher: false
    # 授权码模式验证 client 时不检查 client 的一致性
    not-check-client-equals: false
    # 移动设备ID参数
    device-id-parameter: device_id
    # 登录设备来源参数
    source-type-parameter: source_type
    # 移动端开启图形验证码校验,默认不开启
    enable-app-captcha: false
    # 始终开启图形验证码校验,默认否
    # 设置为 true 时,在进入登录页面时就显示图形验证码
    enable-always-captcha: false
    # client_credentials 模式是否返回 refresh_token,标准模式不返回
    credentials-allow-refresh: false
    # 登录页面标题
    title: HZERO
    login:
      # 允许使用的登录名,默认有 用户名、邮箱、手机号
      support-fields: username,email,phone
      # 手机登录参数
      mobile-parameter: phone
      # 登录页面默认模板,oauth 提供了两套模板
      # 在请求 /oauth/authorize 接口时,可通过 template 参数指定使用的模板
      default-template: main
      # 默认登录成功跳转地址
      # 在直接访问 oauth 的登录地址时,登录成功后会跳转到这个默认地址
      success-url: http://dev.hzero.org/workplace
    logout:
      # 退出时是否清理token
      clear-token: true
    sso:
      # 是否启用二级域名单点登录
      enabled: false
      service: 
        # oauth 服务地址
        baseUrl: http://dev.hzero.org:8080/oauth
      # SAML 协议登录相关配置  
      saml:
        entity_id: hzero:org:sp
        passphrase: secret
        private_key: MIIEvQIBADANBgk......
        certificate: MIIDEzCCA.......
    password:
      # 是否启用 RSA 加密
      enable-encrypt: true
      # 密码传输加密:公钥
      public-key: MFwwDQYJKoZIhvcNAQEBB......
      # 密码传输加密:私钥
      private-key: MIIBVQIBADANBgkqhkiG......
密码加密配置
- 
密码传输使用 RSA 非对称加密,注意一定要设置加密的公钥和私钥,可以直接从配置文件中拷贝,也可以使用如下工具方法生成自定义的公钥和私钥。
public static void main(String[] args) { Pair<String, String> key = EncryptionUtils.RSA.generateKeyPair(); String privateKey = key.getFirst(); String publicKey = key.getSecond(); System.out.println("PrivateKey >>>\n" + privateKey + "\n"); System.out.println("PublicKey >>>\n" + publicKey + "\n"); String content = "12345678"; System.out.println("Content >>>\n" + content + "\n"); // 使用公钥加密 String encrypt = EncryptionUtils.RSA.encrypt(content, publicKey); // 使用私钥解密 String decrypt = EncryptionUtils.RSA.decrypt(encrypt, privateKey); System.out.println("Encrypt >>>\n" + encrypt + "\n"); System.out.println("Decrypt >>>\n" + decrypt); } - 
如果覆盖或重定义了登录页面,后端访问登录页面时(/login),默认会在 Model 中返回
publicKey公钥,前端访问登录接口时,需使用 publicKey 将密码加密后传输。前端需引入/public/static/main/js/jsencrypt.min.js加密工具。使用方式如下<input id="publicKey" type="hidden" th:value="${publicKey}" /> function encryptPwd(password, publicKey) { // 初始化加密器 const encrypt = new JSEncrypt(); // 设置公钥 encrypt.setPublicKey(publicKey); // 加密 return encrypt.encrypt(password); } - 
如果后端需要对密码加密,可以使用
org.hzero.core.util.EncryptionUtils工具,使用方式如下public String encryptPwd(String password, String publicKey) { return EncryptionUtils.RSA.encrypt(password, publicKey); }