优化url权限,并增加用户管理的前后端结合样例
This commit is contained in:
+9
-6
@@ -12,17 +12,20 @@ import lombok.Setter;
|
||||
@Getter
|
||||
public class AuthProperties {
|
||||
/**
|
||||
* 要认证的url
|
||||
* 配置要认证的url(默认不需要配置)
|
||||
*
|
||||
* 优先级大于忽略认证配置`zlt.security.ignore.httpUrls`
|
||||
* 意思是如果同一个url同时配置了`忽略认证`和`需要认证`,则该url还是会被认证
|
||||
*/
|
||||
private String[] httpUrls = {};
|
||||
|
||||
/**
|
||||
* 是否开启url权限验证
|
||||
*/
|
||||
private Boolean urlEnabled = false;
|
||||
|
||||
/**
|
||||
* token自动续签配置(目前只有redis实现)
|
||||
*/
|
||||
private RenewProperties renew = new RenewProperties();
|
||||
|
||||
/**
|
||||
* url权限配置
|
||||
*/
|
||||
private UrlPermissionProperties urlPermission = new UrlPermissionProperties();
|
||||
}
|
||||
|
||||
-5
@@ -33,11 +33,6 @@ public class PermitProperties {
|
||||
*/
|
||||
private String[] httpUrls = {};
|
||||
|
||||
/**
|
||||
* 设置认证后不需要判断具体权限的url,所有登录的人都能访问
|
||||
*/
|
||||
private String[] menusPaths = {};
|
||||
|
||||
public String[] getUrls() {
|
||||
if (httpUrls == null || httpUrls.length == 0) {
|
||||
return ENDPOINTS;
|
||||
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.central.oauth2.common.properties;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* url权限配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/8/2
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
public class UrlPermissionProperties {
|
||||
/**
|
||||
* 是否开启url级别权限
|
||||
*/
|
||||
private Boolean enable = false;
|
||||
|
||||
/**
|
||||
* 白名单,配置需要url权限认证的应用id(与黑名单互斥,只能配置其中一个),不配置默认所有应用都生效
|
||||
* 配置enable为true时才生效
|
||||
*/
|
||||
private List<String> includeClientIds = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 黑名单,配置不需要url权限认证的应用id(与白名单互斥,只能配置其中一个)
|
||||
* 配置enable为true时才生效
|
||||
*/
|
||||
private List<String> exclusiveClientIds = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 配置只进行登录认证,不进行url权限认证的api
|
||||
* 所有已登录的人都能访问的api
|
||||
*/
|
||||
private String[] ignoreUrls = {};
|
||||
}
|
||||
+36
-8
@@ -1,6 +1,7 @@
|
||||
package com.central.oauth2.common.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.model.SysMenu;
|
||||
import com.central.oauth2.common.properties.SecurityProperties;
|
||||
@@ -13,6 +14,7 @@ import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -43,13 +45,12 @@ public abstract class DefaultPermissionServiceImpl implements IPermissionService
|
||||
@Override
|
||||
public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
|
||||
// 前端跨域OPTIONS请求预检放行 也可通过前端配置代理实现
|
||||
// 在这里放行具有一定风险,也可通过前端配置代理实现
|
||||
if (HttpMethod.OPTIONS.name().equalsIgnoreCase(request.getMethod())) {
|
||||
return true;
|
||||
}
|
||||
if (!(authentication instanceof AnonymousAuthenticationToken)) {
|
||||
//判断是否开启url权限验证
|
||||
if (!securityProperties.getAuth().getUrlEnabled()) {
|
||||
if (!securityProperties.getAuth().getUrlPermission().getEnable()) {
|
||||
return true;
|
||||
}
|
||||
//超级管理员admin不需认证
|
||||
@@ -58,8 +59,14 @@ public abstract class DefaultPermissionServiceImpl implements IPermissionService
|
||||
return true;
|
||||
}
|
||||
|
||||
//判断认证通过后,所有用户都能访问的url
|
||||
for (String path : securityProperties.getIgnore().getMenusPaths()) {
|
||||
OAuth2Authentication auth2Authentication = (OAuth2Authentication)authentication;
|
||||
//判断应用黑白名单
|
||||
if (!isNeedAuth(auth2Authentication.getOAuth2Request().getClientId())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
//判断不进行url权限认证的api,所有已登录用户都能访问的url
|
||||
for (String path : securityProperties.getAuth().getUrlPermission().getIgnoreUrls()) {
|
||||
if (antPathMatcher.match(path, request.getRequestURI())) {
|
||||
return true;
|
||||
}
|
||||
@@ -74,14 +81,35 @@ public abstract class DefaultPermissionServiceImpl implements IPermissionService
|
||||
String roleCodes = grantedAuthorityList.stream().map(SimpleGrantedAuthority::getAuthority).collect(Collectors.joining(", "));
|
||||
List<SysMenu> menuList = findMenuByRoleCodes(roleCodes);
|
||||
for (SysMenu menu : menuList) {
|
||||
if (StringUtils.isNotEmpty(menu.getPath()) && StringUtils.isNotEmpty(menu.getPathMethod())
|
||||
&& antPathMatcher.match(menu.getPath(), request.getRequestURI())
|
||||
&& request.getMethod().equalsIgnoreCase(menu.getPathMethod())) {
|
||||
return true;
|
||||
if (StringUtils.isNotEmpty(menu.getUrl()) && antPathMatcher.match(menu.getUrl(), request.getRequestURI())) {
|
||||
if (StrUtil.isNotEmpty(menu.getPathMethod())) {
|
||||
return request.getMethod().equalsIgnoreCase(menu.getPathMethod());
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断应用是否满足白名单和黑名单的过滤逻辑
|
||||
* @param clientId 应用id
|
||||
* @return true(需要认证),false(不需要认证)
|
||||
*/
|
||||
private boolean isNeedAuth(String clientId) {
|
||||
boolean result = true;
|
||||
//白名单
|
||||
List<String> includeClientIds = securityProperties.getAuth().getUrlPermission().getIncludeClientIds();
|
||||
//黑名单
|
||||
List<String> exclusiveClientIds = securityProperties.getAuth().getUrlPermission().getExclusiveClientIds();
|
||||
if (includeClientIds.size() > 0) {
|
||||
result = includeClientIds.contains(clientId);
|
||||
} else if(exclusiveClientIds.size() > 0) {
|
||||
result = !exclusiveClientIds.contains(clientId);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user