增加全功能spring-cloud-gateway,优化部分公共模块适配webFlux
This commit is contained in:
+9
-6
@@ -6,6 +6,7 @@ import org.springframework.context.annotation.Import;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
|
||||
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
|
||||
import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;
|
||||
@@ -52,12 +53,14 @@ public class DefaultResourceServerConf extends ResourceServerConfigurerAdapter {
|
||||
.antMatchers(HttpMethod.OPTIONS).permitAll()
|
||||
.anyRequest();
|
||||
setAuthenticate(authorizedUrl);
|
||||
//允许使用iframe 嵌套,避免swagger-ui 不被加载的问题
|
||||
http.headers()
|
||||
.frameOptions()
|
||||
.disable()
|
||||
.and()
|
||||
.csrf().disable();
|
||||
|
||||
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
|
||||
.and()
|
||||
.httpBasic().disable()
|
||||
.headers()
|
||||
.frameOptions().disable()
|
||||
.and()
|
||||
.csrf().disable();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.central.oauth2.common.config;
|
||||
|
||||
import com.central.oauth2.common.properties.SecurityProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2019/10/7
|
||||
* <p>
|
||||
* Blog: https://blog.csdn.net/zlt2000
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@EnableConfigurationProperties(SecurityProperties.class)
|
||||
public class SecurityPropertiesConfig {
|
||||
}
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
package com.central.oauth2.common.service;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 请求权限判断service
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2018/10/28
|
||||
*/
|
||||
public interface IPermissionService {
|
||||
/**
|
||||
* 判断请求是否有权限
|
||||
*
|
||||
* @param authentication 认证信息
|
||||
* @return 是否有权限
|
||||
*/
|
||||
boolean hasPermission(HttpServletRequest request, Authentication authentication);
|
||||
}
|
||||
+6
-9
@@ -6,7 +6,6 @@ import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.context.TenantContextHolder;
|
||||
import com.central.common.model.SysMenu;
|
||||
import com.central.oauth2.common.properties.SecurityProperties;
|
||||
import com.central.oauth2.common.service.IPermissionService;
|
||||
import com.central.oauth2.common.util.AuthUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -18,7 +17,6 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -29,7 +27,7 @@ import java.util.stream.Collectors;
|
||||
* @date 2018/10/28
|
||||
*/
|
||||
@Slf4j
|
||||
public abstract class DefaultPermissionServiceImpl implements IPermissionService {
|
||||
public abstract class DefaultPermissionServiceImpl {
|
||||
|
||||
@Autowired
|
||||
private SecurityProperties securityProperties;
|
||||
@@ -43,10 +41,9 @@ public abstract class DefaultPermissionServiceImpl implements IPermissionService
|
||||
*/
|
||||
public abstract List<SysMenu> findMenuByRoleCodes(String roleCodes);
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
|
||||
public boolean hasPermission(Authentication authentication, String requestMethod, String requestURI) {
|
||||
// 前端跨域OPTIONS请求预检放行 也可通过前端配置代理实现
|
||||
if (HttpMethod.OPTIONS.name().equalsIgnoreCase(request.getMethod())) {
|
||||
if (HttpMethod.OPTIONS.name().equalsIgnoreCase(requestMethod)) {
|
||||
return true;
|
||||
}
|
||||
if (!(authentication instanceof AnonymousAuthenticationToken)) {
|
||||
@@ -68,7 +65,7 @@ public abstract class DefaultPermissionServiceImpl implements IPermissionService
|
||||
|
||||
//判断不进行url权限认证的api,所有已登录用户都能访问的url
|
||||
for (String path : securityProperties.getAuth().getUrlPermission().getIgnoreUrls()) {
|
||||
if (antPathMatcher.match(path, request.getRequestURI())) {
|
||||
if (antPathMatcher.match(path, requestURI)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -86,9 +83,9 @@ 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.getUrl()) && antPathMatcher.match(menu.getUrl(), request.getRequestURI())) {
|
||||
if (StringUtils.isNotEmpty(menu.getUrl()) && antPathMatcher.match(menu.getUrl(), requestURI)) {
|
||||
if (StrUtil.isNotEmpty(menu.getPathMethod())) {
|
||||
return request.getMethod().equalsIgnoreCase(menu.getPathMethod());
|
||||
return requestMethod.equalsIgnoreCase(menu.getPathMethod());
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
||||
+1
-1
@@ -52,7 +52,7 @@ public class AuthUtils {
|
||||
Enumeration<String> headers = request.getHeaders(CommonConstant.TOKEN_HEADER);
|
||||
while (headers.hasMoreElements()) {
|
||||
String value = headers.nextElement();
|
||||
if ((value.toLowerCase().startsWith(OAuth2AccessToken.BEARER_TYPE.toLowerCase()))) {
|
||||
if ((value.toLowerCase().startsWith(OAuth2AccessToken.BEARER_TYPE))) {
|
||||
String authHeaderValue = value.substring(OAuth2AccessToken.BEARER_TYPE.length()).trim();
|
||||
int commaIndex = authHeaderValue.indexOf(',');
|
||||
if (commaIndex > 0) {
|
||||
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.central.oauth2.common.config.SecurityPropertiesConfig
|
||||
Reference in New Issue
Block a user