Skip to content

Commit 9243db9

Browse files
committed
feat:swagger增加权限
1 parent 5f7d016 commit 9243db9

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed

Blog.Core.Api/Blog.Core.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Blog.Core.Api/Controllers/LoginController.cs

+25
Original file line numberDiff line numberDiff line change
@@ -266,5 +266,30 @@ public string Md5Password(string password = "")
266266
{
267267
return MD5Helper.MD5Encrypt32(password);
268268
}
269+
270+
/// <summary>
271+
/// swagger登录
272+
/// </summary>
273+
/// <param name="loginRequest"></param>
274+
/// <returns></returns>
275+
[HttpPost]
276+
[Route("swgLogin")]
277+
public dynamic SwgLogin([FromBody] SwaggerLoginRequest loginRequest)
278+
{
279+
// 这里可以查询数据库等各种校验
280+
if (loginRequest?.name == "admin" && loginRequest?.pwd == "admin")
281+
{
282+
HttpContext.Session.SetString("swagger-code", "success");
283+
return new { result = true };
284+
}
285+
286+
return new { result = false };
287+
}
288+
}
289+
290+
public class SwaggerLoginRequest
291+
{
292+
public string name { get; set; }
293+
public string pwd { get; set; }
269294
}
270295
}

Blog.Core.Api/Startup.cs

+5
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public void ConfigureServices(IServiceCollection services)
9292

9393
services.Configure<KestrelServerOptions>(x => x.AllowSynchronousIO = true)
9494
.Configure<IISServerOptions>(x => x.AllowSynchronousIO = true);
95+
96+
services.AddDistributedMemoryCache();
97+
services.AddSession();
9598

9699
services.AddControllers(o =>
97100
{
@@ -165,6 +168,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, MyContex
165168
//app.UseHsts();
166169
}
167170

171+
app.UseSession();
172+
app.UseSwaggerAuthorized();
168173
// 封装Swagger展示
169174
app.UseSwaggerMildd(() => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("Blog.Core.Api.index.html"));
170175

Blog.Core.Api/wwwroot/swg-login.html

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>默认首页</title>
6+
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
7+
8+
</head>
9+
<body>
10+
<div id="requestMsg"></div>
11+
<div style="text-align: center;">
12+
<p>用户名:admin,密码:admin</p>
13+
<input id="name" placeholder="name" type="text" />
14+
<br />
15+
<input id="pwd" placeholder="pwd" type="password" />
16+
<br />
17+
<input type="submit" onclick="submit()" value="submit" />
18+
</div>
19+
<script>
20+
function submit() {
21+
let postdata = {
22+
"name": $("#name").val(),
23+
"pwd": $("#pwd").val(),
24+
};
25+
if (!(postdata.name && postdata.pwd)) {
26+
alert('参数不正确');
27+
return
28+
}
29+
$.ajax({
30+
url: "/api/Login/swgLogin",
31+
type: "POST",
32+
contentType: "application/json; charset=utf-8",
33+
data: JSON.stringify(postdata),
34+
dataType: 'json',
35+
success: function (data) {
36+
if (data?.result) {
37+
window.location.href = "/index.html";
38+
} else {
39+
alert('参数不正确');
40+
}
41+
}
42+
});
43+
}
44+
45+
</script>
46+
</body>
47+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Http;
3+
using System;
4+
using System.Net;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Blog.Core.Middlewares
9+
{
10+
public class SwaggerAuthMildd
11+
{
12+
13+
private readonly RequestDelegate next;
14+
15+
public SwaggerAuthMildd(RequestDelegate next)
16+
{
17+
this.next = next;
18+
}
19+
20+
public async Task InvokeAsync(HttpContext context)
21+
{
22+
// 也可以根据是否是本地做判断 IsLocalRequest
23+
if (context.Request.Path.Value.ToLower().Contains("index.html"))
24+
{
25+
// 判断权限是否正确
26+
if (IsAuthorized(context))
27+
{
28+
await next.Invoke(context);
29+
return;
30+
}
31+
32+
// 无权限,跳转swagger登录页
33+
context.Response.Redirect("/swg-login.html");
34+
}
35+
else
36+
{
37+
await next.Invoke(context);
38+
}
39+
}
40+
41+
public bool IsAuthorized(HttpContext context)
42+
{
43+
// 使用session模式
44+
// 可以使用其他的
45+
return context.Session.GetString("swagger-code") == "success";
46+
}
47+
48+
/// <summary>
49+
/// 判断是不是本地访问
50+
/// 本地不用swagger拦截
51+
/// </summary>
52+
/// <param name="context"></param>
53+
/// <returns></returns>
54+
public bool IsLocalRequest(HttpContext context)
55+
{
56+
if (context.Connection.RemoteIpAddress == null && context.Connection.LocalIpAddress == null)
57+
{
58+
return true;
59+
}
60+
if (context.Connection.RemoteIpAddress.Equals(context.Connection.LocalIpAddress))
61+
{
62+
return true;
63+
}
64+
if (IPAddress.IsLoopback(context.Connection.RemoteIpAddress))
65+
{
66+
return true;
67+
}
68+
return false;
69+
}
70+
}
71+
public static class SwaggerAuthorizeExtensions
72+
{
73+
public static IApplicationBuilder UseSwaggerAuthorized(this IApplicationBuilder builder)
74+
{
75+
return builder.UseMiddleware<SwaggerAuthMildd>();
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)