首页 > 其他分享 >基于springboot架构 钉钉扫码登录第三方应用

基于springboot架构 钉钉扫码登录第三方应用

时间:2022-12-09 14:02:27浏览次数:69  
标签:username 扫码 架构 springboot rememberMe false password public String


基于springboot架构 钉钉扫码登录第三方应用

  • ​​获取appId及appSecret​​
  • ​​项目应用​​
  • ​​1.配置文件增加如下配置​​
  • ​​2.下载sdk​​
  • ​​3.将sdk引入项目​​
  • ​​3.修改登录跳转方法/login​​
  • ​​4.登录页修改​​
  • ​​5.扫码重定向实现登录的方法如下​​
  • ​​6.重写UsernamePasswordToken​​

获取appId及appSecret

点击进入​​钉钉开发者平台​​ 的页面,点击左侧菜单的【移动接入应用-登录】,然后点击右上角的【创建扫码登录应用授权】,创建用于免登过程中验证身份的appId及appSecret,创建后即可看到appId和appSecret。

基于springboot架构 钉钉扫码登录第三方应用_扫码登录

项目应用

1.配置文件增加如下配置

基于springboot架构 钉钉扫码登录第三方应用_扫码登录_02

2.下载sdk

下载地址: https://ding-doc.dingtalk.com/doc#/faquestions/vzbp02

3.将sdk引入项目

在resources下创建lib文件夹,放入下载的sdk

基于springboot架构 钉钉扫码登录第三方应用_ide_03


pom.xml引入sdk jar包

基于springboot架构 钉钉扫码登录第三方应用_扫码登录_04

3.修改登录跳转方法/login

基于springboot架构 钉钉扫码登录第三方应用_ide_05

4.登录页修改

基于springboot架构 钉钉扫码登录第三方应用_加载_06


基于springboot架构 钉钉扫码登录第三方应用_加载_07


基于springboot架构 钉钉扫码登录第三方应用_扫码登录_08


基于springboot架构 钉钉扫码登录第三方应用_扫码登录_09


dingding.css文件

#background { position:fixed;left:0px;top:0px;
background-color:black;
width:100%;
height:100%;
opacity:0.5;
display:none;
z-Index:3;
}
#content { position:fixed;
width:420px;
height:420px;
top:0;
bottom:0;
left:0;
right:0;
margin:auto;
display:none;
cursor:pointer;
z-Index:3;
}
#close {
float: right;
margin-right: 50px;
display: block;
width: 32px;
height: 32px;
background: url(../img/close.png) no-repeat 0px 0px;
}

dingding.js文件

//钉钉扫码登录
function show() //显示隐藏层和弹出层
{
var background=document.getElementById("background");
background.style.display="block"; //显示隐藏层
document.getElementById("content").style.display="block"; //显示弹出层
//此处不采用初始加载办法,根据需要加载,提高加载速度
var appId = $("#appId").val();
var projectUrl = $("#projectUrl").val();
/*
* 解释一下goto参数,参考以下例子:
* var url = encodeURIComponent('http://localhost.me/index.php?test=1&aa=2');
* var goto = encodeURIComponent('https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=appid&response_type=code&scope=snsapi_login&state=STATE&redirect_uri='+url)
*/
var redirectUrl = projectUrl+'loginSys';
var goto = encodeURIComponent('https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid='+appId+'&response_type=code&scope=snsapi_login&state=STATE&redirect_uri='+redirectUrl);
var obj = DDLogin({
id:"login_container",//这里需要你在自己的页面定义一个HTML标签并设置id,例如<div id="login_container"></div>或<span id="login_container"></span>
goto: goto, //请参考注释里的方式
style: "border:none;background-color:#FFFFFF;",
width : "365",
height: "400"
});
var handleMessage = function (event) {
var origin = event.origin;
//console.log("origin", event.origin);
if( origin == "https://login.dingtalk.com" ) { //判断是否来自ddLogin扫码事件。
var loginTmpCode = event.data; //拿到loginTmpCode后就可以在这里构造跳转链接进行跳转了
//console.log("loginTmpCode", loginTmpCode);
window.location.href = 'https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid='+appId+'&response_type=code&scope=snsapi_login&state=STATE&redirect_uri='+redirectUrl+'&loginTmpCode='+ loginTmpCode;
}
};
if (typeof window.addEventListener != 'undefined') {
window.addEventListener('message', handleMessage, false);
} else if (typeof window.attachEvent != 'undefined') {
window.attachEvent('onmessage', handleMessage);
}
}
function hide() //去除隐藏层和弹出层
{
document.getElementById("background").style.display="none";
document.getElementById("content").style.display="none";
}

