Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
qraddx 2022-01-21 13:33:22 +08:00
commit d7aeb07d4a
2 changed files with 10 additions and 10 deletions

View File

@ -18,6 +18,7 @@ package com.pig4cloud.pig.common.core.util;
import cn.hutool.core.codec.Base64; import cn.hutool.core.codec.Base64;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.pig4cloud.pig.common.core.constant.CommonConstants;
import com.pig4cloud.pig.common.core.exception.CheckedException; import com.pig4cloud.pig.common.core.exception.CheckedException;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
@ -36,7 +37,6 @@ import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Optional; import java.util.Optional;
@ -143,7 +143,7 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
* @param contentType contentType * @param contentType contentType
*/ */
public void renderJson(HttpServletResponse response, Object result, String contentType) { public void renderJson(HttpServletResponse response, Object result, String contentType) {
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding(CommonConstants.UTF8);
response.setContentType(contentType); response.setContentType(contentType);
try (PrintWriter out = response.getWriter()) { try (PrintWriter out = response.getWriter()) {
out.append(JSONUtil.toJsonStr(result)); out.append(JSONUtil.toJsonStr(result));
@ -164,7 +164,7 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
} }
@SneakyThrows @SneakyThrows
public String getClientId(HttpServletRequest request) { public String getClientId() {
if (WebUtils.getRequest().isPresent()) { if (WebUtils.getRequest().isPresent()) {
String header = WebUtils.getRequest().get().getHeader(HttpHeaders.AUTHORIZATION); String header = WebUtils.getRequest().get().getHeader(HttpHeaders.AUTHORIZATION);
return splitClient(header)[0]; return splitClient(header)[0];
@ -173,7 +173,7 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
} }
@NotNull @NotNull
private static String[] splitClient(String header) throws UnsupportedEncodingException { private static String[] splitClient(String header) {
if (header == null || !header.startsWith(BASIC_)) { if (header == null || !header.startsWith(BASIC_)) {
throw new CheckedException("请求头中client信息为空"); throw new CheckedException("请求头中client信息为空");
} }

View File

@ -16,7 +16,7 @@
package com.pig4cloud.pig.common.security.component; package com.pig4cloud.pig.common.security.component;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.ArrayUtil;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
@ -32,12 +32,12 @@ import java.util.Collection;
public class PermissionService { public class PermissionService {
/** /**
* 判断接口是否有xxx:xxx权限 * 判断接口是否有任意xxxxxx权限
* @param permission 权限 * @param permissions 权限
* @return {boolean} * @return {boolean}
*/ */
public boolean hasPermission(String permission) { public boolean hasPermission(String... permissions) {
if (StrUtil.isBlank(permission)) { if (ArrayUtil.isEmpty(permissions)) {
return false; return false;
} }
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
@ -46,7 +46,7 @@ public class PermissionService {
} }
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
return authorities.stream().map(GrantedAuthority::getAuthority).filter(StringUtils::hasText) return authorities.stream().map(GrantedAuthority::getAuthority).filter(StringUtils::hasText)
.anyMatch(x -> PatternMatchUtils.simpleMatch(permission, x)); .anyMatch(x -> PatternMatchUtils.simpleMatch(permissions, x));
} }
} }