首页 > 其他分享 >2024年5月6日第四十四篇

2024年5月6日第四十四篇

时间:2024-05-06 19:58:45浏览次数:15  
标签:第四十四 Authentication springframework 2024 authentication org import security

今天完成了接祖作业中后端的开发,并着重对jwt令牌认证进行了学习。

Controller的函数的参数里面 加一个

Authentication authentication

然后调用

authentication.getName()

就能获取到用户名,查找使用即可

package com.example.stdudemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.jwt.JwtClaimsSet;
import org.springframework.security.oauth2.jwt.JwtEncoder;
import org.springframework.security.oauth2.jwt.JwtEncoderParameters;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
public class TokenController {
    @Autowired
    JwtEncoder encoder;
    @PostMapping("/api/token")
    public String token(Authentication authentication)  {
        Instant now = Instant.now();
        long expiry = 3600000L;
        String scope = authentication.getAuthorities().stream()
                .map(GrantedAuthority::getAuthority)
                .collect(Collectors.joining(" "));
        JwtClaimsSet claims = JwtClaimsSet.builder()
                .issuer("self")
                .issuedAt(now)
                .expiresAt(now.plusSeconds(expiry))
                .subject(authentication.getName())
                .claim("scope", scope)
                .build();
        return "\"" +this.encoder.encode(JwtEncoderParameters.from(claims)).getTokenValue() + "\"";
    }
}

使用Rester的Authentication选项中,添加Basic Auth,填入用户名和密码,访问token controller

标签:第四十四,Authentication,springframework,2024,authentication,org,import,security
From: https://www.cnblogs.com/xuechenhao173/p/18175726

相关文章

  • Testing Egineer note:2024_5_5-day05-part01
    版本控制器之svn介绍1.svn介绍(版本控制工具)1、svn的定义:svn是一个开放源代码的版本控制系统,通过采用分支管理系统的高效管理,简而言之就是用于多个人共同开发同一个项目,实现共享资源,实现最终集中式个管理。2.snv的作用:在项目中对需求规格说明书,测试用例,代码,以及项目项目的文件......
  • 云原生周刊:Terraform 1.8 发布 | 2024.5.6
    开源项目推荐xlskubectl用于控制Kubernetes集群的电子表格。xlskubectl将GoogleSpreadsheet与Kubernetes集成。你可以通过用于跟踪费用的同一电子表格来管理集群。git-syncgit-sync是一个简单的命令,它将git存储库拉入本地目录,等待一段时间,然后重复。当远程存储库......
  • 腾讯公益赛个人冲刺博客10(2024.5.6)
    今天测试多人联机整体效果    ......
  • 腾讯公益赛个人冲刺博客7(2024.5.1)
    今天处理sos的定位功能,但自动定位功能不稳定,需要手动定位importandroid.Manifest;importandroid.content.pm.PackageManager;importandroid.os.Bundle;importandroid.widget.Toast;importandroidx.annotation.NonNull;importandroidx.appcompat.app.AppCompatActivit......
  • MyBatis笔记2024-05-06
    MyBatis笔记第1章--MyBatis日志管理与动态SQL日志门面(统一调用接口2两种)与实现(常见:log4j、logback、java.util.logging...)LoggingFacadeForJavaApacheCommons-logs增加依赖:ch.qos.logbackMyBatis会自动调用logback配置文件:logback.xml固定文件名配置内容:指定类,输出格式,日志......
  • 腾讯公益赛个人冲刺博客2(2024.4.24)
    登录注册页面,问题暂无,明天做帮扶的第一个界面<?xmlversion="1.0"encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.andro......
  • 腾讯公益赛个人冲刺博客3(2024.4.25)
    今天做了帮扶功能的添加界面的雏形,以后可能还需要完善问题:一开始在想用不用网格布局,后来结合着改了一下明天做后端<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android......
  • 腾讯公益赛个人冲刺博客4(2024.4.26)
    今天完善了帮扶程序的后端,实现了后端的增和查询packagecom.example.helppeople;importandroid.annotation.SuppressLint;importandroid.content.Intent;importandroid.os.Bundle;importandroidx.appcompat.app.AppCompatActivity;importcom.example.helppeople.R;......
  • 腾讯公益赛个人冲刺博客5(2024.4.29)
    今天调整了帮扶功能前端页面,略微比之前好看了一些<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:to......
  • 腾讯公益赛冲刺个人博客1(2024.4.23)
    今天确定了组内第一阶段的任务是基本完成sos和帮扶两个核心功能以及登录注册等常规功能完成登录和注册的基本页面以及数据库的设计packagecom.example.helppeople.entity;publicclassStudent{privateStringid;privateStringname;privateStringphon......