JWT的前端后端的各项配置(Vue3+webapi)

一、后端接口:WebApi

1.appsettings.json文件

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "Authentication": {
    "SecretKey": "JianKeEducationCRMAPI",
    "Issuer": "******",
    "Audience": "******"
  },
  "AllowedHosts": "*"
}

2.Startup.cs

需要的命名空间

using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;

ConfigureServices需要修改

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                  .AddJwtBearer(options =>
                  {
                      var secretByte = Encoding.UTF8.GetBytes(Configuration["Authentication:SecretKey"]);
                      options.TokenValidationParameters = new TokenValidationParameters()
                      {
                          ValidateIssuer = true,
                          ValidIssuer = Configuration["Authentication:Issuer"],

                          ValidateAudience = true,
                          ValidAudience = Configuration["Authentication:Audience"],

                          ValidateLifetime = true,

                          IssuerSigningKey = new SymmetricSecurityKey(secretByte)
                      };
                  });

Configure里必须有

app.UseAuthorization();

3.登录方法

[ApiController]
    [Route("api/[controller]/[action]")]
    public class UserController : ControllerBase
    {
        private readonly IConfiguration _configuration;
        public UserController(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        [HttpPost]
        public JsonResult Login([FromBody] UserModel userModel)
        //public string Login(string userName, string password)
        {
            if (userModel.userName=="1" && userModel.password=="1")
            {
                //var Permission = "Permission";
                var userId = "1";
                //验证用户名密码
                //创建JWT
                // header
                var signingAlgorithm = SecurityAlgorithms.HmacSha256;
                // payload
                var claims = new List<Claim>
            {
                // sub
                new Claim(JwtRegisteredClaimNames.Sub,userId),
                //new Claim(ClaimTypes.Role, "Admin")
            };
                // signiture
                var secretByte = Encoding.UTF8.GetBytes(_configuration["Authentication:SecretKey"]);//Authentication
                //var secretByte = Encoding.UTF8.GetBytes("dengfenglaiSecretKey");

                var signingKey = new SymmetricSecurityKey(secretByte);
                var signingCredentials = new SigningCredentials(signingKey, signingAlgorithm);

                var token = new JwtSecurityToken(
                    //颁发者
                    issuer: _configuration["Authentication:Issuer"],//"dengfneglai", //
                                                                    //接收者
                    audience: _configuration["Authentication:Audience"],//"zhuifengqu", //
                                                                        //自定义参数
                    claims,
                    notBefore: DateTime.UtcNow,
                    //过期时间
                    expires: DateTime.UtcNow.AddDays(1),
                    //expires: DateTime.UtcNow.AddMinutes(2),
                    //签名证书
                    signingCredentials
                );
                var tokenStr = new JwtSecurityTokenHandler().WriteToken(token);

                return new JsonResult(new JsonApi(tokenStr));
            }
            else
            {
                return new JsonResult(new JsonApi() { Success = false, Error = "用户名或密码错误", Code = "401" });
            }
        }

        [Serializable]
        public class UserModel
        {
            public string userName { get; set; }
            public string password { get; set; }

        }
    }

4.登录成功后需要跳转个人中心页面,调用个人中心方法

[Authorize(AuthenticationSchemes = "Bearer")]
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class UserInfoController : ControllerBase
    {
        [HttpGet]
        public IActionResult GetUserInfoById([FromQuery] int id)
        {

            var entityUserInfo = new UserInfo() {Id=id };
            var json = JsonSerializeHelper.Serialize(entityUserInfo);
            return new JsonResult(new JsonApi(json));
        }
    }

    public class UserInfo
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Description { get; set; }
    }

二、前端Vue

1.登录页面

<template>
<div id="App" class="login">
  {{this.$store.state.name}}
登录名:<input v-model="userName" type="text" placeholder="请输入用户名"/>
密码:<input v-model="password" type="password" placeholder="请输入密码"/>
<!-- <input v-model="message" /> -->
<button v-on:click="LoginClick">登录</button>
</div>
</template>
<script lang='ts'>
import Vue, { toRefs, reactive, defineComponent, createApp } from 'vue'
import axios from 'axios'
import router from '@/router/index'

 interface userData{
   userName:string
   password:string
   LoginClick:()=>void
 }
export default defineComponent({
  setup () {
    const data = reactive<userData>({
      userName: '',
      password: '',
      LoginClick: async () => {
        if (!data.userName) { alert('请输入用户名'); return }
        if (!data.password) { alert('请输入密码'); return }
        const result: any = await axios.post(process.env.VUE_APP_BASE_API + '/user/Login', {
          userName: data.userName,
          password: data.password
        })
        if (result.success === true) {
          console.log(result, 'result')
          localStorage.setItem('Authentication', result.data)
          router.push({ path: '/UserCenter' })
        } else {
          alert(result.error)
        }
      }
    })
    return {
      ...toRefs(data)
    }
  }
})