5.扫码重定向实现登录的方法如下

/**
* 扫码登录并跳转到index
* @param request
* @return
*/
@RequestMapping("/loginSys")
public String loginSys(HttpServletRequest request)
{
String code = request.getParameter("code");
//String state = request.getParameter("state");
String appId = ConstantConfig.dingtalkAppId;
String appSecret = ConstantConfig.dingtalkAppSecret;
String msg = "";
//扫码登录
try {
//获取签名值
String timestamp = String.valueOf(System.currentTimeMillis());
String urlEncodeSignature = DingDingUtil.urlEncodeSignature(timestamp,appSecret);
//获取unionId
String url = ConstantConfig.dingtalkUserInfoUrl+"?accessKey="+appId+"×tamp="+timestamp+"&signature="+urlEncodeSignature;
DefaultDingTalkClient client = new DefaultDingTalkClient(url);
OapiSnsGetuserinfoBycodeRequest req = new OapiSnsGetuserinfoBycodeRequest();
req.setTmpAuthCode(code);
OapiSnsGetuserinfoBycodeResponse response = client.execute(req,appId,appSecret);
OapiSnsGetuserinfoBycodeResponse.UserInfo userInfo = response.getUserInfo();
String unionId = userInfo.getUnionid();
User use = sysService.selectUserByUnionId(unionId);
//根据unionId 获取用户信息
if (use != null && StringUtils.isNotEmpty(use.getUnionId())) {
//已绑定账号则直接登录操作
MyUsernamePasswordToken token = new MyUsernamePasswordToken(use.getLoginName(), use.getPassword(),false,true);
Subject subject = SecurityUtils.getSubject();
subject.login(token);
return redirect("/index");
}else {
//未绑定
msg = "one";
}
} catch (Exception e) {
e.printStackTrace();
msg = "two";
}
return redirect("/login?msg="+msg);
}

6.重写UsernamePasswordToken

package com.ruoyi.framework.shiro.authc;

import org.apache.shiro.authc.UsernamePasswordToken;

/**
* Created by nao'nao on 2020/3/19.
* @author
*/
public class MyUsernamePasswordToken extends UsernamePasswordToken {
private String username;
private char[] password;
private boolean rememberMe;
private String host;
private boolean encryption;
private String passwords;

public MyUsernamePasswordToken() {
this.rememberMe = false;
this.encryption = false;
}

public MyUsernamePasswordToken(String username, char[] password) {
this(username, (char[])password, false, (String)null, false);
}

public MyUsernamePasswordToken(String username, String password) {
this(username, (char[])(password != null?password.toCharArray():null), false, (String)null, false);
}

public MyUsernamePasswordToken(String username, char[] password, String host) {
this(username, password, false, host, false);
}

public MyUsernamePasswordToken(String username, String password, String host) {
this(username, password != null?password.toCharArray():null, false, host, false);
}

public MyUsernamePasswordToken(String username, char[] password, boolean rememberMe) {
this(username, (char[])password, rememberMe, (String)null, false);
}

public MyUsernamePasswordToken(String username, String password, boolean rememberMe) {
this(username, (char[])(password != null?password.toCharArray():null), rememberMe, (String)null,false);
}

public MyUsernamePasswordToken(String username, String password, boolean rememberMe, String host) {
this(username, password != null?password.toCharArray():null, rememberMe, host,false);
}

public MyUsernamePasswordToken(String username, String passwords, boolean rememberMe, boolean encryption) {
this(username, passwords, rememberMe, (String)null, encryption);
}

public MyUsernamePasswordToken(String username, char[] password, boolean rememberMe, String host, boolean encryption) {
this.rememberMe = false;
this.encryption = false;
this.username = username;
this.password = password;
this.rememberMe = rememberMe;
this.host = host;
this.encryption = encryption;
}

public MyUsernamePasswordToken(String username, String passwords, boolean rememberMe, String host, boolean encryption) {
this.rememberMe = false;
this.encryption = false;
this.username = username;
this.passwords = passwords;
this.rememberMe = rememberMe;
this.host = host;
this.encryption = encryption;
}

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

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

@Override
public char[] getPassword() {
return password;
}

@Override
public void setPassword(char[] password) {
this.password = password;
}

@Override
public boolean isRememberMe() {
return rememberMe;
}

@Override
public void setRememberMe(boolean rememberMe) {
this.rememberMe = rememberMe;
}

@Override
public String getHost() {
return host;
}

@Override
public void setHost(String host) {
this.host = host;
}

public boolean isEncryption() {
return encryption;
}

public void setEncryption(boolean encryption) {
this.encryption = encryption;
}

public String getPasswords() {
return passwords;
}

public void setPasswords(String passwords) {
this.passwords = passwords;
}

@Override
public Object getPrincipal() {
return this.getUsername();
}

@Override
public Object getCredentials() {
if (this.isEncryption()) {
return this.getPasswords();
}else {
return this.getPassword();
}
}

@Override
public void clear() {
this.username = null;
this.host = null;
this.rememberMe = false;
this.encryption = false;
if(this.password != null) {
for(int i = 0; i < this.password.length; ++i) {
this.password[i] = 0;
}

this.password = null;
}
if (this.passwords != null) {
this.passwords = null;
}

}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(this.getClass().getName());
sb.append(" - ");
sb.append(this.username);
sb.append(", rememberMe=").append(this.rememberMe);
sb.append(", encryption=").append(this.encryption);
if(this.host != null) {
sb.append(" (").append(this.host).append(")");
}

return sb.toString();
}
}

