常见问题
常见问题
如何不登录直接访问
登录nacos在配置管理中配置列表,修改ruoyi-gateway-dev.yml
在ignore中设置whites,表示允许匿名访问
# 不校验白名单
security:
ignore:
whites:
- /auth/logout
- /auth/login
- /*/v2/api-docs
- /csrf
如何获取用户登录信息
后端获取当前用户信息
// 获取当前用户名
String username = SecurityUtils.getUsername();
// 获取当前用户ID
Long userid = SecurityUtils.getUserId();
// 获取当前的用户信息
LoginUser loginUser = SecurityUtils.getLoginUser();
vue中获取当前用户信息
// 获取当前用户名
var username = this.$store.state.user.name;
如何更换项目包路径
可以使用若依框架包名修改器一键替换。
如何设置令牌有效期
可以在ruoyi-common-core模块中的com.ruoyi.common.core.constant.CacheConstants类中设置。
/**
* 缓存有效期,默认720(分钟)
*/
public final static long EXPIRATION = 720;
/**
* 缓存刷新时间,默认120(分钟)
*/
public final static long REFRESH_TIME = 120;
提示您没有数据的权限
这种情况都属于权限标识配置不对在菜单管理配置好权限标识(菜单&按钮)
- 确认此用户是否已经配置角色
- 确认此角色是否已经配置菜单权限
- 确认此菜单权限标识是否和后台代码一致
如参数管理
后台配置@RequiresPermissions("system:config:list")对应参数管理权限标识为system:config:list
注:如需要角色权限,配置角色权限字符 使用@RequiresRoles("admin")
系统接口不显示对应服务
访问swagger右上角服务列表没有展示新增服务,确认是否在ruoyi-gateway-dev.yml配置了对应服务的路由。
配置示例
spring:
cloud:
gateway:
routes:
# xxxx服务
- id: ruoyi-xxxx
uri: lb://ruoyi-xxxx
predicates:
- Path=/xxxx/**
filters:
- StripPrefix=1
因为swagger右上角服务列表默认是读取的gateway中的routes服务列表。
登录页面如何不显示验证码
登录nacos在配置管理中配置列表,修改ruoyi-gateway-dev.yml
在captcha中设置enabled属性(true开启、false关闭)
# 验证码
security:
captcha:
enabled: false
登录密码如何使用加密传输
目前登录接口密码是明文传输,如果安全性有要求,可以调整成加密方式传输。参考如下
1、修改前端login.js对密码进行rsa加密。
import { encrypt } from '@/utils/jsencrypt'
export function login(username, password, code, uuid) {
password = encrypt(password);
.........
}
2、工具类sign包下添加RsaUtils.java,用于RSA加密解密。
package com.ruoyi.common.core.utils.sign;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
/**
* RSA加密解密
*
* @author ruoyi
**/
public class RsaUtils
{
// Rsa 私钥
public static String privateKey = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY"
+ "7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKN"
+ "PuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gA"
+ "kM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWow"
+ "cSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99Ecv"
+ "DQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthh"
+ "YhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3"
+ "UP8iWi1Qw0Y=";
/**
* 私钥解密
*
* @param privateKeyString 私钥
* @param text 待解密的文本
* @return 解密后的文本
*/
public static String decryptByPrivateKey(String text) throws Exception
{
return decryptByPrivateKey(privateKey, text);
}
/**
* 公钥解密
*
* @param publicKeyString 公钥
* @param text 待解密的信息
* @return 解密后的文本
*/
public static String decryptByPublicKey(String publicKeyString, String text) throws Exception
{
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyString));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] result = cipher.doFinal(Base64.decodeBase64(text));
return new String(result);
}
/**
* 私钥加密
*
* @param privateKeyString 私钥
* @param text 待加密的信息
* @return 加密后的文本
*/
public static String encryptByPrivateKey(String privateKeyString, String text) throws Exception
{
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(text.getBytes());
return Base64.encodeBase64String(result);
}
/**
* 私钥解密
*
* @param privateKeyString 私钥
* @param text 待解密的文本
* @return 解密后的文本
*/
public static String decryptByPrivateKey(String privateKeyString, String text) throws Exception
{
PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(Base64.decodeBase64(text));
return new String(result);
}
/**
* 公钥加密
*
* @param publicKeyString 公钥
* @param text 待加密的文本
* @return 加密后的文本
*/
public static String encryptByPublicKey(String publicKeyString, String text) throws Exception
{
X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyString));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] result = cipher.doFinal(text.getBytes());
return Base64.encodeBase64String(result);
}
/**
* 构建RSA密钥对
*
* @return 生成后的公私钥信息
*/
public static RsaKeyPair generateKeyPair() throws NoSuchAlgorithmException
{
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
String publicKeyString = Base64.encodeBase64String(rsaPublicKey.getEncoded());
String privateKeyString = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
return new RsaKeyPair(publicKeyString, privateKeyString);
}
/**
* RSA密钥对对象
*/
public static class RsaKeyPair
{
private final String publicKey;
private final String privateKey;
public RsaKeyPair(String publicKey, String privateKey)
{
this.publicKey = publicKey;
this.privateKey = privateKey;
}
public String getPublicKey()
{
return publicKey;
}
public String getPrivateKey()
{
return privateKey;
}
}
}
3、登录方法TokenController.java,对密码进行rsa解密。
@PostMapping("login")
public R<?> login(@RequestBody LoginBody form) throws Exception
{
// 用户登录
LoginUser userInfo = sysLoginService.login(form.getUsername(), RsaUtils.decryptByPrivateKey(form.getPassword()));
// 获取登录token
return R.ok(tokenService.createToken(userInfo));
}
访问 http://localhost/login 登录页面。提交时检查密码是否为加密传输,且后台也能正常解密。
特殊字符串被过滤的解决办法
默认所有的都会过滤脚本,可以在ruoyi-gateway-dev.yml配置xss.excludeUrls属性排除URL
# 安全配置
security:
xss:
enabled: true
excludeUrls:
- /system/notice
如何区分不同环境下配置文件
当在多配置文件中,需要切换配置文件时,通常的做法都是修改激活的文件名称,而spring.profiles.active=@profiles.active@是配合maven profile进行选择不同配置文件进行启动,可以避免修改文件,而在maven打包是指定使用哪个配置文件。
1、配置pom.xml,定义不同环境配置属性。
<profiles>
<profile>
<!-- 本地环境 -->
<id>dev</id>
<properties>
<spring.profile>dev</spring.profile>
<nacos.server.address>127.0.0.1:8848</nacos.server.address>
</properties>
<activation>
<!-- 是否默认激活 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<spring.profile>test</spring.profile>
<nacos.server.address>120.120.120.120:8848</nacos.server.address>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<!-- 生产环境 -->
<id>prod</id>
<properties>
<spring.profile>prod</spring.profile>
<nacos.server.address>http://ruoyi.vip:8848</nacos.server.address>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
2、修改对应的配置文件,示例如下。
# Tomcat
server:
port: 9201
# Spring
spring:
application:
# 应用名称
name: ruoyi-system
profiles:
# 环境配置
active: @spring.profile@
cloud:
nacos:
discovery:
# 服务注册地址
server-addr: @nacos.server.address@
config:
# 配置中心地址
server-addr: @nacos.server.address@
# 配置文件格式
file-extension: yml
# 共享配置
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
3、打包测试。
mvn clean package -P dev
mvn clean package -P test
mvn clean package -P prod
打包成功后会进行对应的替换,例如使用test环境打包,配置文件的@nacos.server.address@会被替换成pom.xml测试配置环境变量nacos.server.address值120.120.120.120:8848。
Druid更多项目常见问题
Nacos更多项目常见问题
Seata更多项目常见问题
Sentinel更多项目常见问题
RuoYi更多项目常见问题查询
微服务版本问题和不分离版本大多数雷同。
RuoYi-Vue更多项目常见问题查询
微服务版本问题和分离版本大多数雷同。
