feat: 替换swagger2为OpenAPI3
This commit is contained in:
-49
@@ -1,49 +0,0 @@
|
||||
package com.central.gateway.auth;
|
||||
|
||||
import org.springframework.security.authentication.ReactiveAuthenticationManager;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||
import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2019/10/6
|
||||
* <p>
|
||||
* Blog: http://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
public class CustomAuthenticationManager implements ReactiveAuthenticationManager {
|
||||
private TokenStore tokenStore;
|
||||
|
||||
public CustomAuthenticationManager(TokenStore tokenStore) {
|
||||
this.tokenStore = tokenStore;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Authentication> authenticate(Authentication authentication) {
|
||||
return Mono.justOrEmpty(authentication)
|
||||
.filter(a -> a instanceof BearerTokenAuthenticationToken)
|
||||
.cast(BearerTokenAuthenticationToken.class)
|
||||
.map(BearerTokenAuthenticationToken::getToken)
|
||||
.flatMap((accessTokenValue -> {
|
||||
OAuth2AccessToken accessToken = tokenStore.readAccessToken(accessTokenValue);
|
||||
if (accessToken == null) {
|
||||
return Mono.error(new InvalidTokenException("Invalid access token: " + accessTokenValue));
|
||||
} else if (accessToken.isExpired()) {
|
||||
tokenStore.removeAccessToken(accessToken);
|
||||
return Mono.error(new InvalidTokenException("Access token expired: " + accessTokenValue));
|
||||
}
|
||||
|
||||
OAuth2Authentication result = tokenStore.readAuthentication(accessToken);
|
||||
if (result == null) {
|
||||
return Mono.error(new InvalidTokenException("Invalid access token: " + accessTokenValue));
|
||||
}
|
||||
return Mono.just(result);
|
||||
}))
|
||||
.cast(Authentication.class);
|
||||
}
|
||||
}
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
package com.central.gateway.auth;
|
||||
|
||||
import com.central.oauth2.common.properties.SecurityProperties;
|
||||
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* 自定义 ServerWebExchangeMatcher
|
||||
* 解决只要请求携带access_token,排除鉴权的url依然会被拦截
|
||||
*
|
||||
* @author zlt
|
||||
* @version 1.0
|
||||
* @date 2022/6/10
|
||||
* <p>
|
||||
* Blog: http://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
public class CustomServerWebExchangeMatchers implements ServerWebExchangeMatcher {
|
||||
private final SecurityProperties securityProperties;
|
||||
|
||||
private final AntPathMatcher antPathMatcher = new AntPathMatcher();
|
||||
|
||||
public CustomServerWebExchangeMatchers(SecurityProperties securityProperties) {
|
||||
this.securityProperties = securityProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<MatchResult> matches(ServerWebExchange exchange) {
|
||||
for (String url : securityProperties.getIgnore().getUrls()) {
|
||||
if (antPathMatcher.match(url, exchange.getRequest().getURI().getPath())) {
|
||||
return MatchResult.notMatch();
|
||||
}
|
||||
}
|
||||
return MatchResult.match();
|
||||
}
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.central.gateway.auth;
|
||||
|
||||
import com.central.common.utils.WebfluxResponseUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.web.server.authorization.ServerAccessDeniedHandler;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* 403拒绝访问异常处理,转换为JSON
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/10/7
|
||||
* <p>
|
||||
* Blog: http://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Slf4j
|
||||
public class JsonAccessDeniedHandler implements ServerAccessDeniedHandler {
|
||||
@Override
|
||||
public Mono<Void> handle(ServerWebExchange exchange, AccessDeniedException e) {
|
||||
return WebfluxResponseUtil.responseFailed(exchange, HttpStatus.FORBIDDEN.value(), e.getMessage());
|
||||
}
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.central.gateway.auth;
|
||||
|
||||
import com.central.common.utils.WebfluxResponseUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* 401未授权异常处理,转换为JSON
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/10/7
|
||||
* <p>
|
||||
* Blog: http://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Slf4j
|
||||
public class JsonAuthenticationEntryPoint implements ServerAuthenticationEntryPoint {
|
||||
@Override
|
||||
public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException e) {
|
||||
return WebfluxResponseUtil.responseFailed(exchange, HttpStatus.UNAUTHORIZED.value(), e.getMessage());
|
||||
}
|
||||
}
|
||||
-54
@@ -1,54 +0,0 @@
|
||||
package com.central.gateway.auth;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.model.SysUser;
|
||||
import com.central.oauth2.common.util.AuthUtils;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.security.web.server.WebFilterExchange;
|
||||
import org.springframework.security.web.server.authentication.ServerAuthenticationSuccessHandler;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* 认证成功处理类
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/10/7
|
||||
* <p>
|
||||
* Blog: http://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
public class Oauth2AuthSuccessHandler implements ServerAuthenticationSuccessHandler {
|
||||
@Override
|
||||
public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange, Authentication authentication) {
|
||||
MultiValueMap<String, String> headerValues = new LinkedMultiValueMap<>(4);
|
||||
Object principal = authentication.getPrincipal();
|
||||
//客户端模式只返回一个clientId
|
||||
if (principal instanceof SysUser) {
|
||||
SysUser user = (SysUser)authentication.getPrincipal();
|
||||
headerValues.add(SecurityConstants.USER_ID_HEADER, String.valueOf(user.getId()));
|
||||
headerValues.add(SecurityConstants.USER_HEADER, user.getUsername());
|
||||
}
|
||||
OAuth2Authentication oauth2Authentication = (OAuth2Authentication)authentication;
|
||||
String clientId = oauth2Authentication.getOAuth2Request().getClientId();
|
||||
headerValues.add(SecurityConstants.TENANT_HEADER, clientId);
|
||||
headerValues.add(SecurityConstants.ROLE_HEADER, CollectionUtil.join(authentication.getAuthorities(), ","));
|
||||
String accountType = AuthUtils.getAccountType(oauth2Authentication.getUserAuthentication());
|
||||
if (StrUtil.isNotEmpty(accountType)) {
|
||||
headerValues.add(SecurityConstants.ACCOUNT_TYPE_HEADER, accountType);
|
||||
}
|
||||
ServerWebExchange exchange = webFilterExchange.getExchange();
|
||||
ServerHttpRequest serverHttpRequest = exchange.getRequest().mutate()
|
||||
.headers(h -> h.addAll(headerValues))
|
||||
.build();
|
||||
|
||||
ServerWebExchange build = exchange.mutate().request(serverHttpRequest).build();
|
||||
return webFilterExchange.getChain().filter(build);
|
||||
}
|
||||
}
|
||||
-56
@@ -1,56 +0,0 @@
|
||||
package com.central.gateway.auth;
|
||||
|
||||
import com.central.common.model.SysMenu;
|
||||
import com.central.gateway.feign.AsynMenuService;
|
||||
import com.central.oauth2.common.service.impl.DefaultPermissionServiceImpl;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.security.authorization.AuthorizationDecision;
|
||||
import org.springframework.security.authorization.ReactiveAuthorizationManager;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.web.server.authorization.AuthorizationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
* url权限认证
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/10/6
|
||||
* <p>
|
||||
* Blog: http://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class PermissionAuthManager extends DefaultPermissionServiceImpl implements ReactiveAuthorizationManager<AuthorizationContext> {
|
||||
@Resource
|
||||
private AsynMenuService asynMenuService;
|
||||
|
||||
@Override
|
||||
public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, AuthorizationContext authorizationContext) {
|
||||
return authentication.map(auth -> {
|
||||
ServerWebExchange exchange = authorizationContext.getExchange();
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
boolean isPermission = super.hasPermission(auth, request.getMethodValue(), request.getURI().getPath());
|
||||
return new AuthorizationDecision(isPermission);
|
||||
}).defaultIfEmpty(new AuthorizationDecision(false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysMenu> findMenuByRoleCodes(String roleCodes) {
|
||||
Future<List<SysMenu>> futureResult = asynMenuService.findByRoleCodes(roleCodes);
|
||||
try {
|
||||
return futureResult.get();
|
||||
} catch (Exception e) {
|
||||
log.error("asynMenuService.findMenuByRoleCodes-error", e);
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
-71
@@ -1,71 +0,0 @@
|
||||
package com.central.gateway.config;
|
||||
|
||||
import com.central.gateway.auth.*;
|
||||
import com.central.oauth2.common.properties.SecurityProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.authentication.ReactiveAuthenticationManager;
|
||||
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
|
||||
import org.springframework.security.config.web.server.SecurityWebFiltersOrder;
|
||||
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
||||
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||
import org.springframework.security.oauth2.server.resource.web.server.authentication.ServerBearerTokenAuthenticationConverter;
|
||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||
import org.springframework.security.web.server.authentication.AuthenticationWebFilter;
|
||||
import org.springframework.security.web.server.authentication.ServerAuthenticationEntryPointFailureHandler;
|
||||
|
||||
/**
|
||||
* 资源服务器配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/10/5
|
||||
* <p>
|
||||
* Blog: http://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Configuration
|
||||
@EnableWebFluxSecurity
|
||||
public class ResourceServerConfiguration {
|
||||
@Bean
|
||||
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http
|
||||
, SecurityProperties securityProperties, TokenStore tokenStore, PermissionAuthManager permissionAuthManager) {
|
||||
//认证处理器
|
||||
ReactiveAuthenticationManager customAuthenticationManager = new CustomAuthenticationManager(tokenStore);
|
||||
JsonAuthenticationEntryPoint entryPoint = new JsonAuthenticationEntryPoint();
|
||||
//token转换器
|
||||
ServerBearerTokenAuthenticationConverter tokenAuthenticationConverter = new ServerBearerTokenAuthenticationConverter();
|
||||
tokenAuthenticationConverter.setAllowUriQueryParameter(true);
|
||||
//oauth2认证过滤器
|
||||
AuthenticationWebFilter oauth2Filter = new AuthenticationWebFilter(customAuthenticationManager);
|
||||
oauth2Filter.setServerAuthenticationConverter(tokenAuthenticationConverter);
|
||||
oauth2Filter.setAuthenticationFailureHandler(new ServerAuthenticationEntryPointFailureHandler(entryPoint));
|
||||
oauth2Filter.setAuthenticationSuccessHandler(new Oauth2AuthSuccessHandler());
|
||||
oauth2Filter.setRequiresAuthenticationMatcher(new CustomServerWebExchangeMatchers(securityProperties));
|
||||
http.addFilterAt(oauth2Filter, SecurityWebFiltersOrder.AUTHENTICATION);
|
||||
|
||||
ServerHttpSecurity.AuthorizeExchangeSpec authorizeExchange = http.authorizeExchange();
|
||||
if (securityProperties.getAuth().getHttpUrls().length > 0) {
|
||||
authorizeExchange.pathMatchers(securityProperties.getAuth().getHttpUrls()).authenticated();
|
||||
}
|
||||
if (securityProperties.getIgnore().getUrls().length > 0) {
|
||||
authorizeExchange.pathMatchers(securityProperties.getIgnore().getUrls()).permitAll();
|
||||
}
|
||||
authorizeExchange
|
||||
.pathMatchers(HttpMethod.OPTIONS).permitAll()
|
||||
.anyExchange()
|
||||
.access(permissionAuthManager)
|
||||
.and()
|
||||
.exceptionHandling()
|
||||
.accessDeniedHandler(new JsonAccessDeniedHandler())
|
||||
.authenticationEntryPoint(entryPoint)
|
||||
.and()
|
||||
.headers()
|
||||
.frameOptions()
|
||||
.disable()
|
||||
.and()
|
||||
.httpBasic().disable()
|
||||
.csrf().disable();
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user