</script>

2.个人中心页

<template>
<div id="App">
个人中心
</div>
</template>
<script lang=ts>
import { createApp, defineComponent, reactive, toRefs } from 'vue'
import axios from 'axios'
// import baseUrl from 'baseUrl'
interface userData{
  userId:string
   userName:string
   password:string
   GetUserInfoById:()=>void
 }
export default defineComponent({
  name: 'userCenter',
  setup () {
    const data = reactive<userData>({
      userId: '1',
      userName: '',
      password: '',
      GetUserInfoById: async () => {
        const result:any = await axios.get(process.env.VUE_APP_BASE_API + '/UserInfo/GetUserInfoById?id=1', {
          // id: data.userId
          // headers: {
          //   Authorization: 'Bearer ' + localStorage.getItem('Authentication')
          // }
        })
        if (result.success === true) {
          console.log(result, 'result')
        } else {
          alert(result.error)
        }
      }
    })
    return {
      ...toRefs(data)
    }
  },
  mounted: function () {
    this.GetUserInfoById()
  }
})
// console.log(localStorage.getItem('token'), 'userCenter.token')

</script>

3.main.ts

// import { config } from '@vue/test-utils'
import axios from 'axios'
import { createApp } from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store'

// 设置axios请求头加入token(Authentication)
axios.interceptors.request.use(config => {
  // if (config.push === '/') {} else {
  if (localStorage.getItem('Authentication')) {
    // 在请求头加入token(Authentication),名字要和后端接收请求头的token(Authentication)名字一样
    config.headers.Authorization = 'Bearer ' + localStorage.getItem('Authentication')
    console.log('Bearer ' + localStorage.getItem('Authentication'))
  }
  // }
  return config
}, error => {
  return Promise.reject(error)
})
//= ============================
// 响应回来token是否过期
axios.interceptors.response.use(res => {
  const data = res.data
  console.log('响应回来:' + data.code)
  // 和后端token失效返回码约定403
  if (data.code === '403') {
    alert('身份已失效')
    localStorage.removeItem('token')
    router.push({ name: 'Login' })
  } else {
    return data
  }
}, error => {
  return Promise.reject(error)
}
)
createApp(App).use(store).use(router).mount('#app')

4.router的index.ts

import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
// import UserCenter from '..views/UserCenter.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/Login',
    name: 'Login',
    component: Login
  },
  {
    path: '/UserCenter',
    name: 'UserCenter',
    // component: UserCenter
    component: () => import(/* webpackChunkName: "about" */ '../views/UserCenter.vue')
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 导航守卫
// 使用router.beforeEach注册一个全局前置守卫,判断用户是否登录
router.beforeEach((to, from, next) => {
  // to到哪儿 from从哪儿离开 next跳转 为空就是放行
  if (to.path === '/' || to.path === '/Login') {
    next()
  } else {
    const token = localStorage.getItem('Authentication')
    if (token == null || token === '') {
      console.log('请先登录')
      next({ name: 'Login' })
    } else {
      next()
    }
  }
})
export default router

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Netcore6.0是微软推出的全新版本的开发框架,它提供了强大且灵活的功能,用于构建Web应用程序和APIWeb API是Netcore6.0中的一项重要功能,它允许我们构建基于HTTP协议的API,并通过JSON格式进行数据交换。 JWT(JSON Web Token)是一种用于在网络应用间传递信息的安全方法。在Netcore6.0中,我们可以使用JWT来实现Web API的授权功能。JWT由三部分组成:头部、载荷和签名。头部包含了令牌的类型和算法,载荷包含了我们想要传递的数据,签名通过使用密钥进行加密来验证令牌的合法性。 在Netcore6.0中,我们可以使用Microsoft提供的Microsoft.AspNetCore.Authentication.JwtBearer包来简单地实现JWT的授权功能。首先,我们需要在Startup.cs文件的ConfigureServices方法中配置JWT的身份验证服务,并指定密钥、颁发者、验证等参数。然后,在Configure方法中启用身份验证中间件和JWT授权中间件。 在Vue3中,我们可以使用Axios库来发送HTTP请求并附带JWT令牌进行授权。Vue3是一种流行的JavaScript框架,用于构建现代化的用户界面。通过Axios,我们可以将JWT令牌添加到请求的Authorization头部中,并在后端接收到请求时进行验证。 为了实现Vue3与Netcore6.0的JWT授权,我们首先需要在Vue3项目中安装Axios库,并配置请求拦截器,在每个请求发送前将JWT令牌添加到请求头中。后端接收到带有JWT令牌的请求后,使用相同的密钥和算法进行解密并验证令牌的合法性。 综上所述,Netcore6.0的Web APIVue3的JWT授权组合,可以实现安全可靠的API授权。通过合理的配置和使用,我们可以保护API免受未经授权的访问,并确保只有经过身份验证的用户才能访问敏感数据或执行特定操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值