百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程字典 > 正文

单点登录系统实现(单点登录的实现)

toyiye 2024-09-02 02:36 4 浏览 0 评论

单点登录系统实现基于SpringBoot

今天的干货有点湿,里面夹杂着我的泪水。可能也只有代码才能让我暂时的平静。通过本章内容你将学到单点登录系统和传统登录系统的区别,单点登录系统设计思路,Spring4 Java配置方式整合HttpClient,整合Redis ,HttpClient简易教程。还在等什么?撸起袖子开始干吧!

效果图:8081端口是sso系统,其他两个8082和8083端口模拟两个系统。登录成功后检查Redis数据库中是否有值

技术:SpringBoot,SpringMVC,Spring,SpringData,Redis,HttpClient

说明:本章的用户登录注册的代码部分已经在SpringBoot基础入门中介绍过了,这里不会重复贴代码。

源码: https://github.com/ITDragonBlog/daydayup/tree/master/SpringBoot-SSO

SpringBoot基础入门:http://www.cnblogs.com/itdragon/p/8047132.html

单点登录系统简介

在传统的系统,或者是只有一个服务器的系统中。Session在一个服务器中,各个模块都可以直接获取,只需登录一次就进入各个模块。若在服务器集群或者是分布式系统架构中,每个服务器之间的Session并不是共享的,这会出现每个模块都要登录的情况。这时候需要通过单点登录系统(Single Sign On)将用户信息存在Redis数据库中实现Session共享的效果。从而实现一次登录就可以访问所有相互信任的应用系统。

单点登录系统实现

Maven项目核心配置文件 pom.xml 需要在原来的基础上添加 httpclient和jedis jar包

 <dependency> <!-- http client version is 4.5.3 -->
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency> <!-- redis java client version is 2.9.0 -->
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>

Spring4 Java配置方式

这里,我们需要整合httpclient用于各服务之间的通讯(也可以用okhttp)。同时还需要整合redis用于存储用户信息(Session共享)。

在Spring3.x之前,一般在应用的基本配置用xml,比如数据源、资源文件等。业务开发用注解,比如Component,Service,Controller等。其实在Spring3.x的时候就已经提供了Java配置方式。现在的Spring4.x和SpringBoot都开始推荐使用Java配置方式配置bean。它可以使bean的结构更加的清晰。

整合 HttpClient

HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。HttpClient4.5系列教程 : http://blog.csdn.net/column/details/httpclient.html

首先在src/main/resources 目录下创建 httpclient.properties 配置文件

#设置整个连接池默认最大连接数
http.defaultMaxPerRoute=100
#设置整个连接池最大连接数
http.maxTotal=300
#设置请求超时
http.connectTimeout=1000
#设置从连接池中获取到连接的最长时间
http.connectionRequestTimeout=500
#设置数据传输的最长时间
http.socketTimeout=10000

然后在 src/main/java/com/itdragon/config 目录下创建 HttpclientSpringConfig.java 文件

这里用到了四个很重要的注解

@Configuration : 作用于类上,指明该类就相当于一个xml配置文件

@Bean : 作用于方法上,指明该方法相当于xml配置中的

@PropertySource : 指定读取的配置文件,引入多个value={"xxx:xxx","xxx:xxx"},ignoreResourceNotFound=true 文件不存在时忽略

@Value : 获取配置文件的值,该注解还有很多语法知识,这里暂时不扩展开

package com.itdragon.config;

import java.util.concurrent.TimeUnit;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.IdleConnectionEvictor;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;

/**
* @Configuration 作用于类上,相当于一个xml配置文件
* @Bean 作用于方法上,相当于xml配置中的<bean>
* @PropertySource 指定读取的配置文件
* @Value 获取配置文件的值
*/
@Configuration
@PropertySource(value = "classpath:httpclient.properties")
public class HttpclientSpringConfig {

@Value("${http.maxTotal}")
private Integer httpMaxTotal;

@Value("${http.defaultMaxPerRoute}")
private Integer httpDefaultMaxPerRoute;

@Value("${http.connectTimeout}")
private Integer httpConnectTimeout;

@Value("${http.connectionRequestTimeout}")
private Integer httpConnectionRequestTimeout;

@Value("${http.socketTimeout}")
private Integer httpSocketTimeout;

@Autowired
private PoolingHttpClientConnectionManager manager;

@Bean
public PoolingHttpClientConnectionManager poolingHttpClientConnectionManager {
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager;
// 最大连接数
poolingHttpClientConnectionManager.setMaxTotal(httpMaxTotal);
// 每个主机的最大并发数
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(httpDefaultMaxPerRoute);
return poolingHttpClientConnectionManager;
}

@Bean // 定期清理无效连接
public IdleConnectionEvictor idleConnectionEvictor {
return new IdleConnectionEvictor(manager, 1L, TimeUnit.HOURS);
}

@Bean // 定义HttpClient对象 注意该对象需要设置scope="prototype":多例对象
@Scope("prototype")
public CloseableHttpClient closeableHttpClient {
return HttpClients.custom.setConnectionManager(this.manager).build;
}

@Bean // 请求配置
public RequestConfig requestConfig {
return RequestConfig.custom.setConnectTimeout(httpConnectTimeout) // 创建连接的最长时间
.setConnectionRequestTimeout(httpConnectionRequestTimeout) // 从连接池中获取到连接的最长时间
.setSocketTimeout(httpSocketTimeout) // 数据传输的最长时间
.build;
}
}

整合 Redis

SpringBoot官方其实提供了spring-boot-starter-redis pom 帮助我们快速开发,但我们也可以自定义配置,这样可以更方便地掌控。Redis 系列教程 : http://www.cnblogs.com/itdragon/category/1122427.html

首先在src/main/resources 目录下创建 redis.properties 配置文件设置Redis主机的ip地址和端口号,和存入Redis数据库中的key以及存活时间。这里为了方便测试,存活时间设置的比较小。这里的配置是单例Redis。

redis.node.host=192.168.225.131
redis.node.port=6379

REDIS_USER_SESSION_KEY=REDIS_USER_SESSION
SSO_SESSION_EXPIRE=30

在src/main/java/com/itdragon/config 目录下创建 RedisSpringConfig.java 文件

package com.itdragon.config;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;

@Configuration
@PropertySource(value = "classpath:redis.properties")
public class RedisSpringConfig {

@Value("${redis.maxTotal}")
private Integer redisMaxTotal;

@Value("${redis.node.host}")
private String redisNodeHost;

@Value("${redis.node.port}")
private Integer redisNodePort;

private JedisPoolConfig jedisPoolConfig {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig;
jedisPoolConfig.setMaxTotal(redisMaxTotal);
return jedisPoolConfig;
}

@Bean
public JedisPool getJedisPool{ // 省略第一个参数则是采用 Protocol.DEFAULT_DATABASE
JedisPool jedisPool = new JedisPool(jedisPoolConfig, redisNodeHost, redisNodePort);
return jedisPool;
}

@Bean
public ShardedJedisPool shardedJedisPool {
List<JedisShardInfo> jedisShardInfos = new ArrayList<JedisShardInfo>;
jedisShardInfos.add(new JedisShardInfo(redisNodeHost, redisNodePort));
return new ShardedJedisPool(jedisPoolConfig, jedisShardInfos);
}
}

Service 层

在src/main/java/com/itdragon/service 目录下创建 UserService.java 文件,它负责三件事情

第一件事情:验证用户信息是否正确,并将登录成功的用户信息保存到Redis数据库中。

第二件事情:负责判断用户令牌是否过期,若没有则刷新令牌存活时间。

第三件事情:负责从Redis数据库中删除用户信息。

这里用到了一些工具类,不影响学习,可以从源码中直接获取。

package com.itdragon.service;

import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.itdragon.pojo.ItdragonResult;
import com.itdragon.pojo.User;
import com.itdragon.repository.JedisClient;
import com.itdragon.repository.UserRepository;
import com.itdragon.utils.CookieUtils;
import com.itdragon.utils.ItdragonUtils;
import com.itdragon.utils.JsonUtils;

@Service
@Transactional
@PropertySource(value = "classpath:redis.properties")
public class UserService {

@Autowired
private UserRepository userRepository;

@Autowired
private JedisClient jedisClient;

@Value("${REDIS_USER_SESSION_KEY}")
private String REDIS_USER_SESSION_KEY;

@Value("${SSO_SESSION_EXPIRE}")
private Integer SSO_SESSION_EXPIRE;

public ItdragonResult userLogin(String account, String password,
HttpServletRequest request, HttpServletResponse response) {
// 判断账号密码是否正确
User user = userRepository.findByAccount(account);
if (!ItdragonUtils.decryptPassword(user, password)) {
return ItdragonResult.build(400, "账号名或密码错误");
}
// 生成token
String token = UUID.randomUUID.toString;
// 清空密码和盐避免泄漏
String userPassword = user.getPassword;
String userSalt = user.getSalt;
user.setPassword;
user.setSalt;
// 把用户信息写入 redis
jedisClient.set(REDIS_USER_SESSION_KEY + ":" + token, JsonUtils.objectToJson(user));
// user 已经是持久化对象,被保存在session缓存当中,若user又重新修改属性值,那么在提交事务时,此时 hibernate对象就会拿当前这个user对象和保存在session缓存中的user对象进行比较,如果两个对象相同,则不会发送update语句,否则会发出update语句。
user.setPassword(userPassword);
user.setSalt(userSalt);
// 设置 session 的过期时间
jedisClient.expire(REDIS_USER_SESSION_KEY + ":" + token, SSO_SESSION_EXPIRE);
// 添加写 cookie 的逻辑,cookie 的有效期是关闭浏览器就失效。
CookieUtils.setCookie(request, response, "USER_TOKEN", token);
// 返回token
return ItdragonResult.ok(token);
}

public void logout(String token) {
jedisClient.del(REDIS_USER_SESSION_KEY + ":" + token);
}

public ItdragonResult queryUserByToken(String token) {
// 根据token从redis中查询用户信息
String json = jedisClient.get(REDIS_USER_SESSION_KEY + ":" + token);
// 判断是否为空
if (StringUtils.isEmpty(json)) {
return ItdragonResult.build(400, "此session已经过期,请重新登录");
}
// 更新过期时间
jedisClient.expire(REDIS_USER_SESSION_KEY + ":" + token, SSO_SESSION_EXPIRE);
// 返回用户信息
return ItdragonResult.ok(JsonUtils.jsonToPojo(json, User.class));
}
}

Controller 层

负责跳转登录页面跳转

package com.itdragon.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class PageController {

@RequestMapping("/login")
public String showLogin(String redirect, Model model) {
model.addAttribute("redirect", redirect);
return "login";
}

}

负责用户的登录,退出,获取令牌的操作

package com.itdragon.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.itdragon.pojo.ItdragonResult;
import com.itdragon.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {

@Autowired
private UserService userService;

@RequestMapping(value="/login", method=RequestMethod.POST)
@ResponseBody
public ItdragonResult userLogin(String username, String password,
HttpServletRequest request, HttpServletResponse response) {
try {
ItdragonResult result = userService.userLogin(username, password, request, response);
return result;
} catch (Exception e) {
e.printStackTrace;
return ItdragonResult.build(500, "");
}
}

@RequestMapping(value="/logout/{token}")
public String logout(@PathVariable String token) {
userService.logout(token); // 思路是从Redis中删除key,实际情况请和业务逻辑结合
return "index";
}

@RequestMapping("/token/{token}")
@ResponseBody
public Object getUserByToken(@PathVariable String token) {
ItdragonResult result = ;
try {
result = userService.queryUserByToken(token);
} catch (Exception e) {
e.printStackTrace;
result = ItdragonResult.build(500, "");
}
return result;
}
}

视图层

一个简单的登录页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!doctype html>
<html lang="zh">
<head>
<meta name="viewport" content="initial-scale=1.0, width=device-width, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<title>欢迎登录</title>
<link type="image/x-icon" href="images/favicon.ico" rel="shortcut icon">
<link rel="stylesheet" href="static/css/main.css" />
</head>
<body>
<div class="wrapper">
<div class="container">
<h1>Welcome</h1>
<form method="post" onsubmit="return false;" class="form">
<input type="text" value="itdragon" name="username" placeholder="Account"/>
<input type="password" value="123456789" name="password" placeholder="Password"/>
<button type="button" id="login-button">Login</button>
</form>
</div>
<ul class="bg-bubbles">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<script type="text/javascript" src="static/js/jquery-1.10.1.min.js" ></script>
<script type="text/javascript">
var redirectUrl = "${redirect}"; // 浏览器中返回的URL
function doLogin {
$.post("/user/login", $(".form").serialize,function(data){
if (data.status == 200) {
if (redirectUrl == "") {
location.href = "http://localhost:8082";
} else {
location.href = redirectUrl;
}
} else {
alert("登录失败,原因是:" + data.msg);
}
});
}
$(function{
$("#login-button").click(function{
doLogin;
});
});
</script>
</body>
</html>

HttpClient 基础语法

这里封装了get,post请求的方法

package com.itdragon.utils;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpClientUtil {

public static String doGet(String url) {// 无参数get请求
return doGet(url, );
}

public static String doGet(String url, Map<String, String> param) {// 带参数get请求
CloseableHttpClient httpClient = HttpClients.createDefault;// 创建一个默认可关闭的Httpclient 对象
String resultMsg = "";// 设置返回值
CloseableHttpResponse response = ;// 定义HttpResponse 对象
try {
URIBuilder builder = new URIBuilder(url);// 创建URI,可以设置host,设置参数等
if (param != ) {
for (String key : param.keySet) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build;
HttpGet httpGet = new HttpGet(uri);// 创建http GET请求
response = httpClient.execute(httpGet); // 执行请求
if (response.getStatusLine.getStatusCode == 200) { // 判断返回状态为200则给返回值赋值
resultMsg = EntityUtils.toString(response.getEntity, "UTF-8");
}
} catch (Exception e) {
e.printStackTrace;
} finally { // 不要忘记关闭
try {
if (response != ) {
response.close;
}
httpClient.close;
} catch (IOException e) {
e.printStackTrace;
}
}
return resultMsg;
}

public static String doPost(String url) { // 无参数post请求
return doPost(url, );
}

public static String doPost(String url, Map<String, String> param) {// 带参数post请求
CloseableHttpClient httpClient = HttpClients.createDefault;// 创建一个默认可关闭的Httpclient 对象
CloseableHttpResponse response = ;
String resultMsg = "";
try {
HttpPost httpPost = new HttpPost(url); // 创建Http Post请求
if (param != ) { // 创建参数列表
List<NameValuePair> paramList = new ArrayList<NameValuePair>;
for (String key : param.keySet) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);// 模拟表单
httpPost.setEntity(entity);
}
response = httpClient.execute(httpPost);// 执行http请求
if (response.getStatusLine.getStatusCode == 200) {
resultMsg = EntityUtils.toString(response.getEntity, "utf-8");
}
} catch (Exception e) {
e.printStackTrace;
} finally {
try {
if (response != ) {
response.close;
}
httpClient.close;
} catch (IOException e) {
e.printStackTrace;
}
}
return resultMsg;
}

public static String doPostJson(String url, String json) {
CloseableHttpClient httpClient = HttpClients.createDefault;
CloseableHttpResponse response = ;
String resultString = "";
try {
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
if (response.getStatusLine.getStatusCode == 200) {
resultString = EntityUtils.toString(response.getEntity, "utf-8");
}
} catch (Exception e) {
e.printStackTrace;
} finally {
try {
if (response != ) {
response.close;
}
httpClient.close;
} catch (IOException e) {
e.printStackTrace;
}
}
return resultString;
}
}

Spring 自定义拦截器

