templates.service.DefaultSecurityContextRepository.ftl Maven / Gradle / Ivy
package ${packageName}.service.security;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.web.server.context.ServerSecurityContextRepository;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.server.ServerWebExchange;
import ${packageName}.common.constants.Constants;
import ${packageName}.common.context.InnerContextHolder;
import ${packageName}.common.context.TokenContextHolder;
import ${packageName}.common.enums.InnerEnums;
import org.yes.tools.core.exception.YesBaseException;
import reactor.core.publisher.Mono;
import java.util.List;
import java.util.Objects;
/**
* 存储认证授权的相关信息
*/
@Component
public class DefaultSecurityContextRepository implements ServerSecurityContextRepository {
public final static String TOKEN_HEADER = "Authorization";
public final static String BEARER = "Bearer ";
@Resource
private TokenAuthenticationManager tokenAuthenticationManager;
@Override
public Mono save(ServerWebExchange exchange, SecurityContext context) {
return Mono.empty();
}
<#if isSpringCloud==1>
@Override
public Mono load(ServerWebExchange exchange) {
ServerHttpRequest request = exchange.getRequest();
List headers = request.getHeaders().get(TOKEN_HEADER);
List forms = request.getHeaders().get(Constants.FROM);
InnerEnums innerType = InnerContextHolder.getInnerType();
if (Objects.isNull(innerType)) {
return Mono.empty();
}
//
if (innerType.equals(InnerEnums.NOT_PASS)) {
Mono securityContextMono = getTokenAuthenticeation(headers);
if (securityContextMono != null) {
return securityContextMono;
}
return Mono.empty();
} else if (innerType.equals(InnerEnums.HALF_PASS)) {
if (!CollectionUtils.isEmpty(forms)) {
String form = forms.get(0);
if (StringUtils.isNotEmpty(form) && form.equals(Constants.FROM_IN)) {
Mono securityContextMono = getTokenAuthenticeation(headers);
if (securityContextMono != null) {
return securityContextMono;
}
throw new YesBaseException("feign调用无token传递");
}
throw new YesBaseException("feign调用无内部调用参数");
}
throw new YesBaseException("feign调用无内部调用参数");
} else {
TokenContextHolder.setToken(null);
return tokenAuthenticationManager.authenticate(new UsernamePasswordAuthenticationToken(null, null))
.map(SecurityContextImpl::new);
}
}
<#else >
@Override
public Mono load(ServerWebExchange exchange) {
ServerHttpRequest request = exchange.getRequest();
List headers = request.getHeaders().get(TOKEN_HEADER);
InnerEnums innerType = InnerContextHolder.getInnerType();
if (Objects.isNull(innerType)) {
return Mono.empty();
}
//
if (innerType.equals(InnerEnums.NOT_PASS)) {
Mono securityContextMono = getTokenAuthenticeation(headers);
if (securityContextMono != null) {
return securityContextMono;
}
return Mono.empty();
} else {
TokenContextHolder.setToken(null);
return tokenAuthenticationManager.authenticate(new UsernamePasswordAuthenticationToken(null, null))
.map(SecurityContextImpl::new);
}
}
#if>
private Mono getTokenAuthenticeation(List headers) {
if (!CollectionUtils.isEmpty(headers)) {
String authorization = headers.get(0);
if (StringUtils.isNotEmpty(authorization)) {
String token = authorization.substring(BEARER.length());
if (StringUtils.isNotEmpty(token)) {
TokenContextHolder.setToken(token);
return tokenAuthenticationManager.authenticate(new UsernamePasswordAuthenticationToken(token, null))
.map(SecurityContextImpl::new);
}
}
}
return null;
}
}