🔖 发布一个版本。 v 1.6.1
This commit is contained in:
1 parent
1d25c31e0c
commit
83005acfc1
29 files changed
+313
-104
No files matched your search
+35
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.security.feign;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/9/14
|
||||
* feign 租户信息拦截
|
||||
*/
|
||||
@Configuration
|
||||
public class PigxFeignTenantConfiguration {
|
||||
@Bean
|
||||
public RequestInterceptor PigxFeignTenantInterceptor() {
|
||||
return new PigxFeignTenantInterceptor();
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.security.feign;
|
||||
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstant;
|
||||
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/9/14
|
||||
*/
|
||||
@Slf4j
|
||||
public class PigxFeignTenantInterceptor implements RequestInterceptor {
|
||||
@Override
|
||||
public void apply(RequestTemplate requestTemplate) {
|
||||
if (SecurityUtils.getTenantId() == null) {
|
||||
log.error("TTL 中的 租户ID为空,feign拦截器 >> 增强失败");
|
||||
return;
|
||||
}
|
||||
requestTemplate.header(CommonConstant.TENANT_ID, SecurityUtils.getTenantId().toString());
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.security.filter;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstant;
|
||||
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.GenericFilterBean;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/9/13
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class TenantIdTtlFilter extends GenericFilterBean {
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||
HttpServletRequest request = (HttpServletRequest) servletRequest;
|
||||
HttpServletResponse response = (HttpServletResponse) servletResponse;
|
||||
|
||||
String tenantId = request.getHeader(CommonConstant.TENANT_ID);
|
||||
log.debug("获取header中的租户ID为:{}", tenantId);
|
||||
|
||||
if (StrUtil.isNotBlank(tenantId)) {
|
||||
SecurityUtils.setTenantId(Integer.parseInt(tenantId));
|
||||
} else {
|
||||
SecurityUtils.setTenantId(1);
|
||||
}
|
||||
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
}
|
||||
+10
-2
@@ -41,12 +41,19 @@ public class PigxUser extends User {
|
||||
@Getter
|
||||
private Integer deptId;
|
||||
|
||||
/**
|
||||
* 租户ID
|
||||
*/
|
||||
@Getter
|
||||
private Integer tenantId;
|
||||
|
||||
/**
|
||||
* Construct the <code>User</code> with the details required by
|
||||
* {@link DaoAuthenticationProvider}.
|
||||
*
|
||||
* @param id 用户ID
|
||||
* @param deptId 部门ID
|
||||
* @param tenantId 租户ID
|
||||
* @param username the username presented to the
|
||||
* <code>DaoAuthenticationProvider</code>
|
||||
* @param password the password that should be presented to the
|
||||
@@ -61,9 +68,10 @@ public class PigxUser extends User {
|
||||
* @throws IllegalArgumentException if a <code>null</code> value was passed either as
|
||||
* a parameter or as an element in the <code>GrantedAuthority</code> collection
|
||||
*/
|
||||
public PigxUser(Integer id, Integer deptId, String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
|
||||
public PigxUser(Integer id, Integer deptId, Integer tenantId, String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
|
||||
super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
|
||||
this.id = id;
|
||||
this.deptId = id;
|
||||
this.deptId = deptId;
|
||||
this.tenantId = tenantId;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -90,7 +90,7 @@ public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
|
||||
Set<String> dbAuthsSet = new HashSet<>();
|
||||
if (ArrayUtil.isNotEmpty(info.getRoles())) {
|
||||
// 获取角色
|
||||
Arrays.stream(info.getRoles()).forEach(role -> dbAuthsSet.add(SecurityConstants.ROLE + role));
|
||||
Arrays.stream(info.getRoles()).forEach(roleId -> dbAuthsSet.add(SecurityConstants.ROLE + roleId));
|
||||
// 获取资源
|
||||
dbAuthsSet.addAll(Arrays.asList(info.getPermissions()));
|
||||
|
||||
@@ -101,7 +101,7 @@ public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
|
||||
boolean enabled = StrUtil.equals(user.getDelFlag(), CommonConstant.STATUS_NORMAL);
|
||||
// 构造security用户
|
||||
|
||||
return new PigxUser(user.getUserId(), user.getDeptId(), user.getUsername(), SecurityConstants.BCRYPT + user.getPassword(), enabled,
|
||||
return new PigxUser(user.getUserId(), user.getDeptId(), user.getTenantId(), user.getUsername(), SecurityConstants.BCRYPT + user.getPassword(), enabled,
|
||||
true, true, true, authorities);
|
||||
}
|
||||
}
|
||||
+27
-4
@@ -21,6 +21,7 @@ package com.pig4cloud.pigx.common.security.util;
|
||||
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import com.pig4cloud.pigx.common.security.service.PigxUser;
|
||||
import org.springframework.security.core.Authentication;
|
||||
@@ -38,6 +39,7 @@ import java.util.List;
|
||||
* @author L.cm
|
||||
*/
|
||||
public class SecurityUtils {
|
||||
private static final ThreadLocal<Integer> THREAD_LOCAL_TENANT = new TransmittableThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 获取Authentication
|
||||
@@ -82,14 +84,35 @@ public class SecurityUtils {
|
||||
*
|
||||
* @return 角色集合
|
||||
*/
|
||||
public static List<String> getRoles() {
|
||||
public static List<Integer> getRoles() {
|
||||
Authentication authentication = getAuthentication();
|
||||
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
||||
|
||||
List<String> roles = new ArrayList<>();
|
||||
List<Integer> roleIds = new ArrayList<>();
|
||||
authorities.stream()
|
||||
.filter(granted -> StrUtil.startWith(granted.getAuthority(), SecurityConstants.ROLE))
|
||||
.forEach(granted -> roles.add(StrUtil.removePrefix(granted.getAuthority(), SecurityConstants.ROLE)));
|
||||
return roles;
|
||||
.forEach(granted -> {
|
||||
String id = StrUtil.removePrefix(granted.getAuthority(), SecurityConstants.ROLE);
|
||||
roleIds.add(Integer.parseInt(id));
|
||||
});
|
||||
return roleIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* TTL 设置租户ID
|
||||
*
|
||||
* @param tenantId
|
||||
*/
|
||||
public static void setTenantId(Integer tenantId) {
|
||||
THREAD_LOCAL_TENANT.set(tenantId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取TTL中的租户ID
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static Integer getTenantId() {
|
||||
return THREAD_LOCAL_TENANT.get();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user