二维码扫码登录组件
组件编码
hippius-boot-qrcode
简介
集成到hzero-oauth服务,增加扫码登录功能
1.1 组件坐标
<dependency>
<groupId>org.hippius.boot</groupId>
<artifactId>hippius-boot-qrcode</artifactId>
<version>${hippius.version}</version>
</dependency>
使用说明
2.1 修改hzero-oauth 的pom文件
引入扫码组件以及海马汇的仓库地址
例如:
-- 引入组件
<dependency>
<groupId>org.hippius.boot</groupId>
<artifactId>hippius-boot-qrcode</artifactId>
<version>0.6.1.RELEASE</version>
</dependency>
-- 添加仓库
<repositories>
<repository>
<id>hippiusRelease</id>
<url>http://nexus.saas.hand-china.com/content/repositories/hippiusRelease</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
2.2 修改hzero-oauth的启动类
添加扫描包路径
org.hippius.boot.qrcode
例如:
@ComponentScan(value = {
"org.hippius.boot.qrcode"
})
@EnableHZeroOauth
@EnableDiscoveryClient
@SpringBootApplication
public class OauthApplication {
public static void main(String[] args) {
SpringApplication.run(OauthApplication.class, args);
}
}
2.3 修改hzero-oauth WebSecurityConfig
覆盖这个文件路径
org.hzero.oauth.config.WebSecurityConfig添加扫码登录的代码
--注入
@Autowired
private QrAuthenticationProvider qrAuthenticationProvider;
@Autowired
private QrAuthenticationDetailsSource qrAuthenticationDetailsSource;
@Autowired
private QrAuthenticationFailureHandler qrAuthenticationFailureHandler;
-- configure()方法中加入以下内容
-- 加在 http.authenticationProvider(this.smsAuthenticationProvider) 之后
QrLoginConfigurer qrLoginConfigurer = new QrLoginConfigurer();
qrLoginConfigurer.authenticationDetailsSource(this.qrAuthenticationDetailsSource).successHandler(this.authenticationSuccessHandler).failureHandler(this.qrAuthenticationFailureHandler).qrCodeParameter("qrCodeId");
http.apply(qrLoginConfigurer);
http.authenticationProvider(this.qrAuthenticationProvider);
2.4 前端登录页面说明
含有扫码功能的模版:
hips,替换hzero的main
hzero:
oauth:
# 登录页面标题
title: HZERO
login:
# 登录页面默认模板,oauth 提供了两套模板
default-template: hips
如果登录页面自定义开发,可以参考组件中hips
2.5 调用的接口说明
二维码状态接口
[POST] http://domain/oauth/token/qrLogin?qrCodeId=
qrCodeId 为空的时候获取新的二维码code
第一次调用接口的时候qrCodeId值为空,第二次根据返回的qrCodeId请求接口刷新二维码状态 获取qrCodeId之后生成内容是
hipsWebLogin://?qrCodeId=xxxxxxxxxx的二维码
返回结果
| 字段 | 说明 |
|---|---|
| success | 状态,当为true的时候登录成功 |
| code | 二维码的状态值 |
| message | 二维码状态说明 |
| qrCodeId | 二维码codeID |
返回结果:
-- 等待扫码
{
"success":false,
"code":"hips.waiting.scan",
"message":"等待用户扫描",
"qrCodeId":"7b3dea95594f4b908c0ed5dafed1773a"
}
{
"success":false,
"code":"hips.qrcode.invalid",
"message":"二维码已失效"
}
{
"success":false,
"code":"hips.waiting.confirm",
"message":"扫码成功,等待app确认"
}
{
"success":false,
"code":"hips.login.cancel",
"message":"app取消登录"
}
{
"success":false,
"code":"hips.logging.in",
"message":"app确认登录,登录中"
}
-- 当success=true 说明app授权成功,需要调用登录接口
{
"success":true,
"code":"hips.logging.in",
"message":"app确认登录,登录中"
}
登录接口
[POST] http://domain/oauth/login/scan
qrCodeId是的二维码code
参数: > qrCodeId=xxxxxxxx
具体调用方式参考组件中使用