以上为整合钉钉扫码的主要代码,并不是全部代码,仅供参考
​​​参考文献​


标签:username,扫码,架构,springboot,rememberMe,false,password,public,String
From: https://blog.51cto.com/u_10917175/5925145

相关文章

  • springboot项目打包没有生成target文件及jar包
    在pom文件中声明了pompom:打出来可以作为其他项目的maven依赖,在工程A中添加工程B的pom,A就可以使用B中的类。用在父级工程或聚合工程中。用来做jar包的版本控制。既不会出......
  • 【分布式技术专题】「架构设计方案」盘点和总结秒杀服务的功能设计及注意事项技术体系
    秒杀应该考虑哪些问题超卖问题分析秒杀的业务场景,最重要的有一点就是超卖问题,假如备货只有100个,但是最终超卖了200,一般来讲秒杀系统的价格都比较低,如果超卖将严重影响公司的......
  • springboot项目搭建,访问controller接口失败
    错误表现:Thisapplicationhasnoexplicitmappingfor/error,soyouareseeingthisasafallback.在启动类上的注解:@EnableAutoConfiguration解决:使用这个注解......
  • SpringBoot+RabbitMQ实现消息订阅发送
    RabbitMQ安装(docker)拉取rabbitmq镜像文件#未指定版本,拉取最新版本root#dockerpullrabbitmq启动镜像root#dockerrun-d--hostnamemy-rabbit--na......
  • springboot集成kettle实现对接oracle数据
    简单记录一下springboot引用kettle对接数据第一步(这一步讲述了下载kettle、创建数据库连接、转换等,如果这一步会的可以略过,直接看第二步)先从kettle官网下载kettle,官网地......
  • 创建springboot过滤器
    springboot过滤器importorg.springframework.core.ParameterizedTypeReference;importorg.springframework.web.context.WebApplicationContext;importorg.springframewo......
  • springboot+mybatis+log4j日志sql输出和文件输出
    pom引入依赖:<dependency><!--排除spring-boot-starter-logging--><groupId>org.springframework.boot</groupId><artifactId>sprin......
  • Hello,SpringBoot
    什么是SpringBoot一个javaweb的开发框架,和SpringMVC类似,对比其他javaweb框架的好处,官方说是简化开发,约定大于配置,youcan"justrun",能迅速的开发web应用,几行代码开发......
  • 如何将传统 Web 框架迁移部署到 Serverless 架构?
    与其说Serverless架构是一个新的概念,不如说它是一种全新的思路,一种新的编程范式。但是原生的Serverless开发框架却非常少。以Web框架为例,目前主流的Web框架“均不支持Se......
  • 源码浅析--整体架构
    最近一直在研读jQuery源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美。其结构明晰,高内聚、低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼......