前端技术
HTML
CSS
Javascript
前端框架和UI库
VUE
ReactJS
AngularJS
JQuery
NodeJS
JSON
Element-UI
Bootstrap
Material UI
服务端和客户端
Java
Python
PHP
Golang
Scala
Kotlin
Groovy
Ruby
Lua
.net
c#
c++
后端WEB和工程框架
SpringBoot
SpringCloud
Struts2
MyBatis
Hibernate
Tornado
Beego
Go-Spring
Go Gin
Go Iris
Dubbo
HessianRPC
Maven
Gradle
数据库
MySQL
Oracle
Mongo
中间件与web容器
Redis
MemCache
Etcd
Cassandra
Kafka
RabbitMQ
RocketMQ
ActiveMQ
Nacos
Consul
Tomcat
Nginx
Netty
大数据技术
Hive
Impala
ClickHouse
DorisDB
Greenplum
PostgreSQL
HBase
Kylin
Hadoop
Apache Pig
ZooKeeper
SeaTunnel
Sqoop
Datax
Flink
Spark
Mahout
数据搜索与日志
ElasticSearch
Apache Lucene
Apache Solr
Kibana
Logstash
数据可视化与OLAP
Apache Atlas
Superset
Saiku
Tesseract
系统与容器
Linux
Shell
Docker
Kubernetes
[网络问题在Oracle备份恢复过程中如何...]的搜索结果
这里是文章列表。热门标签的颜色随机变换,标签颜色没有特殊含义。
点击某个标签可搜索标签相关的文章。
点击某个标签可搜索标签相关的文章。
Oracle
...遇到各种各样的数据库问题,其中最常见的就是数据库无法备份或恢复。这可能是因为各种乱七八糟的因素导致的,比如系统抽风啦、硬件罢工啦、软件闹脾气什么的,都可能是罪魁祸首。这篇文章将会深入探讨这些问题,并提供一些解决方案。 二、原因分析 1. 系统错误 这是最常见的一种原因。例如,操作系统可能出现了问题,或者是Oracle服务没有正确启动。此外,还可能是由于网络问题或其他外部因素导致的系统错误。 2. 硬件故障 硬件故障也可能导致数据库无法备份或恢复。例如,硬盘驱动器可能出现故障,导致数据丢失。另外,别忘了服务器上的其他硬件部件也有可能闹脾气,比如电源供应器啦、内存条什么的,都可能时不时出个小差错。 3. 软件问题 软件问题是另一种常见的原因。比如,数据库可能被病毒给“袭击”了,或者是因为装了个不合适的软件包,引发了系统内部的“矛盾斗争”。此外,软件版本过旧也可能导致数据库无法备份或恢复。 三、解决方案 针对以上原因,我们可以采取以下几种解决方案: 1. 检查系统错误 首先,我们需要检查系统的各个组件是否正常运行。例如,我们可以使用Oracle的服务控制台来检查Oracle服务的状态。如果发现有问题,我们可以尝试重新启动服务。此外,我们还需要检查操作系统是否存在错误。比如说,我们完全可以翻翻操作系统的日记本——日志文件,瞧瞧有没有冒出什么错误提示消息来。 2. 检查硬件故障 如果硬件设备存在问题,我们需要及时更换设备。例如,如果硬盘驱动器出现问题,我们可以更换一个新的硬盘驱动器。另外,我们还要时不时地给服务器上的其他硬件设备做个全面体检,确保它们都运转得倍儿棒。 3. 检查软件问题 对于软件问题,我们需要首先找出问题的原因。比如说,如果这是那个讨厌的病毒感染惹的祸,那咱们就得祭出反病毒软件,给电脑做个全身扫描,然后把那些捣乱的病毒一扫而光。如果是由于软件版本过旧导致的,我们需要更新软件版本。另外,我们还有一种方法可以尝试一下,那就是用Oracle的数据恢复神器来找回那些丢失的信息。 四、结论 总的来说,数据库无法备份或恢复是一个比较严重的问题,可能会导致数据丢失和其他一系列问题。因此,我们需要及时采取措施来解决问题。在解决这个问题的过程中,咱们得像个老朋友一样,深入地去了解数据库这家伙的各种脾性和能耐,还有怎么才能把它使唤得溜溜的。同时,我们也需要注意保持数据库的安全性,防止数据泄露和破坏。通过不断地学习和实践,我们可以成为一名优秀的数据库管理员。
2023-09-16 08:12:28
93
春暖花开-t
转载文章
...tity技术,描述了如何运用ASP.NET Identity实现应用程序的用户管理,以及实现应用程序的认证与授权等相关技术,译者希望本系列教程能成为掌握ASP.NET Identity技术的一份完整而有价值的资料。读者若是能够按照文章的描述,一边阅读、一边实践、一边理解,定能有意想不到的巨大收获!希望本系列博文能够得到广大园友的高度推荐。 15 Advanced ASP.NET Identity 15 ASP.NET Identity高级技术 In this chapter, I finish my description of ASP.NET Identity by showing you some of the advanced features it offers. I demonstrate how you can extend the database schema by defining custom properties on the user class and how to use database migrations to apply those properties without deleting the data in the ASP.NET Identity database. I also explain how ASP.NET Identity supports the concept of claims and demonstrates how they can be used to flexibly authorize access to action methods. I finish the chapter—and the book—by showing you how ASP.NET Identity makes it easy to authenticate users through third parties. I demonstrate authentication with Google accounts, but ASP.NET Identity has built-in support for Microsoft, Facebook, and Twitter accounts as well. Table 15-1 summarizes this chapter. 本章将完成对ASP.NET Identity的描述,向你展示它所提供的一些高级特性。我将演示,你可以扩展ASP.NET Identity的数据库架构,其办法是在用户类上定义一些自定义属性。也会演示如何使用数据库迁移,这样可以运用自定义属性,而不必删除ASP.NET Identity数据库中的数据。还会解释ASP.NET Identity如何支持声明(Claims)概念,并演示如何将它们灵活地用来对动作方法进行授权访问。最后向你展示ASP.NET Identity很容易通过第三方部件来认证用户,以此结束本章以及本书。将要演示的是使用Google账号认证,但ASP.NET Identity对于Microsoft、Facebook以及Twitter账号,都有内建的支持。表15-1是本章概要。 Table 15-1. Chapter Summary 表15-1. 本章概要 Problem 问题 Solution 解决方案 Listing 清单号 Store additional information about users. 存储用户的附加信息 Define custom user properties. 定义自定义用户属性 1–3, 8–11 Update the database schema without deleting user data. 更新数据库架构而不删除用户数据 Perform a database migration. 执行数据库迁移 4–7 Perform fine-grained authorization. 执行细粒度授权 Use claims. 使用声明(Claims) 12–14 Add claims about a user. 添加用户的声明(Claims) Use the ClaimsIdentity.AddClaims method. 使用ClaimsIdentity.AddClaims方法 15–19 Authorize access based on claim values. 基于声明(Claims)值授权访问 Create a custom authorization filter attribute. 创建一个自定义的授权过滤器注解属性 20–21 Authenticate through a third party. 通过第三方认证 Install the NuGet package for the authentication provider, redirect requests to that provider, and specify a callback URL that creates the user account. 安装认证提供器的NuGet包,将请求重定向到该提供器,并指定一个创建用户账号的回调URL。 22–25 15.1 Preparing the Example Project 15.1 准备示例项目 In this chapter, I am going to continue working on the Users project I created in Chapter 13 and enhanced in Chapter 14. No changes to the application are required, but start the application and make sure that there are users in the database. Figure 15-1 shows the state of my database, which contains the users Admin, Alice, Bob, and Joe from the previous chapter. To check the users, start the application and request the /Admin/Index URL and authenticate as the Admin user. 本章打算继续使用第13章创建并在第14章增强的Users项目。对应用程序无需做什么改变,但需要启动应用程序,并确保数据库中有一些用户。图15-1显示了数据库的状态,它含有上一章的用户Admin、Alice、Bob以及Joe。为了检查用户,请启动应用程序,请求/Admin/Index URL,并以Admin用户进行认证。 Figure 15-1. The initial users in the Identity database 图15-1. Identity数据库中的最初用户 I also need some roles for this chapter. I used the RoleAdmin controller to create roles called Users and Employees and assigned the users to those roles, as described in Table 15-2. 本章还需要一些角色。我用RoleAdmin控制器创建了角色Users和Employees,并为这些角色指定了一些用户,如表15-2所示。 Table 15-2. The Types of Web Forms Code Nuggets 表15-2. 角色及成员(作者将此表的标题写错了——译者注) Role 角色 Members 成员 Users Alice, Joe Employees Alice, Bob Figure 15-2 shows the required role configuration displayed by the RoleAdmin controller. 图15-2显示了由RoleAdmin控制器所显示出来的必要的角色配置。 Figure 15-2. Configuring the roles required for this chapter 图15-2. 配置本章所需的角色 15.2 Adding Custom User Properties 15.2 添加自定义用户属性 When I created the AppUser class to represent users in Chapter 13, I noted that the base class defined a basic set of properties to describe the user, such as e-mail address and telephone number. Most applications need to store more information about users, including persistent application preferences and details such as addresses—in short, any data that is useful to running the application and that should last between sessions. In ASP.NET Membership, this was handled through the user profile system, but ASP.NET Identity takes a different approach. 我在第13章创建AppUser类来表示用户时曾做过说明,基类定义了一组描述用户的基本属性,如E-mail地址、电话号码等。大多数应用程序还需要存储用户的更多信息,包括持久化应用程序爱好以及地址等细节——简言之,需要存储对运行应用程序有用并且在各次会话之间应当保持的任何数据。在ASP.NET Membership中,这是通过用户资料(User Profile)系统来处理的,但ASP.NET Identity采取了一种不同的办法。 Because the ASP.NET Identity system uses Entity Framework to store its data by default, defining additional user information is just a matter of adding properties to the user class and letting the Code First feature create the database schema required to store them. Table 15-3 puts custom user properties in context. 因为ASP.NET Identity默认是使用Entity Framework来存储其数据的,定义附加的用户信息只不过是给用户类添加属性的事情,然后让Code First特性去创建需要存储它们的数据库架构即可。表15-3描述了自定义用户属性的情形。 Table 15-3. Putting Cusotm User Properties in Context 表15-3. 自定义用户属性的情形 Question 问题 Answer 回答 What is it? 什么是自定义用户属性? Custom user properties allow you to store additional information about your users, including their preferences and settings. 自定义用户属性让你能够存储附加的用户信息,包括他们的爱好和设置。 Why should I care? 为何要关心它? A persistent store of settings means that the user doesn’t have to provide the same information each time they log in to the application. 设置的持久化存储意味着,用户不必每次登录到应用程序时都提供同样的信息。 How is it used by the MVC framework? 在MVC框架中如何使用它? This feature isn’t used directly by the MVC framework, but it is available for use in action methods. 此特性不是由MVC框架直接使用的,但它在动作方法中使用是有效的。 15.2.1 Defining Custom Properties 15.2.1 定义自定义属性 Listing 15-1 shows how I added a simple property to the AppUser class to represent the city in which the user lives. 清单15-1演示了如何给AppUser类添加一个简单的属性,用以表示用户生活的城市。 Listing 15-1. Adding a Property in the AppUser.cs File 清单15-1. 在AppUser.cs文件中添加属性 using System;using Microsoft.AspNet.Identity.EntityFramework;namespace Users.Models { public enum Cities {LONDON, PARIS, CHICAGO}public class AppUser : IdentityUser {public Cities City { get; set; } }} I have defined an enumeration called Cities that defines values for some large cities and added a property called City to the AppUser class. To allow the user to view and edit their City property, I added actions to the Home controller, as shown in Listing 15-2. 这里定义了一个枚举,名称为Cities,它定义了一些大城市的值,另外给AppUser类添加了一个名称为City的属性。为了让用户能够查看和编辑City属性,给Home控制器添加了几个动作方法,如清单15-2所示。 Listing 15-2. Adding Support for Custom User Properties in the HomeController.cs File 清单15-2. 在HomeController.cs文件中添加对自定义属性的支持 using System.Web.Mvc;using System.Collections.Generic;using System.Web;using System.Security.Principal;using System.Threading.Tasks;using Users.Infrastructure;using Microsoft.AspNet.Identity;using Microsoft.AspNet.Identity.Owin;using Users.Models;namespace Users.Controllers {public class HomeController : Controller {[Authorize]public ActionResult Index() {return View(GetData("Index"));}[Authorize(Roles = "Users")]public ActionResult OtherAction() {return View("Index", GetData("OtherAction"));}private Dictionary<string, object> GetData(string actionName) {Dictionary<string, object> dict= new Dictionary<string, object>();dict.Add("Action", actionName);dict.Add("User", HttpContext.User.Identity.Name);dict.Add("Authenticated", HttpContext.User.Identity.IsAuthenticated);dict.Add("Auth Type", HttpContext.User.Identity.AuthenticationType);dict.Add("In Users Role", HttpContext.User.IsInRole("Users"));return dict;} [Authorize]public ActionResult UserProps() {return View(CurrentUser);}[Authorize][HttpPost]public async Task<ActionResult> UserProps(Cities city) {AppUser user = CurrentUser;user.City = city;await UserManager.UpdateAsync(user);return View(user);}private AppUser CurrentUser {get {return UserManager.FindByName(HttpContext.User.Identity.Name);} }private AppUserManager UserManager {get {return HttpContext.GetOwinContext().GetUserManager<AppUserManager>();} }} } I added a CurrentUser property that uses the AppUserManager class to retrieve an AppUser instance to represent the current user. I pass the AppUser object as the view model object in the GET version of the UserProps action method, and the POST method uses it to update the value of the new City property. Listing 15-3 shows the UserProps.cshtml view, which displays the City property value and contains a form to change it. 我添加了一个CurrentUser属性,它使用AppUserManager类接收了表示当前用户的AppUser实例。在GET版本的UserProps动作方法中,传递了这个AppUser对象作为视图模型。而在POST版的方法中用它更新了City属性的值。清单15-3显示了UserProps.cshtml视图,它显示了City属性的值,并包含一个修改它的表单。 Listing 15-3. The Contents of the UserProps.cshtml File in the Views/Home Folder 清单15-3. Views/Home文件夹中UserProps.cshtml文件的内容 @using Users.Models@model AppUser@{ ViewBag.Title = "UserProps";}<div class="panel panel-primary"><div class="panel-heading">Custom User Properties</div><table class="table table-striped"><tr><th>City</th><td>@Model.City</td></tr></table></div> @using (Html.BeginForm()) {<div class="form-group"><label>City</label>@Html.DropDownListFor(x => x.City, new SelectList(Enum.GetNames(typeof(Cities))))</div><button class="btn btn-primary" type="submit">Save</button>} Caution Don’t start the application when you have created the view. In the sections that follow, I demonstrate how to preserve the contents of the database, and if you start the application now, the ASP.NET Identity users will be deleted. 警告:创建了视图之后不要启动应用程序。在以下小节中,将演示如何保留数据库的内容,如果现在启动应用程序,将会删除ASP.NET Identity的用户。 15.2.2 Preparing for Database Migration 15.2.2 准备数据库迁移 The default behavior for the Entity Framework Code First feature is to drop the tables in the database and re-create them whenever classes that drive the schema have changed. You saw this in Chapter 14 when I added support for roles: When the application was started, the database was reset, and the user accounts were lost. Entity Framework Code First特性的默认行为是,一旦修改了派生数据库架构的类,便会删除数据库中的数据表,并重新创建它们。在第14章可以看到这种情况,在我添加角色支持时:当重启应用程序后,数据库被重置,用户账号也丢失。 Don’t start the application yet, but if you were to do so, you would see a similar effect. Deleting data during development is usually not a problem, but doing so in a production setting is usually disastrous because it deletes all of the real user accounts and causes a panic while the backups are restored. In this section, I am going to demonstrate how to use the database migration feature, which updates a Code First schema in a less brutal manner and preserves the existing data it contains. 不要启动应用程序,但如果你这么做了,会看到类似的效果。在开发期间删除数据没什么问题,但如果在产品设置中这么做了,通常是灾难性的,因为它会删除所有真实的用户账号,而备份恢复是很痛苦的事。在本小节中,我打算演示如何使用数据库迁移特性,它能以比较温和的方式更新Code First的架构,并保留架构中的已有数据。 The first step is to issue the following command in the Visual Studio Package Manager Console: 第一个步骤是在Visual Studio的“Package Manager Console(包管理器控制台)”中发布以下命令: Enable-Migrations –EnableAutomaticMigrations This enables the database migration support and creates a Migrations folder in the Solution Explorer that contains a Configuration.cs class file, the contents of which are shown in Listing 15-4. 它启用了数据库的迁移支持,并在“Solution Explorer(解决方案资源管理器)”创建一个Migrations文件夹,其中含有一个Configuration.cs类文件,内容如清单15-4所示。 Listing 15-4. The Contents of the Configuration.cs File 清单15-4. Configuration.cs文件的内容 namespace Users.Migrations {using System;using System.Data.Entity;using System.Data.Entity.Migrations;using System.Linq;internal sealed class Configuration: DbMigrationsConfiguration<Users.Infrastructure.AppIdentityDbContext> {public Configuration() {AutomaticMigrationsEnabled = true;ContextKey = "Users.Infrastructure.AppIdentityDbContext";}protected override void Seed(Users.Infrastructure.AppIdentityDbContext context) {// This method will be called after migrating to the latest version.// 此方法将在迁移到最新版本时调用// You can use the DbSet<T>.AddOrUpdate() helper extension method// to avoid creating duplicate seed data. E.g.// 例如,你可以使用DbSet<T>.AddOrUpdate()辅助器方法来避免创建重复的种子数据//// context.People.AddOrUpdate(// p => p.FullName,// new Person { FullName = "Andrew Peters" },// new Person { FullName = "Brice Lambson" },// new Person { FullName = "Rowan Miller" }// );//} }} Tip You might be wondering why you are entering a database migration command into the console used to manage NuGet packages. The answer is that the Package Manager Console is really PowerShell, which is a general-purpose tool that is mislabeled by Visual Studio. You can use the console to issue a wide range of helpful commands. See http://go.microsoft.com/fwlink/?LinkID=108518 for details. 提示:你可能会觉得奇怪,为什么要在管理NuGet包的控制台中输入数据库迁移的命令?答案是“Package Manager Console(包管理控制台)”是真正的PowerShell,这是Visual studio冒用的一个通用工具。你可以使用此控制台发送大量的有用命令,详见http://go.microsoft.com/fwlink/?LinkID=108518。 The class will be used to migrate existing content in the database to the new schema, and the Seed method will be called to provide an opportunity to update the existing database records. In Listing 15-5, you can see how I have used the Seed method to set a default value for the new City property I added to the AppUser class. (I have also updated the class file to reflect my usual coding style.) 这个类将用于把数据库中的现有内容迁移到新的数据库架构,Seed方法的调用为更新现有数据库记录提供了机会。在清单15-5中可以看到,我如何用Seed方法为新的City属性设置默认值,City是添加到AppUser类中自定义属性。(为了体现我一贯的编码风格,我对这个类文件也进行了更新。) Listing 15-5. Managing Existing Content in the Configuration.cs File 清单15-5. 在Configuration.cs文件中管理已有内容 using System.Data.Entity.Migrations;using Microsoft.AspNet.Identity;using Microsoft.AspNet.Identity.EntityFramework;using Users.Infrastructure;using Users.Models;namespace Users.Migrations {internal sealed class Configuration: DbMigrationsConfiguration<AppIdentityDbContext> {public Configuration() {AutomaticMigrationsEnabled = true;ContextKey = "Users.Infrastructure.AppIdentityDbContext";}protected override void Seed(AppIdentityDbContext context) {AppUserManager userMgr = new AppUserManager(new UserStore<AppUser>(context));AppRoleManager roleMgr = new AppRoleManager(new RoleStore<AppRole>(context)); string roleName = "Administrators";string userName = "Admin";string password = "MySecret";string email = "admin@example.com";if (!roleMgr.RoleExists(roleName)) {roleMgr.Create(new AppRole(roleName));}AppUser user = userMgr.FindByName(userName);if (user == null) {userMgr.Create(new AppUser { UserName = userName, Email = email },password);user = userMgr.FindByName(userName);}if (!userMgr.IsInRole(user.Id, roleName)) {userMgr.AddToRole(user.Id, roleName);}foreach (AppUser dbUser in userMgr.Users) {dbUser.City = Cities.PARIS;}context.SaveChanges();} }} You will notice that much of the code that I added to the Seed method is taken from the IdentityDbInit class, which I used to seed the database with an administration user in Chapter 14. This is because the new Configuration class added to support database migrations will replace the seeding function of the IdentityDbInit class, which I’ll update shortly. Aside from ensuring that there is an admin user, the statements in the Seed method that are important are the ones that set the initial value for the City property I added to the AppUser class, as follows: 你可能会注意到,添加到Seed方法中的许多代码取自于IdentityDbInit类,在第14章中我用这个类将管理用户植入了数据库。这是因为这个新添加的、用以支持数据库迁移的Configuration类,将代替IdentityDbInit类的种植功能,我很快便会更新这个类。除了要确保有admin用户之外,在Seed方法中的重要语句是那些为AppUser类的City属性设置初值的语句,如下所示: ...foreach (AppUser dbUser in userMgr.Users) { dbUser.City = Cities.PARIS;}context.SaveChanges();... You don’t have to set a default value for new properties—I just wanted to demonstrate that the Seed method in the Configuration class can be used to update the existing user records in the database. 你不一定要为新属性设置默认值——这里只是想演示Configuration类中的Seed方法,可以用它更新数据库中的已有用户记录。 Caution Be careful when setting values for properties in the Seed method for real projects because the values will be applied every time you change the schema, overriding any values that the user has set since the last schema update was performed. I set the value of the City property just to demonstrate that it can be done. 警告:在用于真实项目的Seed方法中为属性设置值时要小心,因为你每一次修改架构时,都会运用这些值,这会将自执行上一次架构更新之后,用户设置的任何数据覆盖掉。这里设置City属性的值只是为了演示它能够这么做。 Changing the Database Context Class 修改数据库上下文类 The reason that I added the seeding code to the Configuration class is that I need to change the IdentityDbInit class. At present, the IdentityDbInit class is derived from the descriptively named DropCreateDatabaseIfModelChanges<AppIdentityDbContext> class, which, as you might imagine, drops the entire database when the Code First classes change. Listing 15-6 shows the changes I made to the IdentityDbInit class to prevent it from affecting the database. 在Configuration类中添加种植代码的原因是我需要修改IdentityDbInit类。此时,IdentityDbInit类派生于描述性命名的DropCreateDatabaseIfModelChanges<AppIdentityDbContext> 类,和你相像的一样,它会在Code First类改变时删除整个数据库。清单15-6显示了我对IdentityDbInit类所做的修改,以防止它影响数据库。 Listing 15-6. Preventing Database Schema Changes in the AppIdentityDbContext.cs File 清单15-6. 在AppIdentityDbContext.cs文件是阻止数据库架构变化 using System.Data.Entity;using Microsoft.AspNet.Identity.EntityFramework;using Users.Models;using Microsoft.AspNet.Identity; namespace Users.Infrastructure {public class AppIdentityDbContext : IdentityDbContext<AppUser> {public AppIdentityDbContext() : base("IdentityDb") { }static AppIdentityDbContext() {Database.SetInitializer<AppIdentityDbContext>(new IdentityDbInit());}public static AppIdentityDbContext Create() {return new AppIdentityDbContext();} } public class IdentityDbInit : NullDatabaseInitializer<AppIdentityDbContext> {} } I have removed the methods defined by the class and changed its base to NullDatabaseInitializer<AppIdentityDbContext> , which prevents the schema from being altered. 我删除了这个类中所定义的方法,并将它的基类改为NullDatabaseInitializer<AppIdentityDbContext> ,它可以防止架构修改。 15.2.3 Performing the Migration 15.2.3 执行迁移 All that remains is to generate and apply the migration. First, run the following command in the Package Manager Console: 剩下的事情只是生成并运用迁移了。首先,在“Package Manager Console(包管理器控制台)”中执行以下命令: Add-Migration CityProperty This creates a new migration called CityProperty (I like my migration names to reflect the changes I made). A class new file will be added to the Migrations folder, and its name reflects the time at which the command was run and the name of the migration. My file is called 201402262244036_CityProperty.cs, for example. The contents of this file contain the details of how Entity Framework will change the database during the migration, as shown in Listing 15-7. 这创建了一个名称为CityProperty的新迁移(我比较喜欢让迁移的名称反映出我所做的修改)。这会在文件夹中添加一个新的类文件,而且其命名会反映出该命令执行的时间以及迁移名称,例如,我的这个文件名称为201402262244036_CityProperty.cs。该文件的内容含有迁移期间Entity Framework修改数据库的细节,如清单15-7所示。 Listing 15-7. The Contents of the 201402262244036_CityProperty.cs File 清单15-7. 201402262244036_CityProperty.cs文件的内容 namespace Users.Migrations {using System;using System.Data.Entity.Migrations; public partial class Init : DbMigration {public override void Up() {AddColumn("dbo.AspNetUsers", "City", c => c.Int(nullable: false));}public override void Down() {DropColumn("dbo.AspNetUsers", "City");} }} The Up method describes the changes that have to be made to the schema when the database is upgraded, which in this case means adding a City column to the AspNetUsers table, which is the one that is used to store user records in the ASP.NET Identity database. Up方法描述了在数据库升级时,需要对架构所做的修改,在这个例子中,意味着要在AspNetUsers数据表中添加City数据列,该数据表是ASP.NET Identity数据库用来存储用户记录的。 The final step is to perform the migration. Without starting the application, run the following command in the Package Manager Console: 最后一步是执行迁移。无需启动应用程序,只需在“Package Manager Console(包管理器控制台)”中运行以下命令即可: Update-Database –TargetMigration CityProperty The database schema will be modified, and the code in the Configuration.Seed method will be executed. The existing user accounts will have been preserved and enhanced with a City property (which I set to Paris in the Seed method). 这会修改数据库架构,并执行Configuration.Seed方法中的代码。已有用户账号会被保留,且增强了City属性(我在Seed方法中已将其设置为“Paris”)。 15.2.4 Testing the Migration 15.2.4 测试迁移 To test the effect of the migration, start the application, navigate to the /Home/UserProps URL, and authenticate as one of the Identity users (for example, as Alice with the password MySecret). Once authenticated, you will see the current value of the City property for the user and have the opportunity to change it, as shown in Figure 15-3. 为了测试迁移的效果,启动应用程序,导航到/Home/UserProps URL,并以Identity中的用户(例如Alice,口令MySecret)进行认证。一旦已被认证,便会看到该用户City属性的当前值,并可以对其进行修改,如图15-3所示。 Figure 15-3. Displaying and changing a custom user property 图15-3. 显示和个性自定义用户属性 15.2.5 Defining an Additional Property 15.2.5 定义附加属性 Now that database migrations are set up, I am going to define a further property just to demonstrate how subsequent changes are handled and to show a more useful (and less dangerous) example of using the Configuration.Seed method. Listing 15-8 shows how I added a Country property to the AppUser class. 现在,已经建立了数据库迁移,我打算再定义一个属性,这恰恰演示了如何处理持续不断的修改,也为了演示Configuration.Seed方法更有用(至少无害)的示例。清单15-8显示了我在AppUser类上添加了一个Country属性。 Listing 15-8. Adding Another Property in the AppUserModels.cs File 清单15-8. 在AppUserModels.cs文件中添加另一个属性 using System;using Microsoft.AspNet.Identity.EntityFramework; namespace Users.Models {public enum Cities {LONDON, PARIS, CHICAGO} public enum Countries {NONE, UK, FRANCE, USA}public class AppUser : IdentityUser {public Cities City { get; set; }public Countries Country { get; set; }public void SetCountryFromCity(Cities city) {switch (city) {case Cities.LONDON:Country = Countries.UK;break;case Cities.PARIS:Country = Countries.FRANCE;break;case Cities.CHICAGO:Country = Countries.USA;break;default:Country = Countries.NONE;break;} }} } I have added an enumeration to define the country names and a helper method that selects a country value based on the City property. Listing 15-9 shows the change I made to the Configuration class so that the Seed method sets the Country property based on the City, but only if the value of Country is NONE (which it will be for all users when the database is migrated because the Entity Framework sets enumeration columns to the first value). 我已经添加了一个枚举,它定义了国家名称。还添加了一个辅助器方法,它可以根据City属性选择一个国家。清单15-9显示了对Configuration类所做的修改,以使Seed方法根据City设置Country属性,但只当Country为NONE时才进行设置(在迁移数据库时,所有用户都是NONE,因为Entity Framework会将枚举列设置为枚举的第一个值)。 Listing 15-9. Modifying the Database Seed in the Configuration.cs File 清单15-9. 在Configuration.cs文件中修改数据库种子 using System.Data.Entity.Migrations;using Microsoft.AspNet.Identity;using Microsoft.AspNet.Identity.EntityFramework;using Users.Infrastructure;using Users.Models; namespace Users.Migrations {internal sealed class Configuration: DbMigrationsConfiguration<AppIdentityDbContext> {public Configuration() {AutomaticMigrationsEnabled = true;ContextKey = "Users.Infrastructure.AppIdentityDbContext";}protected override void Seed(AppIdentityDbContext context) {AppUserManager userMgr = new AppUserManager(new UserStore<AppUser>(context));AppRoleManager roleMgr = new AppRoleManager(new RoleStore<AppRole>(context)); string roleName = "Administrators";string userName = "Admin";string password = "MySecret";string email = "admin@example.com";if (!roleMgr.RoleExists(roleName)) {roleMgr.Create(new AppRole(roleName));}AppUser user = userMgr.FindByName(userName);if (user == null) {userMgr.Create(new AppUser { UserName = userName, Email = email },password);user = userMgr.FindByName(userName);}if (!userMgr.IsInRole(user.Id, roleName)) {userMgr.AddToRole(user.Id, roleName);} foreach (AppUser dbUser in userMgr.Users) {if (dbUser.Country == Countries.NONE) {dbUser.SetCountryFromCity(dbUser.City);} }context.SaveChanges();} }} This kind of seeding is more useful in a real project because it will set a value for the Country property only if one has not already been set—subsequent migrations won’t be affected, and user selections won’t be lost. 这种种植在实际项目中会更有用,因为它只会在Country属性未设置时,才会设置Country属性的值——后继的迁移不会受到影响,因此不会失去用户的选择。 1. Adding Application Support 1. 添加应用程序支持 There is no point defining additional user properties if they are not available in the application, so Listing 15-10 shows the change I made to the Views/Home/UserProps.cshtml file to display the value of the Country property. 应用程序中如果没有定义附加属性的地方,则附加属性就无法使用了,因此,清单15-10显示了我对Views/Home/UserProps.cshtml文件的修改,以显示Country属性的值。 Listing 15-10. Displaying an Additional Property in the UserProps.cshtml File 清单15-10. 在UserProps.cshtml文件中显示附加属性 @using Users.Models@model AppUser@{ ViewBag.Title = "UserProps";} <div class="panel panel-primary"><div class="panel-heading">Custom User Properties</div><table class="table table-striped"><tr><th>City</th><td>@Model.City</td></tr> <tr><th>Country</th><td>@Model.Country</td></tr></table></div>@using (Html.BeginForm()) {<div class="form-group"><label>City</label>@Html.DropDownListFor(x => x.City, new SelectList(Enum.GetNames(typeof(Cities))))</div><button class="btn btn-primary" type="submit">Save</button>} Listing 15-11 shows the corresponding change I made to the Home controller to update the Country property when the City value changes. 为了在City值变化时能够更新Country属性,清单15-11显示了我对Home控制器所做的相应修改。 Listing 15-11. Setting Custom Properties in the HomeController.cs File 清单15-11. 在HomeController.cs文件中设置自定义属性 using System.Web.Mvc;using System.Collections.Generic;using System.Web;using System.Security.Principal;using System.Threading.Tasks;using Users.Infrastructure;using Microsoft.AspNet.Identity;using Microsoft.AspNet.Identity.Owin;using Users.Models; namespace Users.Controllers {public class HomeController : Controller {// ...other action methods omitted for brevity...// ...出于简化,这里忽略了其他动作方法... [Authorize]public ActionResult UserProps() {return View(CurrentUser);}[Authorize][HttpPost]public async Task<ActionResult> UserProps(Cities city) {AppUser user = CurrentUser;user.City = city;user.SetCountryFromCity(city);await UserManager.UpdateAsync(user);return View(user);}// ...properties omitted for brevity...// ...出于简化,这里忽略了一些属性...} } 2. Performing the Migration 2. 准备迁移 All that remains is to create and apply a new migration. Enter the following command into the Package Manager Console: 剩下的事情就是创建和运用新的迁移了。在“Package Manager Console(包管理器控制台)”中输入以下命令: Add-Migration CountryProperty This will generate another file in the Migrations folder that contains the instruction to add the Country column. To apply the migration, execute the following command: 这将在Migrations文件夹中生成另一个文件,它含有添加Country数据表列的指令。为了运用迁移,可执行以下命令: Update-Database –TargetMigration CountryProperty The migration will be performed, and the value of the Country property will be set based on the value of the existing City property for each user. You can check the new user property by starting the application and authenticating and navigating to the /Home/UserProps URL, as shown in Figure 15-4. 这将执行迁移,Country属性的值将根据每个用户当前的City属性进行设置。通过启动应用程序,认证并导航到/Home/UserProps URL,便可以查看新的用户属性,如图15-4所示。 Figure 15-4. Creating an additional user property 图15-4. 创建附加用户属性 Tip Although I am focused on the process of upgrading the database, you can also migrate back to a previous version by specifying an earlier migration. Use the –Force argument make changes that cause data loss, such as removing a column. 提示:虽然我们关注了升级数据库的过程,但你也可以回退到以前的版本,只需指定一个早期的迁移即可。使用-Force参数进行修改,会引起数据丢失,例如删除数据表列。 15.3 Working with Claims 15.3 使用声明(Claims) In older user-management systems, such as ASP.NET Membership, the application was assumed to be the authoritative source of all information about the user, essentially treating the application as a closed world and trusting the data that is contained within it. 在旧的用户管理系统中,例如ASP.NET Membership,应用程序被假设成是用户所有信息的权威来源,本质上将应用程序视为是一个封闭的世界,并且只信任其中所包含的数据。 This is such an ingrained approach to software development that it can be hard to recognize that’s what is happening, but you saw an example of the closed-world technique in Chapter 14 when I authenticated users against the credentials stored in the database and granted access based on the roles associated with those credentials. I did the same thing again in this chapter when I added properties to the user class. Every piece of information that I needed to manage user authentication and authorization came from within my application—and that is a perfectly satisfactory approach for many web applications, which is why I demonstrated these techniques in such depth. 这是软件开发的一种根深蒂固的方法,使人很难认识到这到底意味着什么,第14章你已看到了这种封闭世界技术的例子,根据存储在数据库中的凭据来认证用户,并根据与凭据关联在一起的角色来授权访问。本章前述在用户类上添加属性,也做了同样的事情。我管理用户认证与授权所需的每一个数据片段都来自于我的应用程序——而且这是许多Web应用程序都相当满意的一种方法,这也是我如此深入地演示这些技术的原因。 ASP.NET Identity also supports an alternative approach for dealing with users, which works well when the MVC framework application isn’t the sole source of information about users and which can be used to authorize users in more flexible and fluid ways than traditional roles allow. ASP.NET Identity还支持另一种处理用户的办法,当MVC框架的应用程序不是有关用户的唯一信息源时,这种办法会工作得很好,而且能够比传统的角色授权更为灵活且流畅的方式进行授权。 This alternative approach uses claims, and in this section I’ll describe how ASP.NET Identity supports claims-based authorization. Table 15-4 puts claims in context. 这种可选的办法使用了“Claims(声明)”,因此在本小节中,我将描述ASP.NET Identity如何支持“Claims-Based Authorization(基于声明的授权)”。表15-4描述了声明(Claims)的情形。 提示:“Claim”在英文字典中不完全是“声明”的意思,根据本文的描述,感觉把它说成“声明”也不一定合适,所以在之后的译文中基本都写成中英文并用的形式,即“声明(Claims)”。根据表15-4中的声明(Claims)的定义:声明(Claims)是关于用户的一些信息片段。一个用户的信息片段当然有很多,每一个信息片段就是一项声明(Claim),用户的所有信息片段合起来就是该用户的声明(Claims)。请读者注意该单词的单复数形式——译者注 Table 15-4. Putting Claims in Context 表15-4. 声明(Claims)的情形 Question 问题 Answer 答案 What is it? 什么是声明(Claims)? Claims are pieces of information about users that you can use to make authorization decisions. Claims can be obtained from external systems as well as from the local Identity database. 声明(Claims)是关于用户的一些信息片段,可以用它们做出授权决定。声明(Claims)可以从外部系统获取,也可以从本地的Identity数据库获取。 Why should I care? 为何要关心它? Claims can be used to flexibly authorize access to action methods. Unlike conventional roles, claims allow access to be driven by the information that describes the user. 声明(Claims)可以用来对动作方法进行灵活的授权访问。与传统的角色不同,声明(Claims)让访问能够由描述用户的信息进行驱动。 How is it used by the MVC framework? 如何在MVC框架中使用它? This feature isn’t used directly by the MVC framework, but it is integrated into the standard authorization features, such as the Authorize attribute. 这不是直接由MVC框架使用的特性,但它集成到了标准的授权特性之中,例如Authorize注解属性。 Tip you don’t have to use claims in your applications, and as Chapter 14 showed, ASP.NET Identity is perfectly happy providing an application with the authentication and authorization services without any need to understand claims at all. 提示:你在应用程序中不一定要使用声明(Claims),正如第14章所展示的那样,ASP.NET Identity能够为应用程序提供充分的认证与授权服务,而根本不需要理解声明(Claims)。 15.3.1 Understanding Claims 15.3.1 理解声明(Claims) A claim is a piece of information about the user, along with some information about where the information came from. The easiest way to unpack claims is through some practical demonstrations, without which any discussion becomes too abstract to be truly useful. To get started, I added a Claims controller to the example project, the definition of which you can see in Listing 15-12. 一项声明(Claim)是关于用户的一个信息片段(请注意这个英文单词的单复数形式——译者注),并伴有该片段出自何处的某种信息。揭开声明(Claims)含义最容易的方式是做一些实际演示,任何讨论都会过于抽象根本没有真正的用处。为此,我在示例项目中添加了一个Claims控制器,其定义如清单15-12所示。 Listing 15-12. The Contents of the ClaimsController.cs File 清单15-12. ClaimsController.cs文件的内容 using System.Security.Claims;using System.Web;using System.Web.Mvc; namespace Users.Controllers {public class ClaimsController : Controller {[Authorize]public ActionResult Index() {ClaimsIdentity ident = HttpContext.User.Identity as ClaimsIdentity;if (ident == null) {return View("Error", new string[] { "No claims available" });} else {return View(ident.Claims);} }} } Tip You may feel a little lost as I define the code for this example. Don’t worry about the details for the moment—just stick with it until you see the output from the action method and view that I define. More than anything else, that will help put claims into perspective. 提示:你或许会对我为此例定义的代码感到有点失望。此刻对此细节不必着急——只要稍事忍耐,当看到该动作方法和视图的输出便会明白。尤为重要的是,这有助于洞察声明(Claims)。 You can get the claims associated with a user in different ways. One approach is to use the Claims property defined by the user class, but in this example, I have used the HttpContext.User.Identity property to demonstrate the way that ASP.NET Identity is integrated with the rest of the ASP.NET platform. As I explained in Chapter 13, the HttpContext.User.Identity property returns an implementation of the IIdentity interface, which is a ClaimsIdentity object when working using ASP.NET Identity. The ClaimsIdentity class is defined in the System.Security.Claims namespace, and Table 15-5 shows the members it defines that are relevant to this chapter. 可以通过不同的方式获得与用户相关联的声明(Claims)。方法之一就是使用由用户类定义的Claims属性,但在这个例子中,我使用了HttpContext.User.Identity属性,目的是演示ASP.NET Identity与ASP.NET平台集成的方式(请注意这句话所表示的含义:用户类的Claims属性属于ASP.NET Identity,而HttpContext.User.Identity属性则属于ASP.NET平台。由此可见,ASP.NET Identity已经融合到了ASP.NET平台之中——译者注)。正如第13章所解释的那样,HttpContext.User.Identity属性返回IIdentity的接口实现,当使用ASP.NET Identity时,该实现是一个ClaimsIdentity对象。ClaimsIdentity类是在System.Security.Claims命名空间中定义的,表15-5显示了它所定义的与本章有关的成员。 Table 15-5. The Members Defined by the ClaimsIdentity Class 表15-5. ClaimsIdentity类所定义的成员 Name 名称 Description 描述 Claims Returns an enumeration of Claim objects representing the claims for the user. 返回表示用户声明(Claims)的Claim对象枚举 AddClaim(claim) Adds a claim to the user identity. 给用户添加一个声明(Claim) AddClaims(claims) Adds an enumeration of Claim objects to the user identity. 给用户添加Claim对象的枚举。 HasClaim(predicate) Returns true if the user identity contains a claim that matches the specified predicate. See the “Applying Claims” section for an example predicate. 如果用户含有与指定谓词匹配的声明(Claim)时,返回true。参见“运用声明(Claims)”中的示例谓词 RemoveClaim(claim) Removes a claim from the user identity. 删除用户的声明(Claim)。 Other members are available, but the ones in the table are those that are used most often in web applications, for reason that will become obvious as I demonstrate how claims fit into the wider ASP.NET platform. 还有一些可用的其它成员,但表中的这些是在Web应用程序中最常用的,随着我演示如何将声明(Claims)融入更宽泛的ASP.NET平台,它们为什么最常用就很显然了。 In Listing 15-12, I cast the IIdentity implementation to the ClaimsIdentity type and pass the enumeration of Claim objects returned by the ClaimsIdentity.Claims property to the View method. A Claim object represents a single piece of data about the user, and the Claim class defines the properties shown in Table 15-6. 在清单15-12中,我将IIdentity实现转换成了ClaimsIdentity类型,并且给View方法传递了ClaimsIdentity.Claims属性所返回的Claim对象的枚举。Claim对象所示表示的是关于用户的一个单一的数据片段,Claim类定义的属性如表15-6所示。 Table 15-6. The Properties Defined by the Claim Class 表15-6. Claim类定义的属性 Name 名称 Description 描述 Issuer Returns the name of the system that provided the claim 返回提供声明(Claim)的系统名称 Subject Returns the ClaimsIdentity object for the user who the claim refers to 返回声明(Claim)所指用户的ClaimsIdentity对象 Type Returns the type of information that the claim represents 返回声明(Claim)所表示的信息类型 Value Returns the piece of information that the claim represents 返回声明(Claim)所表示的信息片段 Listing 15-13 shows the contents of the Index.cshtml file that I created in the Views/Claims folder and that is rendered by the Index action of the Claims controller. The view adds a row to a table for each claim about the user. 清单15-13显示了我在Views/Claims文件夹中创建的Index.cshtml文件的内容,它由Claims控制器中的Index动作方法进行渲染。该视图为用户的每项声明(Claim)添加了一个表格行。 Listing 15-13. The Contents of the Index.cshtml File in the Views/Claims Folder 清单15-13. Views/Claims文件夹中Index.cshtml文件的内容 @using System.Security.Claims@using Users.Infrastructure@model IEnumerable<Claim>@{ ViewBag.Title = "Claims"; }<div class="panel panel-primary"><div class="panel-heading">Claims</div><table class="table table-striped"><tr><th>Subject</th><th>Issuer</th><th>Type</th><th>Value</th></tr>@foreach (Claim claim in Model.OrderBy(x => x.Type)) {<tr><td>@claim.Subject.Name</td><td>@claim.Issuer</td><td>@Html.ClaimType(claim.Type)</td><td>@claim.Value</td></tr>}</table></div> The value of the Claim.Type property is a URI for a Microsoft schema, which isn’t especially useful. The popular schemas are used as the values for fields in the System.Security.Claims.ClaimTypes class, so to make the output from the Index.cshtml view easier to read, I added an HTML helper to the IdentityHelpers.cs file, as shown in Listing 15-14. It is this helper that I use in the Index.cshtml file to format the value of the Claim.Type property. Claim.Type属性的值是一个微软模式(Microsoft Schema)的URI(统一资源标识符),这是特别有用的。System.Security.Claims.ClaimTypes类中字段的值使用的是流行模式(Popular Schema),因此为了使Index.cshtml视图的输出更易于阅读,我在IdentityHelpers.cs文件中添加了一个HTML辅助器,如清单15-14所示。Index.cshtml文件正是使用这个辅助器格式化了Claim.Type属性的值。 Listing 15-14. Adding a Helper to the IdentityHelpers.cs File 清单15-14. 在IdentityHelpers.cs文件中添加辅助器 using System.Web;using System.Web.Mvc;using Microsoft.AspNet.Identity.Owin;using System;using System.Linq;using System.Reflection;using System.Security.Claims;namespace Users.Infrastructure {public static class IdentityHelpers {public static MvcHtmlString GetUserName(this HtmlHelper html, string id) {AppUserManager mgr= HttpContext.Current.GetOwinContext().GetUserManager<AppUserManager>();return new MvcHtmlString(mgr.FindByIdAsync(id).Result.UserName);} public static MvcHtmlString ClaimType(this HtmlHelper html, string claimType) {FieldInfo[] fields = typeof(ClaimTypes).GetFields();foreach (FieldInfo field in fields) {if (field.GetValue(null).ToString() == claimType) {return new MvcHtmlString(field.Name);} }return new MvcHtmlString(string.Format("{0}",claimType.Split('/', '.').Last()));} }} Note The helper method isn’t at all efficient because it reflects on the fields of the ClaimType class for each claim that is displayed, but it is sufficient for my purposes in this chapter. You won’t often need to display the claim type in real applications. 注:该辅助器并非十分有效,因为它只是针对每个要显示的声明(Claim)映射出ClaimType类的字段,但对我要的目的已经足够了。在实际项目中不会经常需要显示声明(Claim)的类型。 To see why I have created a controller that uses claims without really explaining what they are, start the application, authenticate as the user Alice (with the password MySecret), and request the /Claims/Index URL. Figure 15-5 shows the content that is generated. 为了弄明白我为何要先创建一个使用声明(Claims)的控制器,而没有真正解释声明(Claims)是什么的原因,可以启动应用程序,以用户Alice进行认证(其口令是MySecret),并请求/Claims/Index URL。图15-5显示了生成的内容。 Figure 15-5. The output from the Index action of the Claims controller 图15-5. Claims控制器中Index动作的输出 It can be hard to make out the detail in the figure, so I have reproduced the content in Table 15-7. 这可能还难以认识到此图的细节,为此我在表15-7中重列了其内容。 Table 15-7. The Data Shown in Figure 15-5 表15-7. 图15-5中显示的数据 Subject(科目) Issuer(发行者) Type(类型) Value(值) Alice LOCAL AUTHORITY SecurityStamp Unique ID Alice LOCAL AUTHORITY IdentityProvider ASP.NET Identity Alice LOCAL AUTHORITY Role Employees Alice LOCAL AUTHORITY Role Users Alice LOCAL AUTHORITY Name Alice Alice LOCAL AUTHORITY NameIdentifier Alice’s user ID The table shows the most important aspect of claims, which is that I have already been using them when I implemented the traditional authentication and authorization features in Chapter 14. You can see that some of the claims relate to user identity (the Name claim is Alice, and the NameIdentifier claim is Alice’s unique user ID in my ASP.NET Identity database). 此表展示了声明(Claims)最重要的方面,这些是我在第14章中实现传统的认证和授权特性时,一直在使用的信息。可以看出,有些声明(Claims)与用户标识有关(Name声明是Alice,NameIdentifier声明是Alice在ASP.NET Identity数据库中的唯一用户ID号)。 Other claims show membership of roles—there are two Role claims in the table, reflecting the fact that Alice is assigned to both the Users and Employees roles. There is also a claim about how Alice has been authenticated: The IdentityProvider is set to ASP.NET Identity. 其他声明(Claims)显示了角色成员——表中有两个Role声明(Claim),体现出Alice被赋予了Users和Employees两个角色这一事实。还有一个是Alice已被认证的声明(Claim):IdentityProvider被设置到了ASP.NET Identity。 The difference when this information is expressed as a set of claims is that you can determine where the data came from. The Issuer property for all the claims shown in the table is set to LOCAL AUTHORITY, which indicates that the user’s identity has been established by the application. 当这种信息被表示成一组声明(Claims)时的差别是,你能够确定这些数据是从哪里来的。表中所显示的所有声明的Issuer属性(发布者)都被设置到了LOACL AUTHORITY(本地授权),这说明该用户的标识是由应用程序建立的。 So, now that you have seen some example claims, I can more easily describe what a claim is. A claim is any piece of information about a user that is available to the application, including the user’s identity and role memberships. And, as you have seen, the information I have been defining about my users in earlier chapters is automatically made available as claims by ASP.NET Identity. 因此,现在你已经看到了一些声明(Claims)示例,我可以更容易地描述声明(Claim)是什么了。一项声明(Claim)是可用于应用程序中的有关用户的一个信息片段,包括用户的标识以及角色成员等。而且,正如你所看到的,我在前几章定义的关于用户的信息,被ASP.NET Identity自动地作为声明(Claims)了。 15.3.2 Creating and Using Claims 15.3.2 创建和使用声明(Claims) Claims are interesting for two reasons. The first reason is that an application can obtain claims from multiple sources, rather than just relying on a local database for information about the user. You will see a real example of this when I show you how to authenticate users through a third-party system in the “Using Third-Party Authentication” section, but for the moment I am going to add a class to the example project that simulates a system that provides claims information. Listing 15-15 shows the contents of the LocationClaimsProvider.cs file that I added to the Infrastructure folder. 声明(Claims)比较有意思的原因有两个。第一个原因是应用程序可以从多个来源获取声明(Claims),而不是只能依靠本地数据库关于用户的信息。你将会看到一个实际的示例,在“使用第三方认证”小节中,将演示如何通过第三方系统来认证用户。不过,此刻我只打算在示例项目中添加一个类,用以模拟一个提供声明(Claims)信息的系统。清单15-15显示了我添加到Infrastructure文件夹中LocationClaimsProvider.cs文件的内容。 Listing 15-15. The Contents of the LocationClaimsProvider.cs File 清单15-15. LocationClaimsProvider.cs文件的内容 using System.Collections.Generic;using System.Security.Claims; namespace Users.Infrastructure {public static class LocationClaimsProvider {public static IEnumerable<Claim> GetClaims(ClaimsIdentity user) {List<Claim> claims = new List<Claim>();if (user.Name.ToLower() == "alice") {claims.Add(CreateClaim(ClaimTypes.PostalCode, "DC 20500"));claims.Add(CreateClaim(ClaimTypes.StateOrProvince, "DC"));} else {claims.Add(CreateClaim(ClaimTypes.PostalCode, "NY 10036"));claims.Add(CreateClaim(ClaimTypes.StateOrProvince, "NY"));}return claims;}private static Claim CreateClaim(string type, string value) {return new Claim(type, value, ClaimValueTypes.String, "RemoteClaims");} }} The GetClaims method takes a ClaimsIdentity argument and uses the Name property to create claims about the user’s ZIP code and state. This class allows me to simulate a system such as a central HR database, which would be the authoritative source of location information about staff, for example. GetClaims方法以ClaimsIdentity为参数,并使用Name属性创建了关于用户ZIP码(邮政编码)和州府的声明(Claims)。上述这个类使我能够模拟一个诸如中心化的HR数据库(人力资源数据库)之类的系统,它可能会成为全体职员的地点信息的权威数据源。 Claims are associated with the user’s identity during the authentication process, and Listing 15-16 shows the changes I made to the Login action method of the Account controller to call the LocationClaimsProvider class. 在认证过程期间,声明(Claims)是与用户标识关联在一起的,清单15-16显示了我对Account控制器中Login动作方法所做的修改,以便调用LocationClaimsProvider类。 Listing 15-16. Associating Claims with a User in the AccountController.cs File 清单15-16. AccountController.cs文件中用户用声明的关联 ...[HttpPost][AllowAnonymous][ValidateAntiForgeryToken]public async Task<ActionResult> Login(LoginModel details, string returnUrl) {if (ModelState.IsValid) {AppUser user = await UserManager.FindAsync(details.Name,details.Password);if (user == null) {ModelState.AddModelError("", "Invalid name or password.");} else {ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user,DefaultAuthenticationTypes.ApplicationCookie); ident.AddClaims(LocationClaimsProvider.GetClaims(ident));AuthManager.SignOut();AuthManager.SignIn(new AuthenticationProperties {IsPersistent = false}, ident);return Redirect(returnUrl);} }ViewBag.returnUrl = returnUrl;return View(details);}... You can see the effect of the location claims by starting the application, authenticating as a user, and requesting the /Claim/Index URL. Figure 15-6 shows the claims for Alice. You may have to sign out and sign back in again to see the change. 为了看看这个地点声明(Claims)的效果,可以启动应用程序,以一个用户进行认证,并请求/Claim/Index URL。图15-6显示了Alice的声明(Claims)。你可能需要退出,然后再次登录才会看到发生的变化。 Figure 15-6. Defining additional claims for users 图15-6. 定义用户的附加声明 Obtaining claims from multiple locations means that the application doesn’t have to duplicate data that is held elsewhere and allows integration of data from external parties. The Claim.Issuer property tells you where a claim originated from, which helps you judge how accurate the data is likely to be and how much weight you should give the data in your application. Location data obtained from a central HR database is likely to be more accurate and trustworthy than data obtained from an external mailing list provider, for example. 从多个地点获取声明(Claims)意味着应用程序不必复制其他地方保持的数据,并且能够与外部的数据集成。Claim.Issuer属性(图15-6中的Issuer数据列——译者注)能够告诉你一个声明(Claim)的发源地,这有助于让你判断数据的精确程度,也有助于让你决定这类数据在应用程序中的权重。例如,从中心化的HR数据库获取的地点数据可能要比外部邮件列表提供器获取的数据更为精确和可信。 1. Applying Claims 1. 运用声明(Claims) The second reason that claims are interesting is that you can use them to manage user access to your application more flexibly than with standard roles. The problem with roles is that they are static, and once a user has been assigned to a role, the user remains a member until explicitly removed. This is, for example, how long-term employees of big corporations end up with incredible access to internal systems: They are assigned the roles they require for each new job they get, but the old roles are rarely removed. (The unexpectedly broad systems access sometimes becomes apparent during the investigation into how someone was able to ship the contents of the warehouse to their home address—true story.) 声明(Claims)有意思的第二个原因是,你可以用它们来管理用户对应用程序的访问,这要比标准的角色管理更为灵活。角色的问题在于它们是静态的,而且一旦用户已经被赋予了一个角色,该用户便是一个成员,直到明确地删除为止。例如,这意味着大公司的长期雇员,对内部系统的访问会十分惊人:他们每次在获得新工作时,都会赋予所需的角色,但旧角色很少被删除。(在调查某人为何能够将仓库里的东西发往他的家庭地址过程中发现,有时会出现异常宽泛的系统访问——真实的故事) Claims can be used to authorize users based directly on the information that is known about them, which ensures that the authorization changes when the data changes. The simplest way to do this is to generate Role claims based on user data that are then used by controllers to restrict access to action methods. Listing 15-17 shows the contents of the ClaimsRoles.cs file that I added to the Infrastructure. 声明(Claims)可以直接根据用户已知的信息对用户进行授权,这能够保证当数据发生变化时,授权也随之而变。此事最简单的做法是根据用户数据来生成Role声明(Claim),然后由控制器用来限制对动作方法的访问。清单15-17显示了我添加到Infrastructure中的ClaimsRoles.cs文件的内容。 Listing 15-17. The Contents of the ClaimsRoles.cs File 清单15-17. ClaimsRoles.cs文件的内容 using System.Collections.Generic;using System.Security.Claims; namespace Users.Infrastructure {public class ClaimsRoles {public static IEnumerable<Claim> CreateRolesFromClaims(ClaimsIdentity user) {List<Claim> claims = new List<Claim>();if (user.HasClaim(x => x.Type == ClaimTypes.StateOrProvince&& x.Issuer == "RemoteClaims" && x.Value == "DC")&& user.HasClaim(x => x.Type == ClaimTypes.Role&& x.Value == "Employees")) {claims.Add(new Claim(ClaimTypes.Role, "DCStaff"));}return claims;} }} The gnarly looking CreateRolesFromClaims method uses lambda expressions to determine whether the user has a StateOrProvince claim from the RemoteClaims issuer with a value of DC and a Role claim with a value of Employees. If the user has both claims, then a Role claim is returned for the DCStaff role. Listing 15-18 shows how I call the CreateRolesFromClaims method from the Login action in the Account controller. CreateRolesFromClaims是一个粗糙的考察方法,它使用了Lambda表达式,以检查用户是否具有StateOrProvince声明(Claim),该声明来自于RemoteClaims发行者(Issuer),值为DC。也检查用户是否具有Role声明(Claim),其值为Employees。如果用户这两个声明都有,那么便返回一个DCStaff角色的Role声明。清单15-18显示了如何在Account控制器中的Login动作中调用CreateRolesFromClaims方法。 Listing 15-18. Generating Roles Based on Claims in the AccountController.cs File 清单15-18. 在AccountController.cs中根据声明生成角色 ...[HttpPost][AllowAnonymous][ValidateAntiForgeryToken]public async Task<ActionResult> Login(LoginModel details, string returnUrl) {if (ModelState.IsValid) {AppUser user = await UserManager.FindAsync(details.Name,details.Password);if (user == null) {ModelState.AddModelError("", "Invalid name or password.");} else {ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user,DefaultAuthenticationTypes.ApplicationCookie);ident.AddClaims(LocationClaimsProvider.GetClaims(ident)); ident.AddClaims(ClaimsRoles.CreateRolesFromClaims(ident));AuthManager.SignOut();AuthManager.SignIn(new AuthenticationProperties {IsPersistent = false}, ident);return Redirect(returnUrl);} }ViewBag.returnUrl = returnUrl;return View(details);}... I can then restrict access to an action method based on membership of the DCStaff role. Listing 15-19 shows a new action method I added to the Claims controller to which I have applied the Authorize attribute. 然后我可以根据DCStaff角色的成员,来限制对一个动作方法的访问。清单15-19显示了在Claims控制器中添加的一个新的动作方法,在该方法上已经运用了Authorize注解属性。 Listing 15-19. Adding a New Action Method to the ClaimsController.cs File 清单15-19. 在ClaimsController.cs文件中添加一个新的动作方法 using System.Security.Claims;using System.Web;using System.Web.Mvc;namespace Users.Controllers {public class ClaimsController : Controller {[Authorize]public ActionResult Index() {ClaimsIdentity ident = HttpContext.User.Identity as ClaimsIdentity;if (ident == null) {return View("Error", new string[] { "No claims available" });} else {return View(ident.Claims);} } [Authorize(Roles="DCStaff")]public string OtherAction() {return "This is the protected action";} }} Users will be able to access OtherAction only if their claims grant them membership to the DCStaff role. Membership of this role is generated dynamically, so a change to the user’s employment status or location information will change their authorization level. 只要用户的声明(Claims)承认他们是DCStaff角色的成员,那么他们便能访问OtherAction动作。该角色的成员是动态生成的,因此,若是用户的雇用状态或地点信息发生变化,也会改变他们的授权等级。 提示:请读者从这个例子中吸取其中的思想精髓。对于读物的理解程度,仁者见仁,智者见智,能领悟多少,全凭各人,译者感觉这里的思想有无数的可能。举例说明:(1)可以根据用户的身份进行授权,比如学生在校时是“学生”,毕业后便是“校友”;(2)可以根据用户所处的部门进行授权,人事部用户属于人事团队,销售部用户属于销售团队,各团队有其自己的应用;(3)下一小节的示例是根据用户的地点授权。简言之:一方面用户的各种声明(Claim)都可以用来进行授权;另一方面用户的声明(Claim)又是可以自定义的。于是可能的运用就无法估计了。总之一句话,这种基于声明的授权(Claims-Based Authorization)有无限可能!要是没有我这里的提示,是否所有读者在此处都会有所体会?——译者注 15.3.3 Authorizing Access Using Claims 15.3.3 使用声明(Claims)授权访问 The previous example is an effective demonstration of how claims can be used to keep authorizations fresh and accurate, but it is a little indirect because I generate roles based on claims data and then enforce my authorization policy based on the membership of that role. A more direct and flexible approach is to enforce authorization directly by creating a custom authorization filter attribute. Listing 15-20 shows the contents of the ClaimsAccessAttribute.cs file, which I added to the Infrastructure folder and used to create such a filter. 前面的示例有效地演示了如何用声明(Claims)来保持新鲜和准确的授权,但有点不太直接,因为我要根据声明(Claims)数据来生成了角色,然后强制我的授权策略基于角色成员。一个更直接且灵活的办法是直接强制授权,其做法是创建一个自定义的授权过滤器注解属性。清单15-20演示了ClaimsAccessAttribute.cs文件的内容,我将它添加在Infrastructure文件夹中,并用它创建了这种过滤器。 Listing 15-20. The Contents of the ClaimsAccessAttribute.cs File 清单15-20. ClaimsAccessAttribute.cs文件的内容 using System.Security.Claims;using System.Web;using System.Web.Mvc; namespace Users.Infrastructure {public class ClaimsAccessAttribute : AuthorizeAttribute {public string Issuer { get; set; }public string ClaimType { get; set; }public string Value { get; set; }protected override bool AuthorizeCore(HttpContextBase context) {return context.User.Identity.IsAuthenticated&& context.User.Identity is ClaimsIdentity&& ((ClaimsIdentity)context.User.Identity).HasClaim(x =>x.Issuer == Issuer && x.Type == ClaimType && x.Value == Value);} }} The attribute I have defined is derived from the AuthorizeAttribute class, which makes it easy to create custom authorization policies in MVC framework applications by overriding the AuthorizeCore method. My implementation grants access if the user is authenticated, the IIdentity implementation is an instance of ClaimsIdentity, and the user has a claim with the issuer, type, and value matching the class properties. Listing 15-21 shows how I applied the attribute to the Claims controller to authorize access to the OtherAction method based on one of the location claims created by the LocationClaimsProvider class. 我所定义的这个注解属性派生于AuthorizeAttribute类,通过重写AuthorizeCore方法,很容易在MVC框架应用程序中创建自定义的授权策略。在这个实现中,若用户是已认证的、其IIdentity实现是一个ClaimsIdentity实例,而且该用户有一个带有issuer、type以及value的声明(Claim),它们与这个类的属性是匹配的,则该用户便是允许访问的。清单15-21显示了如何将这个注解属性运用于Claims控制器,以便根据LocationClaimsProvider类创建的地点声明(Claim),对OtherAction方法进行授权访问。 Listing 15-21. Performing Authorization on Claims in the ClaimsController.cs File 清单15-21. 在ClaimsController.cs文件中执行基于声明的授权 using System.Security.Claims;using System.Web;using System.Web.Mvc;using Users.Infrastructure;namespace Users.Controllers {public class ClaimsController : Controller {[Authorize]public ActionResult Index() {ClaimsIdentity ident = HttpContext.User.Identity as ClaimsIdentity;if (ident == null) {return View("Error", new string[] { "No claims available" });} else {return View(ident.Claims);} } [ClaimsAccess(Issuer="RemoteClaims", ClaimType=ClaimTypes.PostalCode,Value="DC 20500")]public string OtherAction() {return "This is the protected action";} }} My authorization filter ensures that only users whose location claims specify a ZIP code of DC 20500 can invoke the OtherAction method. 这个授权过滤器能够确保只有地点声明(Claim)的邮编为DC 20500的用户才能请求OtherAction方法。 15.4 Using Third-Party Authentication 15.4 使用第三方认证 One of the benefits of a claims-based system such as ASP.NET Identity is that any of the claims can come from an external system, even those that identify the user to the application. This means that other systems can authenticate users on behalf of the application, and ASP.NET Identity builds on this idea to make it simple and easy to add support for authenticating users through third parties such as Microsoft, Google, Facebook, and Twitter. 基于声明的系统,如ASP.NET Identity,的好处之一是任何声明都可以来自于外部系统,即使是将用户标识到应用程序的那些声明。这意味着其他系统可以代表应用程序来认证用户,而ASP.NET Identity就建立在这样的思想之上,使之能够简单而方便地添加第三方认证用户的支持,如微软、Google、Facebook、Twitter等。 There are some substantial benefits of using third-party authentication: Many users will already have an account, users can elect to use two-factor authentication, and you don’t have to manage user credentials in the application. In the sections that follow, I’ll show you how to set up and use third-party authentication for Google users, which Table 15-8 puts into context. 使用第三方认证有一些实际的好处:许多用户已经有了账号、用户可以选择使用双因子认证、你不必在应用程序中管理用户凭据等等。在以下小节中,我将演示如何为Google用户建立并使用第三方认证,表15-8描述了事情的情形。 Table 15-8. Putting Third-Party Authentication in Context 表15-8. 第三方认证情形 Question 问题 Answer 回答 What is it? 什么是第三方认证? Authenticating with third parties lets you take advantage of the popularity of companies such as Google and Facebook. 第三方认证使你能够利用流行公司,如Google和Facebook,的优势。 Why should I care? 为何要关心它? Users don’t like having to remember passwords for many different sites. Using a provider with large-scale adoption can make your application more appealing to users of the provider’s services. 用户不喜欢记住许多不同网站的口令。使用大范围适应的提供器可使你的应用程序更吸引有提供器服务的用户。 How is it used by the MVC framework? 如何在MVC框架中使用它? This feature isn’t used directly by the MVC framework. 这不是一个直接由MVC框架使用的特性。 Note The reason I have chosen to demonstrate Google authentication is that it is the only option that doesn’t require me to register my application with the authentication service. You can get details of the registration processes required at http://bit.ly/1cqLTrE. 提示:我选择演示Google认证的原因是,它是唯一不需要在其认证服务中注册我应用程序的公司。有关认证服务注册过程的细节,请参阅http://bit.ly/1cqLTrE。 15.4.1 Enabling Google Authentication 15.4.1 启用Google认证 ASP.NET Identity comes with built-in support for authenticating users through their Microsoft, Google, Facebook, and Twitter accounts as well more general support for any authentication service that supports OAuth. The first step is to add the NuGet package that includes the Google-specific additions for ASP.NET Identity. Enter the following command into the Package Manager Console: ASP.NET Identity带有通过Microsoft、Google、Facebook以及Twitter账号认证用户的内建支持,并且对于支持OAuth的认证服务具有更普遍的支持。第一个步骤是添加NuGet包,包中含有用于ASP.NET Identity的Google专用附件。请在“Package Manager Console(包管理器控制台)”中输入以下命令: Install-Package Microsoft.Owin.Security.Google -version 2.0.2 There are NuGet packages for each of the services that ASP.NET Identity supports, as described in Table 15-9. 对于ASP.NET Identity支持的每一种服务都有相应的NuGet包,如表15-9所示。 Table 15-9. The NuGet Authenticaton Packages 表15-9. NuGet认证包 Name 名称 Description 描述 Microsoft.Owin.Security.Google Authenticates users with Google accounts 用Google账号认证用户 Microsoft.Owin.Security.Facebook Authenticates users with Facebook accounts 用Facebook账号认证用户 Microsoft.Owin.Security.Twitter Authenticates users with Twitter accounts 用Twitter账号认证用户 Microsoft.Owin.Security.MicrosoftAccount Authenticates users with Microsoft accounts 用Microsoft账号认证用户 Microsoft.Owin.Security.OAuth Authenticates users against any OAuth 2.0 service 根据任一OAuth 2.0服务认证用户 Once the package is installed, I enable support for the authentication service in the OWIN startup class, which is defined in the App_Start/IdentityConfig.cs file in the example project. Listing 15-22 shows the change that I have made. 一旦安装了这个包,便可以在OWIN启动类中启用此项认证服务的支持,启动类的定义在示例项目的App_Start/IdentityConfig.cs文件中。清单15-22显示了所做的修改。 Listing 15-22. Enabling Google Authentication in the IdentityConfig.cs File 清单15-22. 在IdentityConfig.cs文件中启用Google认证 using Microsoft.AspNet.Identity;using Microsoft.Owin;using Microsoft.Owin.Security.Cookies;using Owin;using Users.Infrastructure;using Microsoft.Owin.Security.Google;namespace Users {public class IdentityConfig {public void Configuration(IAppBuilder app) {app.CreatePerOwinContext<AppIdentityDbContext>(AppIdentityDbContext.Create);app.CreatePerOwinContext<AppUserManager>(AppUserManager.Create);app.CreatePerOwinContext<AppRoleManager>(AppRoleManager.Create); app.UseCookieAuthentication(new CookieAuthenticationOptions {AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,LoginPath = new PathString("/Account/Login"),}); app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);app.UseGoogleAuthentication();} }} Each of the packages that I listed in Table 15-9 contains an extension method that enables the corresponding service. The extension method for the Google service is called UseGoogleAuthentication, and it is called on the IAppBuilder implementation that is passed to the Configuration method. 表15-9所列的每个包都含有启用相应服务的扩展方法。用于Google服务的扩展方法名称为UseGoogleAuthentication,它通过传递给Configuration方法的IAppBuilder实现进行调用。 Next I added a button to the Views/Account/Login.cshtml file, which allows users to log in via Google. You can see the change in Listing 15-23. 下一步骤是在Views/Account/Login.cshtml文件中添加一个按钮,让用户能够通过Google进行登录。所做的修改如清单15-23所示。 Listing 15-23. Adding a Google Login Button to the Login.cshtml File 清单15-23. 在Login.cshtml文件中添加Google登录按钮 @model Users.Models.LoginModel@{ ViewBag.Title = "Login";}<h2>Log In</h2> @Html.ValidationSummary()@using (Html.BeginForm()) {@Html.AntiForgeryToken();<input type="hidden" name="returnUrl" value="@ViewBag.returnUrl" /><div class="form-group"><label>Name</label>@Html.TextBoxFor(x => x.Name, new { @class = "form-control" })</div><div class="form-group"><label>Password</label>@Html.PasswordFor(x => x.Password, new { @class = "form-control" })</div><button class="btn btn-primary" type="submit">Log In</button>}@using (Html.BeginForm("GoogleLogin", "Account")) {<input type="hidden" name="returnUrl" value="@ViewBag.returnUrl" /><button class="btn btn-primary" type="submit">Log In via Google</button>} The new button submits a form that targets the GoogleLogin action on the Account controller. You can see this method—and the other changes I made the controller—in Listing 15-24. 新按钮递交一个表单,目标是Account控制器中的GoogleLogin动作。可从清单15-24中看到该方法,以及在控制器中所做的其他修改。 Listing 15-24. Adding Support for Google Authentication to the AccountController.cs File 清单15-24. 在AccountController.cs文件中添加Google认证支持 using System.Threading.Tasks;using System.Web.Mvc;using Users.Models;using Microsoft.Owin.Security;using System.Security.Claims;using Microsoft.AspNet.Identity;using Microsoft.AspNet.Identity.Owin;using Users.Infrastructure;using System.Web; namespace Users.Controllers {[Authorize]public class AccountController : Controller {[AllowAnonymous]public ActionResult Login(string returnUrl) {if (HttpContext.User.Identity.IsAuthenticated) {return View("Error", new string[] { "Access Denied" });}ViewBag.returnUrl = returnUrl;return View();}[HttpPost][AllowAnonymous][ValidateAntiForgeryToken]public async Task<ActionResult> Login(LoginModel details, string returnUrl) {if (ModelState.IsValid) {AppUser user = await UserManager.FindAsync(details.Name,details.Password);if (user == null) {ModelState.AddModelError("", "Invalid name or password.");} else {ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user,DefaultAuthenticationTypes.ApplicationCookie); ident.AddClaims(LocationClaimsProvider.GetClaims(ident));ident.AddClaims(ClaimsRoles.CreateRolesFromClaims(ident)); AuthManager.SignOut();AuthManager.SignIn(new AuthenticationProperties {IsPersistent = false}, ident);return Redirect(returnUrl);} }ViewBag.returnUrl = returnUrl;return View(details);} [HttpPost][AllowAnonymous]public ActionResult GoogleLogin(string returnUrl) {var properties = new AuthenticationProperties {RedirectUri = Url.Action("GoogleLoginCallback",new { returnUrl = returnUrl})};HttpContext.GetOwinContext().Authentication.Challenge(properties, "Google");return new HttpUnauthorizedResult();}[AllowAnonymous]public async Task<ActionResult> GoogleLoginCallback(string returnUrl) {ExternalLoginInfo loginInfo = await AuthManager.GetExternalLoginInfoAsync();AppUser user = await UserManager.FindAsync(loginInfo.Login);if (user == null) {user = new AppUser {Email = loginInfo.Email,UserName = loginInfo.DefaultUserName,City = Cities.LONDON, Country = Countries.UK};IdentityResult result = await UserManager.CreateAsync(user);if (!result.Succeeded) {return View("Error", result.Errors);} else {result = await UserManager.AddLoginAsync(user.Id, loginInfo.Login);if (!result.Succeeded) {return View("Error", result.Errors);} }}ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user,DefaultAuthenticationTypes.ApplicationCookie);ident.AddClaims(loginInfo.ExternalIdentity.Claims);AuthManager.SignIn(new AuthenticationProperties {IsPersistent = false }, ident);return Redirect(returnUrl ?? "/");}[Authorize]public ActionResult Logout() {AuthManager.SignOut();return RedirectToAction("Index", "Home");}private IAuthenticationManager AuthManager {get {return HttpContext.GetOwinContext().Authentication;} }private AppUserManager UserManager {get {return HttpContext.GetOwinContext().GetUserManager<AppUserManager>();} }} } The GoogleLogin method creates an instance of the AuthenticationProperties class and sets the RedirectUri property to a URL that targets the GoogleLoginCallback action in the same controller. The next part is a magic phrase that causes ASP.NET Identity to respond to an unauthorized error by redirecting the user to the Google authentication page, rather than the one defined by the application: GoogleLogin方法创建了AuthenticationProperties类的一个实例,并为RedirectUri属性设置了一个URL,其目标为同一控制器中的GoogleLoginCallback动作。下一个部分是一个神奇阶段,通过将用户重定向到Google认证页面,而不是应用程序所定义的认证页面,让ASP.NET Identity对未授权的错误进行响应: ...HttpContext.GetOwinContext().Authentication.Challenge(properties, "Google");return new HttpUnauthorizedResult();... This means that when the user clicks the Log In via Google button, their browser is redirected to the Google authentication service and then redirected back to the GoogleLoginCallback action method once they are authenticated. 这意味着,当用户通过点击Google按钮进行登录时,浏览器被重定向到Google的认证服务,一旦在那里认证之后,便被重定向回GoogleLoginCallback动作方法。 I get details of the external login by calling the GetExternalLoginInfoAsync of the IAuthenticationManager implementation, like this: 我通过调用IAuthenticationManager实现的GetExternalLoginInfoAsync方法,我获得了外部登录的细节,如下所示: ...ExternalLoginInfo loginInfo = await AuthManager.GetExternalLoginInfoAsync();... The ExternalLoginInfo class defines the properties shown in Table 15-10. ExternalLoginInfo类定义的属性如表15-10所示: Table 15-10. The Properties Defined by the ExternalLoginInfo Class 表15-10. ExternalLoginInfo类所定义的属性 Name 名称 Description 描述 DefaultUserName Returns the username 返回用户名 Email Returns the e-mail address 返回E-mail地址 ExternalIdentity Returns a ClaimsIdentity that identities the user 返回标识该用户的ClaimsIdentity Login Returns a UserLoginInfo that describes the external login 返回描述外部登录的UserLoginInfo I use the FindAsync method defined by the user manager class to locate the user based on the value of the ExternalLoginInfo.Login property, which returns an AppUser object if the user has been authenticated with the application before: 我使用了由用户管理器类所定义的FindAsync方法,以便根据ExternalLoginInfo.Login属性的值对用户进行定位,如果用户之前在应用程序中已经认证,该属性会返回一个AppUser对象: ...AppUser user = await UserManager.FindAsync(loginInfo.Login);... If the FindAsync method doesn’t return an AppUser object, then I know that this is the first time that this user has logged into the application, so I create a new AppUser object, populate it with values, and save it to the database. I also save details of how the user logged in so that I can find them next time: 如果FindAsync方法返回的不是AppUser对象,那么我便知道这是用户首次登录应用程序,于是便创建了一个新的AppUser对象,填充该对象的值,并将其保存到数据库。我还保存了用户如何登录的细节,以便下次能够找到他们: ...result = await UserManager.AddLoginAsync(user.Id, loginInfo.Login);... All that remains is to generate an identity the user, copy the claims provided by Google, and create an authentication cookie so that the application knows the user has been authenticated: 剩下的事情只是生成该用户的标识了,拷贝Google提供的声明(Claims),并创建一个认证Cookie,以使应用程序知道此用户已认证: ...ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user,DefaultAuthenticationTypes.ApplicationCookie);ident.AddClaims(loginInfo.ExternalIdentity.Claims);AuthManager.SignIn(new AuthenticationProperties { IsPersistent = false }, ident);... 15.4.2 Testing Google Authentication 15.4.2 测试Google认证 There is one further change that I need to make before I can test Google authentication: I need to change the account verification I set up in Chapter 13 because it prevents accounts from being created with e-mail addresses that are not within the example.com domain. Listing 15-25 shows how I removed the verification from the AppUserManager class. 在测试Google认证之前还需要一处修改:需要修改第13章所建立的账号验证,因为它不允许example.com域之外的E-mail地址创建账号。清单15-25显示了如何在AppUserManager类中删除这种验证。 Listing 15-25. Disabling Account Validation in the AppUserManager.cs File 清单15-25. 在AppUserManager.cs文件中取消账号验证 using Microsoft.AspNet.Identity;using Microsoft.AspNet.Identity.EntityFramework;using Microsoft.AspNet.Identity.Owin;using Microsoft.Owin;using Users.Models; namespace Users.Infrastructure {public class AppUserManager : UserManager<AppUser> {public AppUserManager(IUserStore<AppUser> store): base(store) {}public static AppUserManager Create(IdentityFactoryOptions<AppUserManager> options,IOwinContext context) {AppIdentityDbContext db = context.Get<AppIdentityDbContext>();AppUserManager manager = new AppUserManager(new UserStore<AppUser>(db)); manager.PasswordValidator = new CustomPasswordValidator {RequiredLength = 6,RequireNonLetterOrDigit = false,RequireDigit = false,RequireLowercase = true,RequireUppercase = true}; //manager.UserValidator = new CustomUserValidator(manager) {// AllowOnlyAlphanumericUserNames = true,// RequireUniqueEmail = true//};return manager;} }} Tip you can use validation for externally authenticated accounts, but I am just going to disable the feature for simplicity. 提示:也可以使用外部已认证账号的验证,但这里出于简化,取消了这一特性。 To test authentication, start the application, click the Log In via Google button, and provide the credentials for a valid Google account. When you have completed the authentication process, your browser will be redirected back to the application. If you navigate to the /Claims/Index URL, you will be able to see how claims from the Google system have been added to the user’s identity, as shown in Figure 15-7. 为了测试认证,启动应用程序,通过点击“Log In via Google(通过Google登录)”按钮,并提供有效的Google账号凭据。当你完成了认证过程时,浏览器将被重定向回应用程序。如果导航到/Claims/Index URL,便能够看到来自Google系统的声明(Claims),已被添加到用户的标识中了,如图15-7所示。 Figure 15-7. Claims from Google 图15-7. 来自Google的声明(Claims) 15.5 Summary 15.5 小结 In this chapter, I showed you some of the advanced features that ASP.NET Identity supports. I demonstrated the use of custom user properties and how to use database migrations to preserve data when you upgrade the schema to support them. I explained how claims work and how they can be used to create more flexible ways of authorizing users. I finished the chapter by showing you how to authenticate users via Google, which builds on the ideas behind the use of claims. 本章向你演示了ASP.NET Identity所支持的一些高级特性。演示了自定义用户属性的使用,还演示了在升级数据架构时,如何使用数据库迁移保护数据。我解释了声明(Claims)的工作机制,以及如何将它们用于创建更灵活的用户授权方式。最后演示了如何通过Google进行认证结束了本章,这是建立在使用声明(Claims)的思想基础之上的。 本篇文章为转载内容。原文链接:https://blog.csdn.net/gz19871113/article/details/108591802。 该文由互联网用户投稿提供,文中观点代表作者本人意见,并不代表本站的立场。 作为信息平台,本站仅提供文章转载服务,并不拥有其所有权,也不对文章内容的真实性、准确性和合法性承担责任。 如发现本文存在侵权、违法、违规或事实不符的情况,请及时联系我们,我们将第一时间进行核实并删除相应内容。
2023-10-28 08:49:21
283
转载
DorisDB
...级失败或升级后不稳定问题的深度剖析与解决方案 一、引言(约500字) 在我们日常使用DorisDB进行大数据处理的过程中,系统升级是不可避免的一环。然而,有时候我们在给系统升级时,可能会遇到些小插曲,比如升级不成功,或者升级完了之后,系统的稳定性反倒不如以前了。这确实会让咱们运维人员头疼不已,平添不少烦恼呢。本文将深入探讨这一现象,并结合实例代码解析可能的原因及应对策略,力求帮助您更好地理解和解决此类问题。 java // 示例代码1:准备DorisDB升级操作 shell> sh bin/start.sh --upgrade // 这是一个简化的DorisDB升级启动命令,实际过程中需要更多详细的参数配置 二、DorisDB升级过程中的常见问题及其原因分析(约1000字) 1. 升级前未做好充分兼容性检查(约200字) 在升级DorisDB时,若未对现有系统环境、数据版本等进行全面兼容性评估,可能会导致升级失败。例如,新版本可能不再支持旧的数据格式或特性。 2. 升级过程中出现中断(约200字) 网络故障、硬件问题或操作失误等因素可能导致升级过程意外中断,从而引发一系列不可预知的问题。 3. 升级后系统资源分配不合理(约300字) 升级后的DorisDB可能对系统资源需求有较大变化,如内存、CPU、磁盘I/O等。要是咱们不把资源分配整得合理点,系统效率怕是要大打折扣,严重时还可能动摇到整个系统的稳定性根基。 java // 示例代码2:查看DorisDB升级前后系统资源占用情况 shell> top // 在升级前后分别执行此命令,对比资源占用的变化 三、案例研究与解决方案(约1000字) 1. 案例一 升级失败并回滚至原版本(约300字) 描述一个具体的升级失败案例,包括问题表现、排查思路以及如何通过备份恢复机制回滚至稳定版本。 java // 示例代码3:执行DorisDB回滚操作 shell> sh bin/rollback_to_version.sh previous_version // 假设这是用于回滚到上一版本的命令 2. 案例二 升级后性能下降的优化措施(约300字) 分析升级后由于资源配置不当导致性能下降的具体场景,并提供调整资源配置的建议和相关操作示例。 3. 案例三 预防性策略与维护实践(约400字) 探讨如何制定预防性的升级策略,比如预先创建测试环境模拟升级流程、严格执行变更控制、持续监控系统健康状况等。 四、结论与展望(约500字) 总结全文讨论的关键点,强调在面对DorisDB系统升级挑战时,理解其内在原理、严谨执行升级步骤以及科学的运维管理策略的重要性。同时,分享对未来DorisDB升级优化方向的思考与期待。 以上内容只是大纲和部分示例,您可以根据实际需求,进一步详细阐述每个章节的内容,增加更多的实战经验和具体代码示例,使文章更具可读性和实用性。
2023-06-21 21:24:48
384
蝶舞花间
Redis
...s官方提供的高可用性解决方案,它是一个分布式系统,负责监控和管理Redis主从集群的健康状态。当主节点出现故障时,Redis Sentinel能够自动检测到问题,并执行故障转移操作,将从节点提升为主节点,从而确保服务的连续性和数据的可靠性。在本文中,探讨了Redis Sentinel配置错误或无法启动的问题及其解决方法。 分布式系统 , 分布式系统是由多个通过网络进行通信的独立计算机节点组成的系统,这些节点共同协作完成一个共同的任务。在本文语境下,Redis Sentinel作为分布式系统的一部分,其作用是在大规模、分布式部署的Redis环境中实现高可用与故障恢复功能。 环境变量 , 环境变量是在操作系统中用于存储有关当前运行环境信息的一种特殊变量,它们能被操作系统、shell脚本以及应用程序访问和使用。在本文中提到的Redis Sentinel配置问题中,环境变量未设置可能会导致Redis Sentinel无法获取必要的运行参数或路径信息,从而无法正常启动。 故障切换(Failover) , 在分布式系统尤其是数据库系统中,故障切换是指当主节点发生故障时,系统能够自动或手动地将服务切换到备份节点的过程,以保证服务的连续性和数据的完整性。在Redis Sentinel的场景下,故障切换由Sentinel组件自动触发并执行,确保即使主Redis服务器宕机,也能快速恢复服务。
2023-03-26 15:30:30
456
秋水共长天一色-t
ClickHouse
...tion:深入理解及解决策略 1. 引言 在大数据时代,ClickHouse作为一款高性能、列式存储的开源SQL数据库管理系统,受到了业界的广泛关注和广泛应用。然而,在实际使用过程中,我们可能会遇到“NodeNotReadyException:节点未准备好异常”这样的问题,这对于初次接触或深度使用ClickHouse的开发者来说,无疑是一次挑战。这篇文章会手把手地带你们钻进这个问题的本质里头,咱们一起通过实实在在的例子把它掰开揉碎了瞧,顺便还会送上解决之道! 2. NodeNotReadyException 现象与原因剖析 “NodeNotReadyException:节点未准备好异常”,顾名思义,是指在对ClickHouse集群中的某个节点进行操作时,该节点尚未达到可以接受请求的状态。这种状况可能是因为节点正在经历重启啊、恢复数据啦、同步副本这些阶段,或者也可能是配置出岔子了,又或者是网络闹脾气、出现问题啥的,给整出来的。 例如,当我们尝试从一个正在启动或者初始化中的节点查询数据时,可能会收到如下错误信息: java try { clickHouseClient.execute("SELECT FROM my_table"); } catch (Exception e) { if (e instanceof NodeNotReadyException) { System.out.println("Caught a NodeNotReadyException: " + e.getMessage()); } } 上述代码中,如果执行查询的ClickHouse节点恰好处于未就绪状态,就会抛出NodeNotReadyException异常。 3. 深入排查与应对措施 (1)检查节点状态 首先,我们需要登录到出现问题的节点,查看其运行状态。可以通过system.clusters表来获取集群节点状态信息: sql SELECT FROM system.clusters; 观察结果中对应节点的is_alive字段是否为1,如果不是,则表示该节点可能存在问题。 (2)日志分析 其次,查阅ClickHouse节点的日志文件(默认路径通常在 /var/log/clickhouse-server/),寻找可能导致节点未准备好的线索,如重启记录、同步失败等信息。 (3)配置核查 检查集群配置文件(如 config.xml 和 users.xml),确认节点间的网络通信、数据复制等相关设置是否正确无误。 (4)网络诊断 排除节点间网络连接的问题,确保各个节点之间的网络是通畅的。可以通过ping命令或telnet工具来测试。 (5)故障转移与恢复 针对分布式场景,合理利用ClickHouse的分布式表引擎特性,设计合理的故障转移策略,当出现节点未就绪时,能自动切换到其他可用节点。 4. 预防与优化策略 - 定期维护与监控:建立完善的监控系统,实时检测每个节点的运行状况,并对可能出现问题的节点提前预警。 - 合理规划集群规模与架构:根据业务需求,合理规划集群规模,避免单点故障,同时确保各节点负载均衡。 - 升级与补丁管理:及时关注ClickHouse的版本更新与安全补丁,确保所有节点保持最新稳定版本,降低因软件问题引发的NodeNotReadyException风险。 - 备份与恢复策略:制定有效的数据备份与恢复方案,以便在节点发生故障时,能够快速恢复服务。 总结起来,面对ClickHouse的NodeNotReadyException异常,我们不仅需要深入理解其背后的原因,更要在实践中掌握一套行之有效的排查方法和预防策略。这样子做,才能确保当我们的大数据处理平台碰上这类问题时,仍然能够坚如磐石地稳定运行,实实在在地保障业务的连贯性不受影响。这一切的一切,都离不开我们对技术细节的死磕和实战演练的过程,这正是我们在大数据这个领域不断进步、持续升级的秘密武器。
2024-02-20 10:58:16
494
月影清风
Etcd
...正常关闭后的重启数据恢复问题详解 Etcd,作为一款分布式键值存储系统,被广泛应用在Kubernetes、Docker Swarm等众多容器编排平台中以实现集群的配置共享和协调服务。不过,在我们日常运维的时候,难免会遇到一些突发状况。比如硬件突然闹脾气出故障啦、网络波动捣乱不稳定啦,甚至有时候人为操作的小失误也可能让Etcd这位小伙伴意外地挂掉,没法正常工作。那么,实际情况中,当Etcd遇到重启后需要恢复数据的状况时,它是怎么巧妙应对的呢?接下来,咱们就通过一些实实在在的代码实例,来一起把这个话题掰开了、揉碎了,好好地研究探讨一番。 1. Etcd的数据持久化机制 首先,我们需要了解Etcd的数据持久化方式。Etcd采用Raft一致性算法保证数据的一致性和高可用性,其数据默认保存在本地磁盘上(可通过--data-dir配置项指定目录),并定期进行快照(snapshot)和日志记录,确保即使在异常情况下也能尽可能减少数据丢失的风险。 bash 启动etcd时设置数据存储目录 etcd --data-dir=/var/lib/etcd 2. 非正常关闭与重启恢复流程 当Etcd非正常关闭后,重启时会自动执行以下恢复流程: (1)检测数据完整性:Etcd启动时,首先会检查data-dir下的快照文件和日志文件是否完整。要是发现文件受损或者不齐全,它会像个贴心的小助手那样,主动去其它Raft节点那里借个肩膀,复制丢失的日志条目,以便把状态恢复重建起来。 (2)恢复Raft状态:基于Raft协议,Etcd通过读取并应用已有的日志和快照文件来恢复集群的最新状态。这一过程包括回放所有未提交的日志,直至达到最新的已提交状态。 (3)恢复成员关系与领导选举:Etcd根据持久化的成员信息重新建立集群成员间的联系,并参与领导选举,以恢复集群的服务能力。 go // 这是一个简化的示例,实际逻辑远比这复杂 func (s EtcdServer) start() error { // 恢复raft状态 err := s raft.Restore() if err != nil { return err } // 恢复成员关系 s.restoreCluster() // 开始参与领导选举 s.startElection() // ... } 3. 数据安全与备份策略 尽管Etcd具备一定的自我恢复能力,但为了应对极端情况下的数据丢失,我们仍需要制定合理的备份策略。例如,可以使用Etcd自带的etcdctl snapshot save命令定期创建数据快照,并将其存储到远程位置。 bash 创建Etcd快照并保存到指定路径 etcdctl snapshot save /path/to/snapshot.db \ --endpoint=https://etcd-cluster-0:2379,https://etcd-cluster-1:2379 如遇数据丢失,可使用etcdctl snapshot restore命令从快照恢复数据,并重新加入至集群。 bash 从快照恢复数据并启动一个新的etcd节点 etcdctl snapshot restore /path/to/snapshot.db \ --data-dir=/var/lib/etcd-restore \ --initial-cluster-token=etcd-cluster-unique-token 4. 结语与思考 面对Etcd非正常关闭后的重启数据恢复问题,我们可以看到Etcd本身已经做了很多工作来保障数据的安全性和系统的稳定性。但这可不代表咱们能对此放松警惕,摸透并熟练掌握Etcd的运行原理,再适时采取一些实打实的备份策略,对提高咱整个系统的稳定性、坚韧性可是至关重要滴!就像人的心跳一旦不给力,虽然身体自带修复技能,但还是得靠医生及时出手治疗,才能最大程度地把生命危险降到最低。同样,我们在运维Etcd集群时,也应该做好“医生”的角色,确保数据的“心跳”永不停息。
2023-06-17 09:26:09
712
落叶归根
SeaTunnel
如何在SeaTunnel中实现数据备份与恢复功能? SeaTunnel(原名Waterdrop)是一款开源、易用且高效的大数据集成工具,它支持从各种数据源抽取数据并进行实时或批处理,同时具备丰富的转换和加载能力。在这篇文章里,咱们就手拉手一起深入探究一下,如何像平常给手机照片做备份防止丢失那样,灵活运用SeaTunnel这个小工具来搞定数据备份与恢复的大问题吧! 1. SeaTunnel基础理解 首先,我们需要对SeaTunnel的核心概念有所了解。在SeaTunnel的世界里,一切操作围绕着“source”(数据源)、“transform”(数据转换)和“sink”(数据目的地)这三个核心模块展开。想象一下,数据如同水流,从源头流出,经过一系列的过滤和转化,最终流向目标水库。 yaml SeaTunnel配置示例 mode: batch 数据源配置 source: type: mysql jdbcUrl: "jdbc:mysql://localhost:3306/test" username: root password: password table: my_table 数据转换(这里暂时为空,但实际可以用于清洗、去重等操作) transforms: 数据目的地(备份到另一个MySQL数据库或HDFS等存储系统) sink: type: mysql jdbcUrl: "jdbc:mysql://backup-server:3306/backup_test" username: backup_root password: backup_password table: backup_my_table 2. 数据备份功能实现 对于数据备份,我们可以将SeaTunnel配置为从生产环境的数据源读取数据,并将其写入到备份存储系统。例如,从MySQL数据库中抽取数据,并存入到另一台MySQL服务器或者HDFS、S3等大数据存储服务: yaml 备份数据到另一台MySQL服务器 sink: type: mysql ... 或者备份数据到HDFS sink: type: hdfs path: /backup/data/ file_type: text 在此过程中,你可以根据业务需求设置定期备份任务,确保数据的实时性和一致性。 3. 数据恢复功能实现 当需要进行数据恢复时,SeaTunnel同样可以扮演关键角色。通过修改配置文件,将备份数据源替换为目标系统的数据源,并重新执行任务,即可完成数据的迁移和恢复。 yaml 恢复数据到原始MySQL数据库 source: type: mysql 这里的配置应指向备份数据所在的MySQL服务器及表信息 sink: type: mysql 这里的配置应指向要恢复数据的目标MySQL服务器及表信息 4. 实践中的思考与探讨 在实际使用SeaTunnel进行数据备份和恢复的过程中,我们可能会遇到一些挑战,如数据量大导致备份时间过长、网络状况影响传输效率等问题。这就需要我们根据实际情况,像变戏法一样灵活调整我们的备份策略。比如说,我们可以试试增量备份这个小妙招,只备份新增或改动的部分,就像给文件更新打个小补丁;或者采用压缩传输的方式,把数据“挤一挤”,让它们更快更高效地在网路上跑起来,这样就能让整个流程更加顺滑、更接地气儿啦。 此外,为了保证数据的一致性,在执行备份或恢复任务时,还需要考虑事务隔离、并发控制等因素,以避免因并发操作引发的数据不一致问题。在SeaTunnel这个工具里头,我们能够借助它那牛哄哄的插件系统和超赞的扩展性能,随心所欲地打造出完全符合自家业务需求的数据备份与恢复方案,就像是量体裁衣一样贴合。 总之,借助SeaTunnel,我们能够轻松实现大规模数据的备份与恢复,保障业务连续性和数据安全性。在实际操作中不断尝试、改进,我坚信你一定能亲手解锁更多SeaTunnel的隐藏实力,让这个工具变成企业数据安全的强大守护神,稳稳地护航你的数据安全。
2023-04-08 13:11:14
114
雪落无痕
Apache Solr
...ication)出现问题 1. 引言 嘿,大家好!今天我要跟大家聊聊Apache Solr中一个让人头疼的问题——复制(Replication)。这玩意儿在Solr里头可重要了,是保证数据高可用性和一致性的关键。但有时候它也会闹脾气,搞得我们焦头烂额。我呢,也是在最近的一次项目中碰上了这个难题。本来以为复制配置很简单,结果发现坑还挺多的。今天我想跟大家分享一下我遇到的问题和我是怎么解决的,希望对大家有点帮助。 2. 复制的基本概念 首先,咱们得知道复制是什么。简单说,就是把一个Solr服务器上的索引文件拷贝到另一个Solr服务器上,就跟把文件从这个文件夹拖到另一个文件夹那样。这样做有几个好处: - 高可用性:即使某个Solr实例宕机,其他实例仍然可以提供服务。 - 负载均衡:多个副本可以分担查询压力,提高整体性能。 - 数据备份:万一主节点数据丢失,副本可以迅速恢复。 但是,如果复制过程中出现问题,就可能导致数据不一致、服务中断等问题。我碰上的是这么个情况,开始还以为是设置不对,结果捣鼓半天才发现原来是网络的事儿。 3. 常见的复制问题 在实际操作中,我遇到了几个常见的问题,包括但不限于: - 网络延迟或断开:这是最常见的问题之一,特别是在跨数据中心的情况下。 - 配置错误:比如主从节点之间的URL配置错误,或者版本不匹配。 - 磁盘空间不足:复制需要大量的磁盘空间,如果空间不足会导致复制失败。 - 权限问题:某些情况下,权限设置不当也会导致复制失败。 4. 解决方案 针对这些问题,我整理了一些解决方案,希望能帮助大家避免类似的麻烦。 4.1 网络问题 先说说网络问题吧,这可能是最头疼的一个。我碰到的问题是主节点和从节点之间的网络有时候会断开,结果复制任务就卡住了,甚至直接失败。解决方法如下: 1. 检查网络连接 确保主节点和从节点之间网络稳定,可以通过ping命令来测试。 2. 增加重试机制 可以在Solr配置文件中设置重试次数,比如: xml 00:00:30 true 5 60 4.2 配置错误 配置错误也很常见,尤其是对于新手来说。有个小窍门,在配置文件里多加点注释,这样就能大大降低出错的几率啦!比如: xml commit schema.xml,stopwords.txt http://localhost:8983/solr/collection1/replication http://localhost:8983/solr/collection1/replication 00:00:30 4.3 磁盘空间问题 磁盘空间不足也是常见的问题,尤其是在大规模数据量的情况下。解决方法是定期清理旧的索引文件,或者增加磁盘容量。Solr提供了清理旧索引的API,可以定时调用: bash curl http://localhost:8983/solr/collection1/admin/cores?action=UNLOAD&core=collection1&deleteIndex=true&deleteDataDir=true 4.4 权限问题 权限问题通常是因为用户没有足够的权限访问Solr API。解决方法是给相关用户分配正确的角色和权限。例如,在Solr的配置文件中设置用户权限: xml etc/security.json true 然后在security.json文件中添加用户的权限信息: json { "authentication": { "class": "solr.BasicAuthPlugin", "credentials": { "admin": "hashed_password" } }, "authorization": { "class": "solr.RuleBasedAuthorizationPlugin", "permissions": [ { "name": "access-replication-handler", "role": "admin" } ], "user-role": { "admin": ["admin"] } } } 5. 总结 通过上面的分享,希望大家都能够更好地理解和处理Apache Solr中的复制问题。复制虽然重要,但也确实容易出错。但只要我们细心排查,合理配置,还是可以解决这些问题的。如果你也有类似的经历或者更好的解决方案,欢迎在评论区留言交流! 最后,我想说的是,技术这条路真的是越走越远,每一个问题都是一次成长的机会。希望大家都能在技术之路上越走越远,越走越稳!
2025-03-11 15:48:41
91
星辰大海
Hive
...据损坏:原因、影响与恢复策略 1. 引言 当我们谈论大数据处理时,Apache Hive作为Hadoop生态系统中的重要组件,以其SQL-like查询语言和对大规模数据集的高效管理能力赢得了广泛的认可。然而,在我们日常运维的过程中,有时候会遇到个让人超级头疼的状况——Hive表的数据竟然出岔子了,或者干脆是损坏了。这篇东西咱们要实实在在地把这个难题掰开了、揉碎了讲明白,从它可能的“病因”一路聊到会带来哪些影响,再到解决这个问题的具体步骤和策略,还会手把手地带你瞅瞅实例代码是怎么操作演示的。 2. 数据损坏的原因剖析 (1)元数据错误 在Hive中,元数据存储在如MySQL或Derby等数据库中,若这部分信息出现丢失或损坏,可能导致Hive无法正确解析和定位数据块。例如,分区信息错误、表结构定义丢失等情况。 sql -- 假设某个分区信息在元数据库中被误删除 ALTER TABLE my_table DROP PARTITION (dt='2022-01-01'); (2)HDFS文件系统问题 Hive底层依赖于HDFS存储实际数据,若HDFS发生节点故障、网络中断导致数据复制因子不足或者数据块损坏,都可能导致Hive表数据不可用。 (3)并发写入冲突 多线程并发写入Hive表时,如果未做好事务隔离和并发控制,可能导致数据覆盖或损坏。 3. 数据损坏的影响及应对思考 数据损坏直接影响业务的正常运行,可能导致数据分析结果错误、报表异常、甚至业务决策失误。因此,发现数据损坏后,首要任务是尽快定位问题根源,并采取相应措施: - 立即停止受影响的服务,防止进一步的数据写入和错误传播。 - 备份当前状态,为后续分析和恢复提供依据。 - 根据日志排查,查找是否有异常操作记录或其他相关线索。 4. 数据恢复实战 (1)元数据恢复 对于元数据损坏,通常需要从备份中恢复,或重新执行DDL语句以重建表结构和分区信息。 sql -- 重新创建分区(假设已知分区详情) ALTER TABLE my_table ADD PARTITION (dt='2022-01-01') LOCATION '/path/to/backup/data'; (2)HDFS数据恢复 对于HDFS层的数据损坏,可利用Hadoop自带的hdfs fsck命令检测并修复损坏的文件块。 bash hdfs fsck /path/to/hive/table -blocks -locations -files -delete 此外,如果存在完整的数据备份,也可直接替换损坏的数据文件。 (3)并发控制优化 对于因并发写入引发的数据损坏,应在设计阶段就充分考虑并发控制策略,例如使用Hive的Transactional Tables(ACID特性),确保数据的一致性和完整性。 sql -- 开启Hive ACID支持 SET hive.support.concurrency=true; SET hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager; 5. 结语 面对Hive表数据损坏的挑战,我们需要具备敏锐的问题洞察力和快速的应急响应能力。同时,别忘了在日常运维中做好预防工作,这就像给你的数据湖定期打个“小强针”,比如按时备份数据、设立警戒线进行监控告警、灵活配置并发策略等等,这样一来,咱们的数据湖就能健健康康,稳稳当当地运行啦。说实在的,对任何一个大数据平台来讲,数据安全和完整性可是咱们绝对不能马虎、时刻得捏在手心里的“命根子”啊!
2023-09-09 20:58:28
642
月影清风
Mongo
...据处理提供了更高效的解决方案。此外,对于异步编程模型,Node.js 14.x及以上版本对async/await的支持更为成熟和完善,结合MongoDB驱动程序的Promise化API,使得开发者能够以更简洁、直观的方式编写异步数据库操作代码。 另外,在实际生产环境中,如何有效利用MongoDB的异步优势进行大规模并发数据处理并确保数据一致性是一大挑战。分布式事务ACID(Atomicity, Consistency, Isolation, Durability)特性的引入以及MongoDB Stitch服务(现已整合进Atlas Serverless)为解决这一问题提供了新的思路。通过集成流式传输框架如Change Streams,开发人员可以构建实时响应的数据处理系统,并保持高可用性和扩展性。 同时,随着云原生架构的普及,MongoDB Atlas作为全球分布式的托管型数据库服务,以其内置的自动分片、备份恢复、监控告警等功能,助力企业无缝迁移至云端,实现弹性伸缩与按需付费,进一步优化资源利用率和降低成本。 综上所述,持续跟踪MongoDB的最新动态和技术演进,结合具体业务场景合理运用其异步特性,有助于提升应用程序性能,应对日益增长的数据处理需求。推荐读者关注MongoDB官方博客、文档更新及行业技术论坛,深入探讨更多关于数据库异步操作的实战经验和最佳实践案例。
2024-03-10 10:44:19
167
林中小径_
Apache Atlas
...。不过呐,它并不插手网络连接层那些具体实现的细枝末节。所以呢,兄弟,咱们没法直接动手写一个Apache Atlas客户端和服务器在网络抽风或者掉线时如何应对的代码实例。为啥呢?原因在于,这些情况通常是由那些藏在底层、默默无闻的通信协议(比如HTTP啊、RESTful API之类的)或者更基础的网络编程工具包在背后自动处理的,不是我们直接能写的。 但是,我可以帮助你构建一篇以“在面对网络不稳定时,Apache Atlas使用者如何优化系统设计和使用策略”为主题的文章,虽然不包含具体的Apache Atlas客户端连接代码,但会尽量满足你的其他要求。 1. 引言 在大数据时代,Apache Atlas作为一款强大的元数据管理系统,在企业级数据湖架构中扮演着至关重要的角色。不过,在实际动手部署和运维的过程中,我们免不了会碰到这样那样的小插曲,就比如说客户端和服务器之间的网络连接时好时坏,甚至有时候还会突然玩个“消失”。这不仅可能导致数据同步延迟,还可能引发一系列的数据一致性问题。在这篇文章里,咱们要实实在在地掰扯一下,在这个特定场景下,咱们该如何正确理解和有效应对,并且在使用Apache Atlas时,有哪些妙招能用上,让整个系统的健壮性和稳定性噌噌噌往上涨。 2. Apache Atlas的服务端与客户端通信机制 Apache Atlas主要通过RESTful API进行服务端与客户端的通信,这意味着任何与Atlas服务器的交互都将以HTTP请求的形式发生。当网络出现波动时,这些请求可能会超时、重试甚至失败。例如,当你尝试执行以下Atlas客户端调用操作(尽管这不是真正的代码,但在真实环境中,它会表现为一个HTTP请求): python 假设的Atlas客户端API调用示例(非真实代码) from atlas_client import AtlasClient client = AtlasClient(base_url="http://atlas-server:21000") entity_result = client.get_entity(guid='your-entity-guid') 3. 应对网络不稳定 策略与实践 (a) 重试机制 在面对网络不稳定时,首要的策略就是实施合理的重试机制。对于HTTP客户端库(如Python的requests库),我们可以设定自动重试策略: python import requests from requests.adapters import HTTPAdapter from urllib3.util.retry import Retry session = requests.Session() retries = Retry(total=5, backoff_factor=0.1, status_forcelist=[ 500, 502, 503, 504 ]) session.mount('http://', HTTPAdapter(max_retries=retries)) session.mount('https://', HTTPAdapter(max_retries=retries)) response = session.get('http://atlas-server:21000/api/atlas/v2/entity/guid/your-entity-guid') 这段伪代码展示了如何配置一个具有重试机制的HTTP客户端,以便在网络状况不佳时仍能尽力获取所需数据。 (b) 缓存策略 在短暂的网络中断期间,可以利用本地缓存存储近期获取的元数据信息,以此降低对实时连接的依赖。一旦网络恢复,再进行必要的数据同步更新。 (c) 心跳检测与故障转移 针对集群环境,可以通过定期心跳检测判断与Atlas服务器的连接状态,及时切换至备份服务器,确保服务的连续性。 4. 结论与思考 面对Apache Atlas客户端与服务器间网络连接不稳定或中断的情况,我们需要从系统设计层面出发,采用合适的容错策略和技术手段提高系统的鲁棒性。同时呢,咱们得摸清楚底层通信机制那些个特性,再结合实际的使用场景,不断打磨、优化咱们的解决方案。这样一来,才能真正让基于Apache Atlas搭建的大数据平台坚如磐石,稳定运行起来。 以上讨论并未给出Apache Atlas本身的代码实现,而是围绕其使用场景和策略给出了建议。实际上,每个项目都有其独特性,具体策略需要根据实际情况灵活调整和实施。
2024-01-10 17:08:06
410
冬日暖阳
DorisDB
数据备份过程中出错?DorisDB助你一臂之力! 1. 引言 在数据管理的世界里,数据备份是保障业务连续性和数据安全的关键环节。然而,在实际操作中,数据备份过程中出现错误的情况时有发生,这些错误可能源于多种因素,包括硬件故障、软件兼容性问题、配置错误等。哎呀,兄弟!今天咱们得聊点实际的,就是用DorisDB处理数据备份时可能会遇到的一些小麻烦。咱们不光要理论分析,还得看看真家伙是怎么出问题的,然后怎么解决。就是要让你我都能明明白白地知道,这些事儿该怎么处理,别让它们成为你的技术路上的绊脚石。咱们得学着从实战中吸取经验,这样下次遇到类似的问题,你就不会一头雾水了,对吧? 2. DorisDB简介与优势 DorisDB是一款高性能、分布式列式存储系统,专为大规模数据集提供实时查询服务。它支持SQL查询语言,并能高效地处理PB级别的数据。哎呀,你瞧,DorisDB这玩意儿可真给力!它提供了超棒的数据备份工具和机制,保证你的数据既完整又一致。不管遇到多复杂的状况,它都能稳稳地运行,就像个忠诚的守护神一样,保护着你的数据安全无虞。是不是感觉用起来既安心又省心呢? 3. 备份策略的重要性 在DorisDB中,制定有效的备份策略至关重要。哎呀,这事儿可得仔细想想!咱们得定期给数据做个备份,以防万一,万一哪天电脑突然罢工或者数据出啥问题,咱还能有东西可补救。别小瞧了这一步,选对备份文件存放在哪儿,多久检查一次备份,还有万一需要恢复数据,咱得有个顺溜的流程,这每一步都挺关键的。就像是给宝贝儿们做保险计划一样,得周全,还得实用,不能光图个形式,对吧?哎呀,兄弟,咱们得给数据做个保险啊!就像你出门前检查门窗一样,定期备份数据,能大大降低数据丢了找不回来的风险。万一哪天电脑罢工或者硬盘坏掉啥的,你也不至于急得团团转,还得去求那些所谓的“数据恢复大师”。而且,备份做得好,恢复数据的时候也快多了,省时间又省心,这事儿得重视起来! 4. 遇到问题时的常见错误及解决方法 错误1:备份失败,日志提示“空间不足” 原因:这通常是因为备份文件的大小超过了可用磁盘空间。 解决方法: 1. 检查磁盘空间 首先确认备份目录的磁盘空间是否足够。 2. 调整备份策略 考虑使用增量备份,仅备份自上次备份以来发生变化的数据部分,减少单次备份的大小。 3. 优化数据存储 定期清理不再需要的数据,释放更多空间。 python 示例代码:设置增量备份 dorisdb_backup = dorisdb.BackupManager() dorisdb_backup.set_incremental_mode(True) 错误2:备份过程中断电导致数据损坏 原因:断电可能导致正在执行的备份任务中断,数据完整性受损。 解决方法: 1. 使用持久化存储 确保备份操作在非易失性存储设备上进行,如SSD或RAID阵列。 2. 实施数据同步 在多个节点间同步数据,即使部分节点在断电时仍能继续备份过程。 python 示例代码:设置持久化备份 dorisdb_backup = dorisdb.BackupManager() dorisdb_backup.enable_persistence() 5. 数据恢复实战 当备份数据出现问题时,及时且正确的恢复策略至关重要。DorisDB提供了多种恢复选项,从完全恢复到特定时间点的恢复,应根据实际情况灵活选择。 步骤1:识别问题并定位 首先,确定是哪个备份文件或时间点出了问题,这需要详细的日志记录和监控系统来辅助。 步骤2:选择恢复方式 - 完全恢复:将数据库回滚到最近的备份状态。 - 时间点恢复:选择一个具体的时间点进行恢复,以最小化数据丢失。 步骤3:执行恢复操作 使用DorisDB的恢复功能,确保数据的一致性和完整性。 python 示例代码:执行时间点恢复 dorisdb_restore = dorisdb.RestoreManager() dorisdb_restore.restore_to_timepoint('2023-03-15T10:30:00Z') 6. 结语 数据备份和恢复是数据库管理中的重要环节,正确理解和应用DorisDB的相关功能,能够有效避免和解决备份过程中遇到的问题。通过本篇讨论,我们不仅了解了常见的备份错误及其解决方案,还学习了如何利用DorisDB的强大功能,确保数据的安全性和业务的连续性。记住,每一次面对挑战都是成长的机会,不断学习和实践,你的数据管理技能将愈发成熟。 --- 以上内容基于实际应用场景进行了概括和举例说明,旨在提供一种实用的指导框架,帮助读者在实际工作中应对数据备份和恢复过程中可能出现的问题。希望这些信息能够对您有所帮助!
2024-07-28 16:23:58
431
山涧溪流
MySQL
...聊聊一个超级实用的小问题——怎么查看MySQL数据库的IP地址。这事儿看起来简单,但其实背后藏着不少门道。嘿,作为一个在数据库这条路上摸爬滚打多年的老鸟,我觉得是时候跟大家唠唠这个事儿了! 首先,咱们得搞清楚为什么需要知道MySQL数据库的IP地址。其实,这个问题的答案可能因人而异。嘿,有的人捣鼓服务器连接,有的人在查网络为啥出问题,还有一堆人就单纯想搞清楚自己鼓捣出来的数据库到底“住”在哪儿,就跟想知道自家小宠物被关在哪间房一样好奇!不管你到底是为了啥,能整清楚数据库的那个IP地址,这本事可真挺关键的!那么接下来,咱们就一步步来解决这个问题! --- 1. 本地MySQL数据库的IP地址 情况一:数据库运行在你的电脑上 如果你的MySQL数据库是安装在你自己的机器上,并且你只打算让它服务于本地的应用程序,那么它的IP地址通常就是localhost或者127.0.0.1。这是最常见的情况之一,也是初学者最容易遇到的场景。 如何确认? 打开命令行工具(Windows用户可以用CMD,Mac/Linux用户可以用Terminal),然后输入以下命令: sql SELECT @@hostname; 这条SQL语句会返回当前MySQL服务器所在的主机名。如果你想进一步验证是不是本地环境,可以再试试: sql SELECT @@datadir; 这段代码会显示MySQL的数据目录路径。要是文件路径里提到你的用户名,或者用的是系统盘符(像 C:\ProgramData\MySQL\MySQL Server 8.0\Data 这种),那十有八九数据库就在你自己的电脑上啦! --- 情况二:数据库运行在远程服务器上 如果你的MySQL数据库部署在一台远程服务器上,那么它的IP地址就不会是localhost了。你需要通过一些工具或者命令来获取具体的IP地址。 方法一:直接登录服务器查看 假设你有一台Linux服务器,可以通过SSH工具(比如PuTTY或终端)登录到服务器后,执行以下命令: bash ifconfig | grep "inet " 这段命令会列出服务器的所有网络接口及其对应的IP地址。如果你看到类似inet 192.168.1.100这样的输出,恭喜你,这就是MySQL数据库所在服务器的IP地址啦! 方法二:通过MySQL命令查看 如果你已经成功连接到了远程MySQL服务器,也可以在MySQL客户端中执行以下命令: sql SELECT @@hostname; 这条命令同样会返回数据库所在的主机名。不过,这里得到的通常是服务器的域名(比如myserver.example.com)。为了找到真实的IP地址,你可以使用ping命令进行测试: bash ping myserver.example.com 通过这种方式,你可以轻松地将域名解析为实际的IP地址。 --- 2. MySQL配置文件中的IP地址 有时候,数据库的IP地址并不是动态分配的,而是明确写在了配置文件里。这种情况下,我们只需要找到配置文件的位置并读取它即可。 配置文件在哪里? 不同的操作系统和安装方式可能会导致配置文件的位置有所不同。以下是常见的几个位置: - Linux/Unix系统:通常是/etc/mysql/my.cnf或者/etc/my.cnf。 - Windows系统:可能是C:\ProgramData\MySQL\MySQL Server 8.0\my.ini。 - macOS:可以尝试查找/usr/local/mysql/my.cnf。 打开配置文件后,搜索关键词bind-address。这个参数定义了MySQL服务监听的IP地址。例如: ini bind-address = 192.168.1.100 这里的192.168.1.100就是MySQL数据库的IP地址。如果该值为空,则表示MySQL监听所有可用的IP地址。 --- 3. 使用第三方工具检测数据库IP 如果你没有权限直接访问服务器或者配置文件,还可以借助一些第三方工具来探测数据库的IP地址。 工具推荐: 1. Nmap 一款强大的网络扫描工具,可以帮助你发现目标服务器上的开放端口和服务。 bash nmap -p 3306 yourdomain.com 如果MySQL服务正在运行并且监听了外部请求,那么这段命令会显示出相应的IP地址。 2. telnet 一种简单的远程连接工具,用于检查特定端口是否可达。 bash telnet yourdomain.com 3306 如果连接成功,说明MySQL服务正在指定的IP地址上运行。 --- 4. 小结与反思 经过一番折腾,我们终于找到了MySQL数据库的IP地址。虽然过程有些曲折,但我相信这些方法对大家来说都非常实用。在这个过程中,我也学到了很多新东西,比如如何解读配置文件、如何利用命令行工具解决问题等等。 最后想提醒大家一句:无论你是新手还是老鸟,在操作数据库时都要小心谨慎,尤其是在涉及网络配置的时候。毕竟,稍不留神就可能导致数据泄露或者其他严重后果。所以,动手之前一定要三思而后行哦! 好了,今天的分享就到这里啦!如果你还有什么疑问或者更好的解决方案,欢迎随时留言交流。咱们下期再见!
2025-03-24 15:46:41
78
笑傲江湖
Kafka
...处理数据时遇到了点小问题,可能是某个部分的状态不对劲了。得赶紧找找是哪里出了岔子,然后对症下药,把这个问题解决掉。毕竟,咱们的系统就像个大家庭,每个成员都得好好配合,才能顺畅运行啊!本文旨在深入探讨这一问题的原因、解决方法以及预防措施。 二、问题解析 理解“InvalidProducerGroupLogPartitionLogSegmentState” 当我们在Kafka的日志中看到这个错误信息时,通常意味着生产者组的日志分区或日志段的状态不正常。这可能是由于多种原因导致的,包括但不限于: - 日志段损坏:Kafka在存储消息时,会将其分割成多个日志段(log segments)。哎呀,你猜怎么着?如果某个日志段因为存储的时候出了点小差错,或者是硬件哪里有点小故障,那可就有可能导致一些问题冒出来!就像是你家电脑里的文件不小心被删了,或者硬盘突然罢工了,结果你得花时间去找回丢失的信息,这事儿在日志里也可能会发生。所以,咱们得好好照顾这些数据,别让它们乱跑乱跳,对吧? - 日志清理策略冲突:Kafka的默认配置可能与特定场景下的需求不匹配,例如日志清理策略设置为保留时间过短或日志备份数量过多等,都可能导致日志段状态异常。 - 生产者组管理问题:生产者组内部的成员管理不当,或者组内成员的增加或减少频繁,也可能引发这种状态的错误。 三、代码示例 如何检测和修复问题 为了更直观地理解这个问题及其解决方法,下面我们将通过一些简单的代码示例来演示如何在Kafka环境中检测并修复这类问题。 示例代码1:检查和修复日志段状态 首先,我们需要使用Kafka提供的命令行工具kafka-log-consumer来检查日志段的状态。以下是一个基本的命令示例: bash 连接到Kafka集群 bin/kafka-log-consumer.sh --zookeeper localhost:2181 --topic your-topic-name --group your-group-name 检查特定日志段的状态 bin/kafka-log-consumer.sh --zookeeper localhost:2181 --topic your-topic-name --group your-group-name --log-segment-state INVALID 如果发现特定日志段的状态为“INVALID”,可以尝试使用kafka-log-cleaner工具来修复问题: bash 启动日志清理器,修复日志段 bin/kafka-log-cleaner.sh --zookeeper localhost:2181 --topic your-topic-name --group your-group-name --repair 示例代码2:调整日志清理策略 对于日志清理策略的调整,可以通过修改Kafka配置文件server.properties来实现。以下是一个示例配置,用于延长日志段的保留时间: properties 延长日志段保留时间 log.retention.hours=24 确保在进行任何配置更改后,重启Kafka服务器以使更改生效: bash 重启Kafka服务器 service kafka-server-start.sh config/server.properties 四、最佳实践与预防措施 为了预防“InvalidProducerGroupLogPartitionLogSegmentState”错误的发生,建议采取以下最佳实践: - 定期监控:使用Kafka监控工具(如Kafka Manager)定期检查集群状态,特别是日志清理和存储情况。 - 合理配置:根据实际业务需求合理配置Kafka的参数,如日志清理策略、备份策略等,避免过度清理导致数据丢失。 - 容错机制:设计具有高容错性的生产者和消费者逻辑,能够处理临时网络中断或其他不可预测的错误。 - 定期维护:执行定期的集群健康检查和日志清理任务,及时发现并解决问题。 五、结语 从失败到成长 面对“InvalidProducerGroupLogPartitionLogSegmentState”这样的问题,虽然它可能会带来暂时的困扰,但正是这些挑战促使我们深入理解Kafka的工作机制和最佳实践。哎呀,学着怎么识别问题,然后把它们解决掉,这事儿可真挺有意思的!不仅能让你的电脑或者啥设备运行得更稳当,还不停地长本事,就像个技术侦探一样,对各种情况都能看得透透的。这不是简单地提升技能,简直是开挂啊!记住,每一次挑战都是成长的机会,让我们在技术的道路上不断前行。
2024-08-28 16:00:42
107
春暖花开
MySQL
...MySQL数据库使用过程中,遇到“Table 'database_name.table_name' doesn't exist”这类错误提示时,表无法找到的问题可能涉及多个层面。深入了解MySQL的权限管理机制、数据库备份与恢复策略以及服务器运行状态监控,是确保数据库稳定高效运行的关键。 近期,一篇由MySQL官方博客发布的《深入理解MySQL权限系统》文章详尽解读了如何精确配置用户权限以避免因权限不足导致的访问错误。文中强调了GRANT和REVOKE命令在分配、撤销特定数据库或表访问权限时的重要性,并提醒用户注意MySQL中大小写敏感设置对表名的影响。 与此同时,关于数据库运维实践,《数据库灾难恢复:从理论到实战》一文结合实例探讨了当数据库表被误删后,如何通过定期备份快速进行数据恢复,并介绍了MySQL自带的binlog日志工具在实时数据同步及增量恢复中的应用。 此外,针对MySQL连接故障问题,InfoQ的一篇报道《优化MySQL连接池配置,提升数据库性能》指出,除了确认服务器运行状态和登录凭据外,合理配置数据库连接池参数也是防止连接故障的有效手段。文章提醒开发者关注连接超时设定、最大连接数限制等关键配置项,以应对高并发场景下的数据库连接挑战。 总之,在实际操作MySQL数据库过程中,不断学习并掌握最新最佳实践,对于解决“Table 'database_name.table_name' doesn't exist”这类常见错误,乃至提高整体数据库管理水平具有深远意义。
2023-11-28 12:42:54
55
算法侠
转载文章
在了解了如何通过ADB工具对Android手机已root后的hosts文件进行修改以适应多环境调试需求后,延伸阅读的内容可以进一步深入到网络安全、域名解析原理以及Android系统权限管理等相关领域。 近期,网络安全专家提醒开发者,在操作过程中需谨慎对待hosts文件的修改。由于hosts文件直接影响系统的域名解析过程,恶意篡改可能导致用户访问钓鱼网站或其他不安全资源,因此在实际操作中务必确保所指向服务器的安全性,并建议定期备份原始hosts文件以便恢复。 与此同时,随着Android 12系统对于隐私和权限控制的强化,对于系统文件如/etc/hosts的直接编辑可能会受到更严格的限制。谷歌正在推动 Scoped Storage 政策,要求应用遵循更加细致的存储访问规则。这意味着未来在处理类似hosts文件这样的系统关键文件时,可能需要采用更为复杂的API调用或用户授权流程。 此外,针对开发者的另一篇深度解读文章指出,除了传统的本地hosts文件修改方法外,还可以利用DNS over HTTPS(DoH)等现代网络技术实现更灵活且安全的域名重定向。这种方式不仅能够满足多环境切换的需求,而且通过加密传输提高了域名解析的安全性和隐私保护能力。 综上所述,虽然本文介绍了基于ADB的hosts文件修改方法,但在实际操作中应充分考虑安全性、权限问题及新技术的应用,以应对不断发展的Android生态系统所带来的挑战与机遇。
2023-06-01 08:27:48
99
转载
MySQL
...根据实际日期填充),Oracle公司发布了MySQL 8.0的最新版本,引入了诸多性能优化和新特性,如窗口函数、原子DDL操作以及改进的安全模块等,进一步提升了MySQL在大规模数据处理与安全防护上的能力。 针对日益严峻的数据安全问题,InfoWorld网站近期发布了一篇深度分析文章,探讨了如何通过实施严格的访问控制策略、加密敏感数据及定期审计来强化MySQL数据库的安全性。此外,文中还介绍了业界最新的数据保护法规GDPR对数据库管理的影响,提醒用户在使用MySQL时需遵循合规要求。 同时,鉴于云服务的普及,Amazon RDS for MySQL作为一种托管型数据库服务备受关注。AWS官方博客分享了关于如何高效迁移本地MySQL数据库至RDS,并实现无缝备份与恢复的实战经验,为众多寻求上云解决方案的企业提供了宝贵参考。 不仅如此,对于希望深入理解MySQL内部机制的开发者,Stack Overflow上有资深专家撰写了系列教程,详尽解析了InnoDB存储引擎的工作原理,以及SQL查询优化技巧,帮助读者提升数据库设计与运维水平。 总之,在掌握MySQL基本使用的基础上,持续跟进技术发展动态,深入了解并实践高级功能与安全管理措施,是确保MySQL数据库在各类型应用程序中稳定高效运行的关键。
2023-02-05 14:43:17
74
程序媛
MySQL
...ySQL数据库的启动过程及其管理命令之后,您可能对数据库运维和优化有了更深的兴趣。近期,MySQL 8.0版本推出了一系列改进与新特性,例如增强的安全功能、性能提升以及InnoDB存储引擎的优化,这些都直接影响了数据库启动和运行效率(参考来源:MySQL官方网站发布说明)。针对MySQL的启动问题,许多专业论坛如Stack Overflow上持续有开发者分享实战经验及解决方案。 此外,随着云原生技术的发展,越来越多的企业选择将MySQL部署在云环境中,如AWS RDS或阿里云RDS等服务,它们提供了自动化的MySQL实例生命周期管理,包括启动、停止、备份恢复以及监控告警等功能,大大简化了运维工作流程(参考来源:AWS官方文档、阿里云RDS产品介绍)。 对于深入理解MySQL启动机制并进一步进行故障排查,可参阅《高性能MySQL》一书中的相关章节,作者深入剖析了MySQL服务器内部运作原理,并给出了大量实战案例和优化建议,是数据库管理员和技术开发人员的重要参考资料(参考来源:《高性能MySQL》)。 同时,为了保障数据安全和业务连续性,掌握MySQL日志文件分析也是至关重要的技能之一。通过查看错误日志、查询日志和二进制日志,可以实时追踪数据库启动过程中的任何异常情况,从而快速定位问题并实施有效修复(参考来源:MySQL官方文档关于日志配置和解读的内容)。 总之,在实际应用中,了解并熟练运用MySQL的启动管理命令只是数据库运维的基础,结合最新版本特性、云环境实践以及深入的理论学习,才能真正实现对MySQL数据库高效稳定的运维管理。
2023-06-06 17:14:58
79
逻辑鬼才
Ruby
...景:在咱们日常开发的过程中,为了揪出性能瓶颈,尝试使用了Rack MiniProfiler进行监控,结果却发现这个小家伙居然不给力,无法准确地显示出想要的结果?哎呀,那种感觉是不是特让人挠头又沮丧,尤其是当你正在全力以赴优化你的应用性能,希望它能飞速奔跑起来的时候。别担心,我们将在本文中共同探讨这个问题的原因,并寻找解决方案。 一、什么是Rack MiniProfiler? Rack MiniProfiler是一款轻量级的性能分析工具,用于在Rails应用程序中捕获并展示HTTP请求的时间消耗。这个小工具可以帮我们揪出那些偷偷“吃掉”大量时间的操作,然后给我们提供线索去改进和优化代码,让程序跑得更溜。 二、为什么Rack MiniProfiler无法正常显示? 造成Rack MiniProfiler无法正常显示的原因有很多。以下是一些常见的原因: 2.1 配置错误 如果你没有正确地配置Rack MiniProfiler,那么它可能无法正常工作。比如说,你可能需要确认自己已经装上了正确的工具包(比如这个叫rack-mini-profiler的小玩意儿),并且得把它妥妥地引入到config.ru文件里边去。 2.2 Ruby版本不兼容 Rack MiniProfiler可能不支持某些旧版本的Ruby。确保你的Ruby版本是最新并且支持的版本。 2.3 网络问题 有时候,网络问题也可能导致Rack MiniProfiler无法正常显示。检查你的网络连接是否有问题。 三、如何解决问题? 如果你遇到了上述的问题,下面是一些可能的解决方案: 3.1 检查配置 首先,你需要确保你的配置是正确的。你可以通过查看Rails日志或者运行rails server -e production --debug命令来确认。 如果配置没有问题,那么可能是其他的问题。 3.2 更新Gem 如果你的Gem版本过低,那么可以尝试更新到最新的版本。嘿,你知道吗?如果你想更换Gemfile里某个Gem的版本,完全可以手动去修改它。改完之后,只需要简单地运行一句命令——bundle install,就可以完成更新啦!就像是给你的项目安装最新软件包一样轻松便捷。 3.3 重启服务器 如果你怀疑是网络问题,那么可以尝试重启服务器。这通常会解决大部分网络相关的问题。 四、总结 Rack MiniProfiler是一个非常强大的性能分析工具,能够帮助我们找出并解决性能瓶颈。然而,由于各种原因,它有时也会出现一些问题。只要你能像侦探一样挖出问题的根源,再对症下药采取合适的解决办法,那么,妥妥地,你就能手到擒来地把问题给解决了,成功绝对在望!所以,请保持耐心和冷静,相信你一定能找到答案!
2023-08-02 20:30:31
106
素颜如水-t
Tomcat
...omcat服务器运行过程中,由于某种资源受限(如CPU、内存、线程池配置不合理等)或代码执行效率低下导致整体响应速度降低的问题。 VisualVM , VisualVM是一款强大的Java性能分析工具,由Oracle公司开发并开源。它能够集成多种JDK命令行工具和轻量级 profiling API,提供包括CPU、内存、类加载、线程、垃圾回收等多方面的监控与分析功能,帮助开发者定位并解决Java应用程序中的性能问题,文中使用VisualVM来检测和诊断Tomcat服务器的性能瓶颈。 微服务架构 , 微服务架构是一种将单一应用程序划分成一组小的、相互独立的服务的设计方法,每个服务运行在其自身的进程中,服务之间采用轻量级的方式进行通信,通常通过HTTP RESTful API。在应对Tomcat性能瓶颈的解决方案中提及微服务架构,是因为它可以将大型系统的复杂性分散到多个小型服务中,从而避免单个节点成为性能瓶颈,提高系统的可扩展性和容错性。
2023-07-31 10:08:12
342
山涧溪流-t
Apache Solr
在解决Apache Solr中SolrServerException的问题之后,深入理解和掌握Solr的配置与网络通信机制对于保障搜索引擎高效稳定运行至关重要。近期,Apache Solr 8.11版本发布,带来了诸多性能优化和安全增强功能,包括对SSL/TLS连接的进一步改进,支持更多现代加密协议,这有助于开发者更好地处理与证书相关的异常情况。 同时,针对云环境和分布式部署场景下Solr集群可能出现的网络问题,《Apache Solr权威指南》一书提供了详尽的实践解析和案例分析,指导读者如何排查、预防类似SolrServerException等由于网络或配置引发的故障。 此外,在实际开发过程中,遵循最佳实践进行Solr服务器配置也相当关键。例如,确保正确的请求超时设置、合理规划核心(Core)和集合(Collection)配置,以及利用Zookeeper进行高效的集群管理和监控等策略,都能有效降低遭遇此类异常的风险。 近期,InfoQ等技术媒体也报道了多个成功解决大型企业级搜索服务中Solr相关问题的实际案例,其中涉及到了对Solr日志的有效分析、自定义插件开发以适应特定业务需求等方面的经验分享,值得广大Solr使用者借鉴参考。
2023-03-23 18:45:13
462
凌波微步-t
Etcd
...无法从数据目录启动的问题及其解决方案后,我们可以进一步关注分布式系统存储和容灾备份的最新实践和发展趋势。近期,随着云原生架构的普及,Etcd作为Kubernetes等容器编排系统的基石,在集群状态管理和配置存储方面的重要性日益凸显。为了提升系统的稳定性和可用性,业界对于Etcd的数据保护策略、高可用设计以及灾难恢复方案的研究与实践不断深化。 例如,Google Cloud Platform团队近期发布了一篇关于Etcd存储层优化与故障恢复机制的深度分析报告,详尽阐述了如何通过改进snapshot策略、增强数据持久化能力以及实现跨地域多副本冗余,以降低由于硬件故障或网络问题导致的数据丢失风险。 同时,CNCF社区也正在积极推动Etcd项目的持续演进,包括对Raft一致性算法的优化、性能提升以及安全特性的增强等方面。针对Etcd的运维管理,有专业团队分享了实战经验,比如定期执行健康检查、监控关键指标,并结合自动化工具进行故障切换演练和备份恢复测试,确保在实际生产环境中能够快速有效地应对类似“Etcdserver无法从数据目录启动”的问题。 总之,理解并掌握Etcd的核心功能与运维要点,紧密跟踪其发展动态和技术前沿,对于构建和维护健壮高效的分布式系统具有重要的现实意义。
2023-01-07 12:31:32
511
岁月静好-t
站内搜索
用于搜索本网站内部文章,支持栏目切换。
知识学习
实践的时候请根据实际情况谨慎操作。
随机学习一条linux命令:
ln -s target link
- 创建符号链接。
推荐内容
推荐本栏目内的其它文章,看看还有哪些文章让你感兴趣。
2023-04-28
2023-08-09
2023-06-18
2023-04-14
2023-02-18
2023-04-17
2024-01-11
2023-10-03
2023-09-09
2023-06-13
2023-08-07
2023-03-11
历史内容
快速导航到对应月份的历史文章列表。
随便看看
拉到页底了吧,随便看看还有哪些文章你可能感兴趣。
时光飞逝
"流光容易把人抛,红了樱桃,绿了芭蕉。"