您有一个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 Details配置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 Details例如,要想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 Details目录 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.# 以下两步解决问题 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稍具规模一点的公司都会搭建属于自己的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 Details一、引入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 Details在引用依赖时经常会有这样的问题:某些间接引用的依赖项是不需要的;产生了依赖冲突。此时需要排除一些依赖。 下面的内容介绍了几种在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 Detailsspringboot 中默认的web容器是tomcat。 在maven 的pom 文件中加入如下依赖,便可使用tomcat 容器。
1 2 3 4 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> |
如果想使用 jetty 作为 web容器,需要2步操作: 1.排除默认的tomcat 容器
1 2 3 4 5 6 7 8 9 10 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> |
2.加入 jetty 的 starter 依赖
1 2 3 4 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> |
from:https://www.cnblogs.com/appleat/p/9100822.html
View DetailsSpringWebflux是SpringFramework5.0添加的新功能,WebFlux本身追随当下最火的Reactive Programming而诞生的框架,那么本篇就来简述一下这个框架到底是做什么的 一、关于WebFlux 我们知道传统的Web框架,比如说:struts2,springmvc等都是基于Servlet API与Servlet容器基础之上运行的,在Servlet3.1之后才有了异步非阻塞的支持。而WebFlux是一个典型非阻塞异步的框架,它的核心是基于Reactor的相关API实现的。相对于传统的web框架来说,它可以运行在诸如Netty,Undertow及支持Servlet3.1的容器上,因此它的运行环境的可选择行要比传统web框架多的多。 根据官方的说法,webflux主要在如下两方面体现出独有的优势: 1)非阻塞式 其实在servlet3.1提供了非阻塞的API,WebFlux提供了一种比其更完美的解决方案。使用非阻塞的方式可以利用较小的线程或硬件资源来处理并发进而提高其可伸缩性 2) 函数式编程端点 老生常谈的编程方式了,Spring5必须让你使用java8,那么函数式编程就是java8重要的特点之一,而WebFlux支持函数式编程来定义路由端点处理请求。 二、SpringMVC与SpringWebFlux 我们先来看官网的一张图: 它们都可以用注解式编程模型,都可以运行在tomcat,jetty,undertow等servlet容器当中。但是SpringMVC采用命令式编程方式,代码一句一句的执行,这样更有利于理解与调试,而WebFlux则是基于异步响应式编程,对于初次接触的码农们来说会不习惯。对于这两种框架官方给出的建议是: 1)如果原先使用用SpringMVC好好的话,则没必要迁移。因为命令式编程是编写、理解和调试代码的最简单方法。因为老项目的类库与代码都是基于阻塞式的。 2)如果你的团队打算使用非阻塞式web框架,WebFlux确实是一个可考虑的技术路线,而且它支持类似于SpringMvc的Annotation的方式实现编程模式,也可以在微服务架构中让WebMvc与WebFlux共用Controller,切换使用的成本相当小 3)在SpringMVC项目里如果需要调用远程服务的话,你不妨考虑一下使用WebClient,而且方法的返回值可以考虑使用Reactive Type类型的,当每个调用的延迟时间越长,或者调用之间的相互依赖程度越高,其好处就越大 我个人意见是:官网明确指出,SpringWebFlux并不是让你的程序运行的更快(相对于SpringMVC来说),而是在有限的资源下提高系统的伸缩性,因此当你对响应式编程非常熟练的情况下并将其应用于新的系统中,还是值得考虑的,否则还是老老实实的使用WebMVC吧 三、Reactive Spring Web 在这里定义了最基本的服务端接口:HttpHandler和WebHandler HttpHandler HttpHandler定义了最基本的处理Http请求行为,这个接口主要作用是处理Http请求并将结果做出响应,下面这个表格是说明了Server API的使用方式及何种方式进行响应式流支持的: Server name Server API used Reactive Streams support Netty Netty API Reactor Netty Undertow Undertow API spring-web: Undertow to Reactive Streams bridge Tomcat Servlet 3.1 non-blocking I/O; Tomcat API to read and write ByteBuffers vs byte[] spring-web: Servlet 3.1 non-blocking I/O to Reactive Streams bridge Jetty Servlet 3.1 non-blocking I/O; Jetty API to write ByteBuffers vs byte[] spring-web: Servlet 3.1 […]
View Details