这里是另外一个项目 itdragon-service-test-sso 中的代码,首先在src/main/resources/spring/springmvc.xml 中配置拦截器,设置那些请求需要拦截

 <!-- 拦截器配置 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/github/**"/>
<bean class="com.itdragon.interceptors.UserLoginHandlerInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>

然后在 src/main/java/com/itdragon/interceptors 目录下创建 UserLoginHandlerInterceptor.java 文件

package com.itdragon.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.itdragon.pojo.User;
import com.itdragon.service.UserService;
import com.itdragon.utils.CookieUtils;

public class UserLoginHandlerInterceptor implements HandlerInterceptor {

public static final String COOKIE_NAME = "USER_TOKEN";

@Autowired
private UserService userService;

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String token = CookieUtils.getCookieValue(request, COOKIE_NAME);
User user = this.userService.getUserByToken(token);
if (StringUtils.isEmpty(token) || == user) {
// 跳转到登录页面,把用户请求的url作为参数传递给登录页面。
response.sendRedirect("http://localhost:8081/login?redirect=" + request.getRequestURL);
// 返回false
return false;
}
// 把用户信息放入Request
request.setAttribute("user", user);
// 返回值决定handler是否执行。true:执行,false:不执行。
return true;
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) throws Exception {
}
}

可能存在的问题

SpringData 自动更新问题

SpringData 是基于Hibernate的。当User 已经是持久化对象,被保存在session缓存当中。若User又重新修改属性值,在提交事务时,此时hibernate对象就会拿当前这个User对象和保存在session缓存中的User对象进行比较,如果两个对象相同,则不会发送update语句,否则,会发出update语句。

笔者采用比较傻的方法,就是在提交事务之前把数据还原。各位如果有更好的办法请告知,谢谢!

参考博客:http://www.cnblogs.com/xiaoluo501395377/p/3380270.html

检查用户信息是否保存

登录成功后,进入Redis客户端查看用户信息是否保存成功。同时为了方便测试,也可以删除这个key。

[root@localhost bin]# ./redis-cli -h 192.168.225.131 -p 6379
192.168.225.131:6379>
192.168.225.131:6379> keys *
1) "REDIS_USER_SESSION:1d869ac0-3d22-4e22-bca0-37c8dfade9ad"
192.168.225.131:6379> get REDIS_USER_SESSION:1d869ac0-3d22-4e22-bca0-37c8dfade9ad
"{\"id\":3,\"account\":\"itdragon\",\"userName\":\"ITDragonGit\",\"plainPassword\":,\"password\":,\"salt\":,\"iphone\":\"12349857999\",\"email\":\"itdragon@git.com\",\"platform\":\"github\",\"createdDate\":\"2017-12-22 21:11:19\",\"updatedDate\":\"2017-12-22 21:11:19\"}"

总结

1 单点登录系统通过将用户信息放在Redis数据库中实现共享Session效果。

2 Java 配置方式使用四个注解 @Configuration @Bean @PropertySource @Value 。

3 Spring 拦截器的设置。

4 HttpClient 的使用。

5 祝大家圣诞节快乐!

源码: https://github.com/ITDragonBlog/daydayup/tree/master/SpringBoot-SSO

到这里,基于SpringBoot的单点登录系统就结束了,有什么不对的地方请指出。

相关推荐

# Python 3 # Python 3字典Dictionary(1)

Python3字典字典是另一种可变容器模型,且可存储任意类型对象。字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中,格式如...

Python第八课:数据类型中的字典及其函数与方法

Python3字典字典是另一种可变容器模型,且可存储任意类型对象。字典的每个键值...

Python中字典详解(python 中字典)

字典是Python中使用键进行索引的重要数据结构。它们是无序的项序列(键值对),这意味着顺序不被保留。键是不可变的。与列表一样,字典的值可以保存异构数据,即整数、浮点、字符串、NaN、布尔值、列表、数...

Python3.9又更新了:dict内置新功能,正式版十月见面

机器之心报道参与:一鸣、JaminPython3.8的热乎劲还没过去,Python就又双叒叕要更新了。近日,3.9版本的第四个alpha版已经开源。从文档中,我们可以看到官方透露的对dic...

Python3 基本数据类型详解(python三种基本数据类型)

文章来源:加米谷大数据Python中的变量不需要声明。每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。在Python中,变量就是变量,它没有类型,我们所说的"类型"是变...

一文掌握Python的字典(python字典用法大全)

字典是Python中最强大、最灵活的内置数据结构之一。它们允许存储键值对,从而实现高效的数据检索、操作和组织。本文深入探讨了字典,涵盖了它们的创建、操作和高级用法,以帮助中级Python开发...

超级完整|Python字典详解(python字典的方法或操作)

一、字典概述01字典的格式Python字典是一种可变容器模型,且可存储任意类型对象,如字符串、数字、元组等其他容器模型。字典的每个键值key=>value对用冒号:分割,每个对之间用逗号,...

Python3.9版本新特性:字典合并操作的详细解读

处于测试阶段的Python3.9版本中有一个新特性:我们在使用Python字典时,将能够编写出更可读、更紧凑的代码啦!Python版本你现在使用哪种版本的Python?3.7分?3.5分?还是2.7...

python 自学,字典3(一些例子)(python字典有哪些基本操作)

例子11;如何批量复制字典里的内容2;如何批量修改字典的内容3;如何批量修改字典里某些指定的内容...

Python3.9中的字典合并和更新,几乎影响了所有Python程序员

全文共2837字,预计学习时长9分钟Python3.9正在积极开发,并计划于今年10月发布。2月26日,开发团队发布了alpha4版本。该版本引入了新的合并(|)和更新(|=)运算符,这个新特性几乎...

Python3大字典:《Python3自学速查手册.pdf》限时下载中

最近有人会想了,2022了,想学Python晚不晚,学习python有前途吗?IT行业行业薪资高,发展前景好,是很多求职群里严重的香饽饽,而要进入这个高薪行业,也不是那么轻而易举的,拿信工专业的大学生...

python学习——字典(python字典基本操作)

字典Python的字典数据类型是基于hash散列算法实现的,采用键值对(key:value)的形式,根据key的值计算value的地址,具有非常快的查取和插入速度。但它是无序的,包含的元素个数不限,值...

324页清华教授撰写【Python 3 菜鸟查询手册】火了,小白入门字典

如何入门学习python...

Python3.9中的字典合并和更新,了解一下

全文共2837字,预计学习时长9分钟Python3.9正在积极开发,并计划于今年10月发布。2月26日,开发团队发布了alpha4版本。该版本引入了新的合并(|)和更新(|=)运算符,这个新特性几乎...

python3基础之字典(python中字典的基本操作)

字典和列表一样,也是python内置的一种数据结构。字典的结构如下图:列表用中括号[]把元素包起来,而字典是用大括号{}把元素包起来,只不过字典的每一个元素都包含键和值两部分。键和值是一一对应的...

取消回复欢迎 发表评论:

请填写验证码