单点登录SSO解决方案之SpringSecurity+JWT实现

本文介绍了如何使用SpringSecurity和JWT技术实现单点登录(SSO)解决方案。首先,详细展示了如何生成RSA密钥对,然后在认证服务中配置相关依赖、创建配置文件、设置公钥私钥以及实现数据认证逻辑。接着,自定义了认证过滤器和校验token的过滤器。最后,在资源服务中仅使用公钥进行验证,确保SSO的正常运作。
摘要由CSDN通过智能技术生成
  • @param publicKeyFilename 公钥文件路径

  • @param privateKeyFilename 私钥文件路径

  • @param secret 生成密钥的密文

*/

public static void generateKey(String publicKeyFilename, String privateKeyFilename, String secret, int keySize) throws Exception {

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(“RSA”);

SecureRandom secureRandom = new SecureRandom(secret.getBytes());

keyPairGenerator.initialize(Math.max(keySize, DEFAULT_KEY_SIZE), secureRandom);

KeyPair keyPair = keyPairGenerator.genKeyPair();

// 获取公钥并写出

byte[] publicKeyBytes = keyPair.getPublic().getEncoded();

publicKeyBytes = Base64.getEncoder().encode(publicKeyBytes);

writeFile(publicKeyFilename, publicKeyBytes);

// 获取私钥并写出

byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();

privateKeyBytes = Base64.getEncoder().encode(privateKeyBytes);

writeFile(privateKeyFilename, privateKeyBytes);

}

private static byte[] readFile(String fileName) throws Exception {

return Files.readAllBytes(new File(fileName).toPath());

}

private static void writeFile(String destPath, byte[] bytes) throws IOException {

File dest = new File(destPath);

if (!dest.exists()) {

dest.createNewFile();

}

Files.write(dest.toPath(), bytes);

}

}

在通用子模块中编写测试类生成rsa公钥和私钥

/**

  • @program: springboot-54-security-jwt-demo

  • @description:

  • @author: 波波烤鸭

  • @create: 2019-12-03 11:08

*/

public class JwtTest {

private String privateKey = “c:/tools/auth_key/id_key_rsa”;

private String publicKey = “c:/tools/auth_key/id_key_rsa.pub”;

@Test

public void test1() throws Exception{

RsaUtils.generateKey(publicKey,privateKey,“dpb”,1024);

}

}

在这里插入图片描述

2.3认证系统创建


接下来我们创建我们的认证服务。

在这里插入图片描述

导入相关的依赖

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-security

security-jwt-common

com.dpb

1.0-SNAPSHOT

mysql

mysql-connector-java

5.1.47

org.mybatis.spring.boot

mybatis-spring-boot-starter

2.1.0

com.alibaba

druid

1.1.10

org.springframework.boot

spring-boot-configuration-processor

true

创建配置文件

spring:

datasource:

driver-class-name: com.mysql.jdbc.Driver

url: jdbc:mysql://localhost:3306/srm

username: root

password: 123456

type: com.alibaba.druid.pool.DruidDataSource

mybatis:

type-aliases-package: com.dpb.domain

mapper-locations: classpath:mapper/*.xml

logging:

level:

com.dpb: debug

rsa:

key:

pubKeyFile: c:\tools\auth_key\id_key_rsa.pub

priKeyFile: c:\tools\auth_key\id_key_rsa

在这里插入图片描述

提供公钥私钥的配置类

package com.dpb.config;

import com.dpb.utils.RsaUtils;

import lombok.Data;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

import java.security.PrivateKey;

import java.security.PublicKey;

/**

  • @program: springboot-54-security-jwt-demo

  • @description:

  • @author: 波波烤鸭

  • @create: 2019-12-03 11:25

*/

@Data

@ConfigurationProperties(prefix = “rsa.key”)

public class RsaKeyProperties {

private String pubKeyFile;

private String priKeyFile;

private PublicKey publicKey;

private PrivateKey privateKey;

/**

  • 系统启动的时候触发

  • @throws Exception

*/

@PostConstruct

public void createRsaKey() throws Exception {

publicKey = RsaUtils.getPublicKey(pubKeyFile);

privateKey = RsaUtils.getPrivateKey(priKeyFile);

}

}

创建启动类

/**

  • @program: springboot-54-security-jwt-demo

  • @description: 启动类

  • @author: 波波烤鸭

  • @create: 2019-12-03 11:23

*/

@SpringBootApplication

@MapperScan(“com.dpb.mapper”)

@EnableConfigurationProperties(RsaKeyProperties.class)

public class App {

public static void main(String[] args) {

SpringApplication.run(App.class,args);

}

}

完成数据认证的逻辑

pojo

package com.dpb.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.Data;

import org.springframework.security.c

Spring Security JWT实现是指使用JWT(JSON Web Token)作为身份验证和授权机制的Spring Security解决方案Spring SecurityJWT提供了自动化配置,使得使用JWT进行身份验证和授权变得更加简单和高效。通过配置JwtAuthenticationTokenFilter,可以实现JWT的验证和解析。同时,可以通过RestfulAccessDeniedHandler和RestAuthenticationEntryPoint来处理登录校验和权限校验的逻辑。使用JWT实现Spring Security解决方案可以提供更加强大和灵活的身份验证和授权功能。 [2 [3<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [厉害,我带的实习生仅用四步就整合好SpringSecurity+JWT实现登录认证](https://blog.csdn.net/qing_gee/article/details/124016059)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [单点登录SSO解决方案SpringSecurity+JWT实现.docx](https://download.csdn.net/download/njbaige/34581331)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值