.NET Core 如何使用Session
第一步先注册session: 在Startup.cs文件中的ConfigureServices方法中添加:
1 |
services.AddSession(); |
在Startup.cs文件中的Configure方法中加上这一句代码
1 |
app.UseSession(); |
第二步从nuget安装Microsoft.AspNetCore.Mvc引用,直接使用自带的方法进行设置和获取session 不过自带的方法设置和获取的session值是byte[]类型的,可以从nuget安装并引用Microsoft.AspNetCore.Http并使用里面的扩展方法
1 |
using Microsoft.AspNetCore.Http; |
扩展后的方法为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/// <summary> /// 设置Session /// </summary> /// <param name="key">键</param> /// <param name="value">值</param> protected void SetSession(string key, string value) { HttpContext.Session.SetString(key, value); } /// <summary> /// 获取Session /// </summary> /// <param name="key">键</param> /// <returns>返回对应的值</returns> protected string GetSession(string key) { var value = HttpContext.Session.GetString(key); if (string.IsNullOrEmpty(value)) value = string.Empty; return value; } |
下面这个图片里面的方法是自带的操作session的方法 下面这个图片是扩展的方法: from:https://www.cnblogs.com/dawenyang/p/9227713.html
View DetailsASP.NET Core 3中的自定义授权
您有一个Web API,并且想要实现自己的授权逻辑,该怎么做?您需要做四件事。 1. 创建您的自定义授权属性 2. 在控制器上使用自定义授权属性 3. 在自定义请求管道中间件中创建授权逻辑 4. 启动时注册中间件 创建您的自定义授权属性
1 2 3 4 5 6 7 8 9 10 |
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] public class CustomAuthorizeAttribute : Attribute { public string[] AllowedUserRoles { get; private set; } public CustomAuthorizeAttribute(params string[] allowedUserRoles) { this.AllowedUserRoles = allowedUserRoles; } } |
在控制器上使用自定义授权属性
1 2 3 4 5 6 7 8 9 10 11 12 |
[ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { [HttpGet] [CustomAuthorize("Admin", "Supervisor", "Worker")] public string Get() { return "Sunny"; } } |
在自定义请求管道中间件中创建授权逻辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
public static class CustomAuthorizationMiddleware { public static async Task Authorize(HttpContext httpContext, Func<dynamic> next) { var endpointMetaData = httpContext.GetEndpoint().Metadata; bool hasCustomAuthorizeAttribute = endpointMetaData.Any(x => x is CustomAuthorizeAttribute); if (!hasCustomAuthorizeAttribute) { await next.Invoke(); return; } CustomAuthorizeAttribute customAuthorizeAttribute = endpointMetaData .FirstOrDefault(x => x is CustomAuthorizeAttribute) as CustomAuthorizeAttribute; // TODO: change authorization logic bool isAuthorized = customAuthorizeAttribute.AllowedUserRoles .Any(allowedRole => httpContext.User.IsInRole(allowedRole)); if (isAuthorized) { await next.Invoke(); return; } httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized; await httpContext.Response.WriteAsync("unauthorized"); } } |
启动时注册中间件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseRouting(); app.Use(CustomAuthorizationMiddleware.Authorize); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } |
确保在调用app.UseRouting()之后添加中间件。这样可以确保在将路由 信息添加到HttpContext 后执行您的中间件。 from:https://www.cnblogs.com/bisslot/p/12330985.html
View Detailsgradle 淘宝镜像
配置gradle镜像 在 %USERPROFILE%\.gradle下面创建新文件init.gradle,输入下面的内容并保存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
allprojects{ repositories { def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public/' all { ArtifactRepository repo -> if(repo instanceof MavenArtifactRepository){ def url = repo.url.toString() if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')) { project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL." remove repo } } } maven { url REPOSITORY_URL } } } |
配置maven镜像 复制E:\maven\apache-maven-3.3.9\conf中的settings.xml到%USERPROFILE%\.m2中,修改mirrors即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </mirror> <mirror> <id>jcenter</id> <mirrorOf>central</mirrorOf> <name>jcenter.bintray.com</name> <url>http://jcenter.bintray.com/</url> </mirror> <mirror> <id>repo1</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://repo1.maven.org/maven2/</url> </mirror> <mirror> <id>repo2</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://repo2.maven.org/maven2/</url> </mirror> </mirrors> |
build.gradle 配置
1 2 3 4 5 |
repositories { mavenLocal() mavenCentral() maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} } |
作者:liurongming 链接:https://www.jianshu.com/p/67b547b4e6ae 来源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
View DetailsLinux查看和编辑文件
例如,要想test.txt文件添加内容"I am a boy",test.txt在当前目录中 方法一:vi编辑法 打开终端,输入vi test.txt 回车,按a或i进入编辑模式,输入 I am a boy,然后按esc键退出编辑模式,输入:wq保存并退出。 方法二:echo命令法 打开终端,输入echo ‘I am a boy’ >> ./test.txt 注:追加单行文本法 方法三:cat命令法 cat >> ./test.txt <<EOF I am a boy EOF 注:结尾的EOF要顶格,成对出现,可以其它字符代替。 方法四:cat编辑法 cat >> ./test.txt (回车后开始编辑输入内容) I am a boy. 按cntl+d组合键结束编辑。 VIM编辑器,可以新建文件也可以修改文件,命令为:vim /usr/local/con.cfg 如果这个文件,以前是没有的,则为新建,则下方有提示为新文件,如果文件已存在,则没有提示。 进入编辑器后,我们先按"I”,即切换到“插入”状态。就可以通过上下左右移动光标,或空格、退格及回车等进行编辑内容了,和WINDOWS是一样的了。 当文本编辑结束之后,通常需要退出编辑器。退出编辑器又分为4种情况:保存退出、正常退出、不保存退出及强制退出。 按键盘左上角的"ESC",左下角的插入状态不见了 然后这时,我们输入“冒号”,即":"(不需双引号),在下方会出现冒号,等待输入命令,如图,我输入的是WQ。功能如下。 W:write,写入 Q:quit,退出 再回车,就保存退出了 其实,保存退出还有二个方法: A:在最后输入命令时,直接输入"x",也是一样的,即X=WQ。 B:最快捷的方法:按了ESC后,直接按shift+zz,或者切换到大写模式按ZZ,就可以保存退出了,即是按2下大写的Z。 可以用查看命令:cat查看其内容:cat /usr/local/con.cfg 正常退出有个前提条件是:打开的文本文件在内容上没有被改动过。 按了ESC后再输入冒号,在输入命令时,直接输入"q", 不保存退出的方法,很多时候打开了文件,或者修改了一些地方,才发现错了,非常需要不保存退出。 先按ESC,再输入冒号,在输入命令时,直接输入"q!" 强制退出。这个实在是不应该做的操作,因为很操蛋! 先按ESC,再按冒号,在输入命令时,直接输入"!" 参考: https://jingyan.baidu.com/article/495ba8410ff14d38b30ede01.html from:https://www.cnblogs.com/bingle/p/9785621.html
View DetailsLinux查看文件内容的5种方式
目录 1. more指令 —— 分页显示文件内容 2. less指令 —— 可以向前或向后查看文件内容 3. head指令 —— 查看文件开头的内容 4. tail指令 —— 显示文件尾部的内容 5. cat指令 —— 显示文件内容 1. more指令 —— 分页显示文件内容 more指令会以一页一页的形式显示文件内容,按空白键(space)显示下一页内容,按Enter键会显示下一行内容,按 b 键就会往回(back)一页显示,其基本用法如下: more file1 查看文件file1的文件内容; more -num file2 查看文件file2的内容,一次显示num行; more +num file3 查看文件file3的内容,从第num行开始显示; 2. less指令 —— 可以向前或向后查看文件内容 less指令查看文件内容时可以向前或向后随意查看内容; less指令的基本用法为: less file1 查看文件file1的内容; less -m file2 查看文件file2的内容,并在屏幕底部显示已显示内容的百分比; 按空格键显示下一屏的内容,按回车键显示下一行的内容; 按 U 向前滚动半页,按 Y 向前滚动一行; 按[PageDown]向下翻动一页,按[PageUp]向上翻动一页; 按 Q 退出less命令; 3. head指令 —— 查看文件开头的内容 head指令用于显示文件开头的内容,默认情况下,只显示文件的头10行内容; head指令的基本用法: head -n <行数> filename 显示文件内容的前n行; 例如:head -n 5 file1 显示文件file1的前5行内容 […]
View DetailsMariaDB 10.4.12 Stable Row size too large (> 8126). Changing some columns to TEXT or BLOB may help.
MariaDB 10.4.12 Stable# Row size too large (> 8126). Changing some columns to TEXT or BLOB may help.# 以下两步解决问题 1. 修改my.ini文件在[mysqld]下面加入下面三行#
1 2 3 |
innodb_file_per_table innodb_file_format = Barracuda innodb_strict_mode = 0 |
2. 新建一个查询进行如下操作将nombre_tabla改成你的表名#
1 2 3 |
ALTER TABLE nombre_tabla ENGINE=InnoDB ROW_FORMAT=COMPRESSED; |
from:https://www.cnblogs.com/fanlinglong/p/12425116.html
View Details搭建gitlab仓库
稍具规模一点的公司都会搭建属于自己的git,svn,而内部git用的最多的则是gitlab,虽然官网已经提供了非常多的功能,但内网搭建更能保证项目的私有性,只有公司内部员工才可以访问,更加安全。 这里演示gitlab的搭建与简单配置 操作 安装一些依赖软件包,SSH一般系统是默认安装好的,不过也不排除一些最小安装的系统没有sshd服务。
1 2 3 |
sudo yum install -y curl policycoreutils-python openssh-server sudo systemctl enable sshd sudo systemctl start sshd |
关闭防火墙,或者开放HTTP的端口
1 2 |
//刷新防火墙的规则 iptables -F |
安装邮件服务,当gitlab想要通过邮件通知,也可以另外配置其它的邮件服务器
1 2 3 |
sudo yum install postfix sudo systemctl enable postfix sudo systemctl start postfix |
从官网获取一件安装脚本,当然自己手动安装也是可以的gitlab下载地址,使用官网脚本会简单一些。执行这一步会如果使用CentOS系统,会添加gitlab的yum源
1 2 3 4 |
//输出到文件里是为了看下下载的脚本内容 curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh > rpm.sh chmod +x rpm.sh ./rpm.sh |
安装gitlab
1 2 3 4 |
//使用yum安装gitlab yum install -y gitlab-ee //可以看下gitlab-ee包的内容,看到gitlab安装在/opt/gitlab目录下 rpm -ql gitlab-ee | less |
上面已经安装好了gitlab,不过可以稍作一些配置,配置gitlab监听的地址与端口,gitlab的配置文件在/etc/gitlab/目录下,主要配置文件为gitlab.rb我修改了下gitlab.rb文件中的nginx监听地址,
1 2 3 4 |
external_url 'http://gitlab.ai-he.me' nginx['listen_addresses'] = ['0.0.0.0', '[::]'] # 系统端口冲突,我把端口改为了82 nginx['listen_port'] = 82 |
里面的配置项非常的多,可以对照官网文档根据需要修改。gitlab配置选项 运行gitlab命名,并重启
1 2 3 4 5 6 |
//重新配置gitlab sudo gitlab-ctl reconfigure //重启gitlab gitlab-ctl restart // 查看gitlab-ctl命令的帮助信息 gitlab-ctl --help |
打开浏览器查看效果,第一次打开页面会让我们设置root用户的密码。记住自己设置的密码,再次刷新进入登录页面 以管理员身份登录,默认的用户是root,密码是刚才设置的。 搭建好环境之后,下面的则根据官方文档解释,自己摸索做一些根据自己需要的修改,二次开发也可以。 最后 公司内部一般都会搭建内部gitlab仓库,自己搭建下摸索着玩玩。 参考 gitlab下载地址 gitlab配置选项 作者:Real_man 链接:https://www.jianshu.com/p/ade38a53b1ac 来源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
View DetailsCentOS postfix启动报错net_interfaces: no local interface
CentOS 7 postfix启动报错: inet_interfaces: no local interface found for ::1 当前环境CentOS 7.4 [root@test ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@test ~]# uname -r 3.10.0-693.2.2.el7.x86_64 使用systemctl start postfix时,总是提示: Job for postfix.service failed because the control process exited with error code. See “systemctl status postfix.service” and “journalctl -xe” for details. 重启也无效,查看日志more /var/log/maillog 或者 more /var/log/message 发现日志有报错信息: postfix: fatal: parameter inet_interfaces: no local interface found for ::1 重启服务器也没有解决 解决方法: vi /etc/postfix/main.cf 发现配置为: inet_interfaces = localhost inet_protocols = all 改成: inet_interfaces = all 或者 inet_interfaces = 127.0.0.1 inet_protocols = all […]
View Detailsspringboot(八) 开启热部署之Idea&Gradle
一、引入starter
1 2 |
//热部署 compile("org.springframework.boot:spring-boot-devtools") |
二、开启自动编译 第一步
1 2 3 |
windows:ctrl + alt + shift + / mac: command + alt + shift + / |
弹出以下界面 第二步 点击Registry,勾选compiler.automake.allow.when.app.running 第三步 勾选 Make/Build project automatically 重新运行项目,随意改一个java类,看看项目是不是重启了^_^ from:https://blog.csdn.net/JonWu0102/article/details/81064776
View DetailsGradle依赖排除
在引用依赖时经常会有这样的问题:某些间接引用的依赖项是不需要的;产生了依赖冲突。此时需要排除一些依赖。 下面的内容介绍了几种在gradle中排除依赖的方式。 在dependency中排除 1 2 3 4 5 6 7 8 dependencies { compile('com.zhyea:ar4j:1.0') { //excluding a particular transitive dependency: exclude module: 'cglib' //by artifact name exclude group: 'org.jmock' //by group exclude group: 'org.unwanted', module: 'iAmBuggy' //by both name and group } } 这种方式是粒度最细的,也是最为繁琐的。此时可以考虑全局设置。 在全局配置中排除 全局配置是在configuration中完成的。 1 2 3 4 configurations { compile.exclude module: 'cglib' all*.exclude group:’org.unwanted', module: 'iAmBuggy' } 禁用传递依赖 禁用传递依赖需要将属性transitive设置为false。可以在dependency中禁用传递依赖,也可以在configuration中全局禁用: 1 2 3 4 5 6 7 compile('com.zhyea:ar4j:1.0') { transitive = false } configurations.all { transitive = false } 还可以在单个依赖项中使用@jar标识符忽略传递依赖: […]
View Details