Skip to content

Commit 7066021

Browse files
committed
Signed-off-by: 徐韦 <[email protected]>
1 parent bc81bfc commit 7066021

File tree

46 files changed

+2194
-1245
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2194
-1245
lines changed

H5/src/components/HeaderBar/index.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
<script>
3535
import mgr from '../../plugin/oidc'
36+
import base from '../../plugin/base64'
3637
export default {
3738
name: "home",
3839
props: {
@@ -52,9 +53,12 @@
5253
},
5354
5455
mounted() {
55-
this.$http.get("/base/api/User/Current").then(u=>{
56-
this.username = u.name;
57-
})
56+
var payload = base.getpayload();
57+
this.username = payload.name;
58+
console.log(payload);
59+
// this.$http.get("/base/api/User/Current").then(u=>{
60+
// this.username = u.name;
61+
// })
5862
},
5963
methods: {
6064
handleCommand(item){

H5/src/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ Vue.use(http);
1818

1919
import './mixin'
2020
import mgr from './plugin/oidc'
21+
import base from './plugin/base64'
2122

2223
var main = async ()=>{
2324
var user = await mgr.getUser();
24-
if (user){
25+
if (user&&user.expired===false){
26+
console.log(user);
27+
http.http.setheader(user.access_token);
28+
base.user = user;
2529
new Vue({
2630
router,
2731
store,

H5/src/plugin/base64.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
export default {
2+
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
3+
// private property
4+
// public method for encoding
5+
encode(input) {
6+
var output = "";
7+
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
8+
var i = 0;
9+
input = this._utf8_encode(input);
10+
while (i < input.length) {
11+
chr1 = input.charCodeAt(i++);
12+
chr2 = input.charCodeAt(i++);
13+
chr3 = input.charCodeAt(i++);
14+
enc1 = chr1 >> 2;
15+
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
16+
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
17+
enc4 = chr3 & 63;
18+
if (isNaN(chr2)) {
19+
enc3 = enc4 = 64;
20+
} else if (isNaN(chr3)) {
21+
enc4 = 64;
22+
}
23+
output = output +
24+
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
25+
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
26+
}
27+
return output;
28+
},
29+
// public method for decoding
30+
decode(input) {
31+
var output = "";
32+
var chr1, chr2, chr3;
33+
var enc1, enc2, enc3, enc4;
34+
var i = 0;
35+
input = input.replace('-', '+'); // 62nd char of encoding
36+
input = input.replace('_', '/'); // 63rd char of encoding
37+
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
38+
while (i < input.length) {
39+
enc1 = this._keyStr.indexOf(input.charAt(i++));
40+
enc2 = this._keyStr.indexOf(input.charAt(i++));
41+
enc3 = this._keyStr.indexOf(input.charAt(i++));
42+
enc4 = this._keyStr.indexOf(input.charAt(i++));
43+
chr1 = (enc1 << 2) | (enc2 >> 4);
44+
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
45+
chr3 = ((enc3 & 3) << 6) | enc4;
46+
output = output + String.fromCharCode(chr1);
47+
if (enc3 != 64&&chr2) {
48+
output = output + String.fromCharCode(chr2);
49+
}
50+
if (enc4 != 64&&chr3) {
51+
output = output + String.fromCharCode(chr3);
52+
}
53+
}
54+
output = this._utf8_decode(output);
55+
var result = output;
56+
return result;
57+
},
58+
// private method for UTF-8 encoding
59+
_utf8_encode(string) {
60+
string = string.replace(/\r\n/g, "\n");
61+
var utftext = "";
62+
for (var n = 0; n < string.length; n++) {
63+
var c = string.charCodeAt(n);
64+
if (c < 128) {
65+
utftext += String.fromCharCode(c);
66+
} else if ((c > 127) && (c < 2048)) {
67+
utftext += String.fromCharCode((c >> 6) | 192);
68+
utftext += String.fromCharCode((c & 63) | 128);
69+
} else {
70+
utftext += String.fromCharCode((c >> 12) | 224);
71+
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
72+
utftext += String.fromCharCode((c & 63) | 128);
73+
}
74+
75+
}
76+
return utftext;
77+
},
78+
// private method for UTF-8 decoding
79+
_utf8_decode(utftext) {
80+
var string = "";
81+
var i = 0;
82+
var c = 0, c1 = 0, c2 = 0, c3 = 0;
83+
while (i < utftext.length) {
84+
c = utftext.charCodeAt(i);
85+
if (c < 128) {
86+
string += String.fromCharCode(c);
87+
i++;
88+
} else if ((c > 191) && (c < 224)) {
89+
c2 = utftext.charCodeAt(i + 1);
90+
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
91+
i += 2;
92+
} else {
93+
c2 = utftext.charCodeAt(i + 1);
94+
c3 = utftext.charCodeAt(i + 2);
95+
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
96+
i += 3;
97+
}
98+
}
99+
return string;
100+
},
101+
getpayload(){
102+
const cargs = this.user.access_token.split('.');
103+
if (cargs.length != 3) throw '非法token';
104+
return JSON.parse(this.decode(cargs[1])) ;
105+
},
106+
user:null
107+
}

H5/src/plugin/http.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,10 @@ var http = {
149149
if(token) this.header.Authorization = "Bearer " + token;
150150
else this.header.Authorization = null;
151151
},
152-
async get(url) {
152+
async get(url,config) {
153+
if (!config) config = { headers: this.header };
153154
this.block();
154-
const res = await axios.get(url).catch(res => res);
155+
const res = await axios.get(url,config).catch(res => res);
155156
this.unblock();
156157
return handleResult(res);
157158

H5/src/plugin/oidc.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import Oidc from 'oidc-client'
22

33
var config = {
4-
authority: "http://localhost:5000",
5-
client_id: "js",
6-
redirect_uri: "http://localhost:8080/callback.html",
4+
authority: "https://localhost:44320",
5+
client_id: "base",
6+
redirect_uri: window.location.origin+"/callback.html",
77
response_type: "id_token token",
88
scope:"openid profile base",
9-
post_logout_redirect_uri : "http://localhost:8080"
9+
post_logout_redirect_uri : window.location.origin
1010
};
1111

1212
// var config = {

H5/src/views/Tabs/ClientEdit.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
<el-form-item label="访问token">
2020
<el-switch v-model="data.allowAccessTokensViaBrowser"></el-switch>
2121
</el-form-item>
22+
<el-form-item label="授权提醒">
23+
<el-switch v-model="data.RequireConsent"></el-switch>
24+
</el-form-item>
25+
<el-form-item label="允许本地登入">
26+
<el-switch v-model="data.EnableLocalLogin"></el-switch>
27+
</el-form-item>
2228
<el-form-item label="登入跳转" prop='redirectUris'>
2329
<el-input size='mini' v-model="data.redirectUris[0]"></el-input>
2430
</el-form-item>
@@ -62,6 +68,8 @@
6268
allowedGrantTypes:[],
6369
allowedScopes:[],
6470
allowAccessTokensViaBrowser:false,
71+
requireConsent:false,
72+
enableLocalLogin:true,
6573
redirectUris:[""],
6674
postLogoutRedirectUris:[""],
6775
allowedCorsOrigins:[""]

H5/vue.config.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ module.exports = {
99
// target: 'http://192.168.103.251:6006'
1010
// }
1111
// }
12+
https: true,
13+
// host: 'localhost', // can be overwritten by process.env.HOST
14+
port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
1215
proxy: {
13-
'/connect': {
14-
target: 'http://localhost:5000'
15-
},
1616
'/base': {
17-
target: 'http://localhost:5000'
17+
target: 'https://localhost:44320'
1818
}
19-
}
19+
},
20+
// setup: (app) => { //解决post没响应的问题
21+
// app.post('/public/**', function(req, res) {
22+
// res.redirect(req.originalUrl); //重定向到对应路径
23+
// });
24+
// }
2025
}
2126
}

QuickstartIdentityServer/Apis/ApiDTO/ValidatePermissionDTO.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class ValidatePermissionDTO
1010
/// <summary>
1111
/// 用户id
1212
/// </summary>
13-
public int UserId { get; set; }
13+
public string Account { get; set; }
1414
/// <summary>
1515
/// 系统code
1616
/// </summary>

QuickstartIdentityServer/Apis/ApiResourceController.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010
using Microsoft.EntityFrameworkCore;
1111
using QuickstartIdentityServer.Apis.ApiDTO;
1212
using QuickstartIdentityServer.CommonDTO;
13+
using QuickstartIdentityServer.Filters;
1314
using QuickstartIdentityServer.IdsAuthorization;
1415

1516
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
1617

1718
namespace QuickstartIdentityServer.Apis
1819
{
19-
[Authorize]
20+
[Authorize(Roles ="admin")]
2021
[Route("base/api/[controller]/[action]")]
2122
[ApiController]
22-
public class ApiResourceController : Controller
23+
[WebApiExceptionFilter]
24+
public class ApiResourceController : ControllerBase
2325
{
2426
ConfigurationDbContext context;
2527
/// <summary>
@@ -82,7 +84,8 @@ public async Task Update([FromBody]ApiResourceDTO input)
8284
var apiresource = await context.ApiResources.FirstOrDefaultAsync(x => x.Name == input.Name);
8385
if (apiresource == null) throw new Exception("不存在该资源");
8486
context.Entry(apiresource).State = EntityState.Deleted;
85-
var add = (new ApiResource(input.Name,input.DisplayName)).ToEntity();
87+
var ar = new ApiResource(input.Name, input.DisplayName);
88+
var add = ar.ToEntity();
8689
context.ApiResources.Add(add);
8790
await context.SaveChangesAsync();
8891
}

QuickstartIdentityServer/Apis/AppController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313

1414
namespace QuickstartIdentityServer.Apis
1515
{
16-
[Authorize]
16+
[Authorize(Roles = "admin")]
1717
[Route("base/api/[controller]/[action]")]
1818
[ApiController]
19+
[WebApiExceptionFilter]
1920
public class AppController : ControllerBase
2021
{
2122
PermissionConext pcontext;

QuickstartIdentityServer/Apis/ClientController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,24 @@
55
using IdentityServer4.EntityFramework.DbContexts;
66
using IdentityServer4.EntityFramework.Mappers;
77
using IdentityServer4.Models;
8-
using Microsoft.AspNetCore.Authentication.JwtBearer;
98
using Microsoft.AspNetCore.Authorization;
109
using Microsoft.AspNetCore.Http;
1110
using Microsoft.AspNetCore.Mvc;
1211
using Microsoft.EntityFrameworkCore;
1312
using QuickstartIdentityServer.Apis.ApiDTO;
1413
using QuickstartIdentityServer.CommonDTO;
14+
using QuickstartIdentityServer.Filters;
1515
using QuickstartIdentityServer.IdsAuthorization;
1616

1717
namespace QuickstartIdentityServer.Apis
1818
{
1919
/// <summary>
2020
/// Clients controller.
2121
/// </summary>
22-
[Authorize]
22+
[Authorize(Roles = "admin")]
2323
[Route("base/api/[controller]/[action]")]
2424
[ApiController]
25+
[WebApiExceptionFilter]
2526
public class ClientController : ControllerBase
2627
{
2728
ConfigurationDbContext context;

QuickstartIdentityServer/Apis/RoleController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313

1414
namespace QuickstartIdentityServer.Apis
1515
{
16-
[Authorize]
16+
[Authorize(Roles = "admin")]
1717
[Route("base/api/[controller]/[action]")]
1818
[ApiController]
19+
[WebApiExceptionFilter]
1920
public class RoleController : ControllerBase
2021
{
2122
PermissionConext pcontext;

QuickstartIdentityServer/Apis/UserController.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Security.Claims;
66
using System.Threading.Tasks;
7+
using Microsoft.AspNetCore.Authentication.Cookies;
78
using Microsoft.AspNetCore.Authorization;
89
using Microsoft.AspNetCore.Http;
910
using Microsoft.AspNetCore.Mvc;
@@ -15,9 +16,10 @@
1516

1617
namespace QuickstartIdentityServer.Apis
1718
{
18-
[Authorize]
19+
[Authorize(Roles = "admin")]
1920
[Route("base/api/[controller]/[action]")]
2021
[ApiController]
22+
[WebApiExceptionFilter]
2123
public class UserController : ControllerBase
2224
{
2325
PermissionConext pcontext;
@@ -33,8 +35,7 @@ public UserController(PermissionConext pcontext)
3335
public async Task<UserNameDTO> Current()
3436
{
3537
var subid = User.Claims.FirstOrDefault(c => c.Type == JwtRegisteredClaimNames.Sub).Value;
36-
int.TryParse(subid, out int userid);
37-
var user = await pcontext.User.Where(u => u.Id == userid).Select(u=>new UserNameDTO {
38+
var user = await pcontext.User.Where(u => u.Account == subid).Select(u=>new UserNameDTO {
3839
Name = u.Name
3940
}).FirstOrDefaultAsync();
4041
return user;
@@ -156,11 +157,11 @@ public async Task SetRole([FromQuery]int userid, [FromBody] int[] roleids)
156157
[AllowAnonymous]
157158
public async Task<bool> Validate([FromBody]ValidatePermissionDTO input)
158159
{
159-
bool isappadmin = await (from u in pcontext.User.Where(u => u.Id == input.UserId)
160+
bool isappadmin = await (from u in pcontext.User.Where(u => u.Account == input.Account)
160161
join urm in pcontext.UserRoleMap on u.Id equals urm.UserId
161162
join ra in pcontext.RoleAppAdmin.Where(m => m.Code == input.Code) on urm.RoleId equals ra.RoleId select 1).AnyAsync();
162163
if (isappadmin) return true;
163-
bool haspermission = await (from u in pcontext.User.Where(u => u.Id == input.UserId)
164+
bool haspermission = await (from u in pcontext.User.Where(u => u.Account == input.Account)
164165
join urm in pcontext.UserRoleMap on u.Id equals urm.UserId
165166
join rmp in pcontext.RolePermissionMap.Where(m => m.Code == input.Code) on urm.RoleId equals rmp.RoleId
166167
join p in pcontext.Permission.Where(per=>per.ControllerName==input.Controller&&per.ActionName==input.Action) on rmp.PermissionId equals p.Id

0 commit comments

Comments
 (0)